html,css,js,反彈的js效果
實現效果:
實現代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js</title>
<style>
#div1{
width:100px ;
height: 100px;
background: red;
position: absolute;
left: 0px;
top: 0;
}
</style>
</head>
<body>
<!--onmousedown();-->
<div id="div1"></div>
<script>
var oDiv=document.getElementById(‘div1‘);
var disX=10;
var disY=10;
setInterval(function(){
var L=oDiv.offsetLeft+disX;
var T=oDiv.offsetTop+disY;
if(T>=document.documentElement.clientHeight-oDiv.offsetHeight){
T=document.documentElement.clientHeight-oDiv.offsetHeight;
disY*=-1;
disY*=0.75;
}
if(T<0)
{
T=0;
disY*=-1;
disY*=0.75;
}
if(L>=document.documentElement.clientWidth-oDiv.offsetWidth)
{L=document.documentElement.clientWidth-oDiv.offsetWidth;
disX*=-1;
disX*=0.75;}
if(L<0)
{L=0;
disX*=-1;
disX*=0.75;
}
// oDiv.style.left=oDiv.offsetLeft+disX+‘px‘;
// oDiv.style.top=oDiv.offsetLeft+disY+‘px‘;
oDiv.style.left=L+‘px‘;
oDiv.style.top=T+‘px‘;
},30)
</script>
</body>
</html>
html,css,js,反彈的js效果