说丑不丑,说好看也不好看的路灯

1.先看效果

2.源代码

import React, { useState, useEffect } from 'react'


export default function StreetLamp() {

const [isNight, setIsNight] = useState(false)

const [isLampOn, setIsLampOn] = useState(false)

const [isAutoMode, setIsAutoMode] = useState(true)


useEffect(() => {

const checkTime = () => {

const currentHour = new Date().getHours()

setIsNight(currentHour >= 18 || currentHour < 6)

}


checkTime()

const interval = setInterval(checkTime, 60000) // Check every minute


return () => clearInterval(interval)

}, [])


useEffect(() => {

if (isAutoMode) {

setIsLampOn(isNight)

}

}, [isNight, isAutoMode])


const toggleLamp = () => {

if (isAutoMode) {

setIsAutoMode(false)

}

setIsLampOn(!isLampOn)

}


return (

Interactive Street Lamp

Auto

type="checkbox"

checked={isAutoMode}

onChange={() => setIsAutoMode(!isAutoMode)}

/>


{/* Lamp post */}

{/* Lamp head */}

{/* Light beam */}

d="M30 20 L10 100 H90 L70 20"

fill={isLampOn ? "rgba(255, 255, 200, 0.2)" : "transparent"}

>

attributeName="opacity"

values={isLampOn ? "0.2;0.5;0.2" : "0;0"}

dur="4s"

repeatCount="indefinite"

/>

{/* Lamp light */}

cx="50"

cy="20"

r="10"

fill={isLampOn ? "#FFFFA0" : "#888"}

>

attributeName="opacity"

values={isLampOn ? "0.7;1;0.7" : "1;1"}

dur="4s"

repeatCount="indefinite"

/>


{/* Sun/Moon */}

className={`absolute top-4 right-4 w-12 h-12 rounded-full transition-all duration-1000 ${

isNight ? "bg-gray-300" : "bg-yellow-400"

}`}

>


)

}


3.同时还需要这段css代码

.switch {

position: relative;

display: inline-block;

width: 60px;

height: 34px;

}


.switch input {

opacity: 0;

width: 0;

height: 0;

}


.slider {

position: absolute;

cursor: pointer;

top: 0;

left: 0;

right: 0;

bottom: 0;

background-color: #ccc;

transition: .4s;

}


.slider:before {

position: absolute;

content: "";

height: 26px;

width: 26px;

left: 4px;

bottom: 4px;

background-color: white;

transition: .4s;

}


input:checked + .slider {

background-color: #2196F3;

}


input:checked + .slider:before {

transform: translateX(26px);

}


.slider.round {

border-radius: 34px;

}


.slider.round:before {

border-radius: 50%;

}

×
二维码

扫描二维码分享

评论区

登录后发表评论。