慕丝7111264
2020-04-03 11:50
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#box1{
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#span{
width: 20px;
height: 50px;
background-color: #bfa;
position: absolute;
left: 200px;
top: 75px;
}
</style>
<script type="text/javascript">
window.onload = function(){
var box1 = document.getElementById("box1");
box1.onmouseenter = function(){
startMove();
}
box1.onmouseout = function(){
startMove2();
}
}
var timer = null;
function startMove(){
clearInterval(timer);
timer = setInterval(function(){
if(box1.offsetLeft == 0){
clearInterval(timer);
}
else{
box1.style.left = box1.offsetLeft + 10 + "px";
}
},30);
};
function startMove2(){
clearInterval(timer);
timer = setInterval(function(){
if(box1.offsetLeft == -200){
clearInterval(timer);
}
else{
box1.style.left = box1.offsetLeft - 10 + "px";
}
},30);
};
</script>
</head>
<body>
<div id="box1"><span id="span">分享</span></div>
</body>
box1.onmouseover = function () {
startMove();
}
你单词拼错了
JS动画效果
113925 学习 · 1443 问题
相似问题