实现点击div能改变伪类元素的颜色
<style type = "text/css">
div{
width:80px;
height:80px;
background-color:red;
}
.green::after{
content:'';
width:30px;
height:30px;
background-color:green;
display:inline-block;
}
.yellow::after{
content:'';
width:30px;
height:30px;
background-color:yellow;
display:inline-block;
}
</style>
<body>
<div class = 'green'></div>
<script type = "text/javascript" src="test.js"></script>
</body>
实现点击div能改变div样式,再次点击又能变回原来样式
<style type = "text/css">
div{
width:80px;
height:80px;
background-color:red;
}
.active{
width:150px;
height:150px;
background-color:purple;
}
</style>
<body>
<div></div>
<script type = "text/javascript" src="test.js"></script>
</body>
实现点击小方块移动
var div = document.getElementsByTagName('div')[0];
var speed = 10;
div.onclick = function(){
var timer = setInterval(function(){
function getStyle(elem,prop){
if(window.getComputedStyle(elem,null)[prop]){
return window.getComputedStyle(elem,null)[prop];
}else{
elem.currentStyle[prop];
}
}
speed += speed/7;
div.style.left = parseInt(getStyle(div,'left')) + speed + 'px';
if(parseInt(div.style.left) > 300){
clearInterval(timer);
}
},10)
}