JS按钮闪烁功能是如何实现的

JavaScript011

JS按钮闪烁功能是如何实现的,第1张

用css3更简单

首先定义一个闪烁的-webkit-animation 的name为twinkling,效果是透明度从0到1

@-webkit-keyframes twinkling{

/*透明度由0到1*/

0%{

opacity:0/*透明度为0*/

}

100%{

opacity:1/*透明度为1*/

}

}

然后设置需要闪烁的button的样式:

button{

-webkit-animation: twinkling 1s infinite ease-in-out

}

其中twinkling 为上面定义的,时间为1s,动画无限次,动画效果是ease-in-out

1、新建html文档,在body标签中添加一些button按钮,然后为这些按钮设置默认颜色为灰色:

2、添加js代码,首先获取所有的button按钮,然后使用for循环为这些按钮添加点击事件:

3、这时被点击的按钮将会更换颜色,而没有被点击的按钮依然是原来的颜色:

html

复制代码代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>apple button</title>

</head>

<body>

<div id="div1">

<div id="div2"></div>

</div>

</body>

</html>

css

复制代码代码如下:

#div1{

width: 170px

height: 100px

border-radius: 50px

position: relative

}

#div2{

width: 96px

height: 96px

border-radius: 48px

position: absolute

background: white

box-shadow: 0px 2px 4px rgba(0,0,0,0.4)

}

.open1{

background: rgba(0,184,0,0.8)

}

.open2{

top: 2px

right: 1px

}

.close1{

background: rgba(255,255,255,0.4)

border:3px solid rgba(0,0,0,0.15)

border-left: transparent

}

.close2{

left: 0px

top: 0px

border:2px solid rgba(0,0,0,0.1)

}

javascript

复制代码代码如下:

window.onload=function(){

var div2=document.getElementById("div2")

var div1=document.getElementById("div1")

div2.onclick=function(){

div1.className=(div1.className=="close1")?"open1":"close1"

div2.className=(div2.className=="close2")?"open2":"close2"

}

}