課程
/前端開發
/JavaScript
/JS動畫效果
怎么不動啊
2016-12-26
源自:JS動畫效果 2-1
正在回答
我按照你的代碼重新寫了一遍,可以運動,沒毛病。
你可以參考下。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS速度動畫</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
font-size: 14px;
}
#div1{
width: 200px;
height: 200px;
background: red;
text-align: center;
position: relative;
left: -200px;
top: 0px;
#div1 span{
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
padding-top: 15px;
color: #fff;
</style>
<script type="text/javascript">
? ? window.onload=function(){
? ? ?var oDiv = document.getElementById('div1');
? ? ?oDiv.onmouseover=function(){
? ? ?startMove();
? ? ?};
? ? };
var timer = null;
function startMove(){
var oDiv = document.getElementById('div1');
setInterval(function(){
//如果當前值為0(即目標值)則清除計時器
if(oDiv.offsetLeft === 0){
clearInterval(timer);
}else{
//offsetLeft當前值
oDiv.style.left = oDiv.offsetLeft+10+'px';
},30);
</script>
</head>
<body>
<div id="div1"><span id="share">分享</span></div>
</body>
</html>
老師不是說了嗎,每次執行這個函數時,清除定時器啊
舉報
通過本課程JS動畫的學習,從簡單動畫開始,逐步深入各種動畫框架封裝
1 回答為什么不會停止?
1 回答為什么clearInterval停止不了?
1 回答為什么動畫無法停止呢
2 回答關于動畫停止
3 回答關于動畫停止
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-12-27
我按照你的代碼重新寫了一遍,可以運動,沒毛病。
你可以參考下。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS速度動畫</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
font-size: 14px;
}
#div1{
width: 200px;
height: 200px;
background: red;
text-align: center;
position: relative;
left: -200px;
top: 0px;
}
#div1 span{
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
padding-top: 15px;
color: #fff;
}
</style>
<script type="text/javascript">
? ? window.onload=function(){
? ? ?var oDiv = document.getElementById('div1');
? ? ?oDiv.onmouseover=function(){
? ? ?startMove();
? ? ?};
? ? };
var timer = null;
function startMove(){
var oDiv = document.getElementById('div1');
setInterval(function(){
//如果當前值為0(即目標值)則清除計時器
if(oDiv.offsetLeft === 0){
clearInterval(timer);
}else{
//offsetLeft當前值
oDiv.style.left = oDiv.offsetLeft+10+'px';
}
},30);
}
</script>
</head>
<body>
<div id="div1"><span id="share">分享</span></div>
</body>
</html>
2016-12-27
老師不是說了嗎,每次執行這個函數時,清除定時器啊