課程
/前端開發(fā)
/JavaScript
/JS動畫效果
怎么不動啊
2016-12-26
源自:JS動畫效果 2-1
正在回答
我按照你的代碼重新寫了一遍,可以運(yùn)動,沒毛病。
你可以參考下。
<!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(){
//如果當(dāng)前值為0(即目標(biāo)值)則清除計(jì)時器
if(oDiv.offsetLeft === 0){
clearInterval(timer);
}else{
//offsetLeft當(dāng)前值
oDiv.style.left = oDiv.offsetLeft+10+'px';
},30);
</script>
</head>
<body>
<div id="div1"><span id="share">分享</span></div>
</body>
</html>
老師不是說了嗎,每次執(zhí)行這個函數(shù)時,清除定時器啊
舉報(bào)
通過本課程JS動畫的學(xué)習(xí),從簡單動畫開始,逐步深入各種動畫框架封裝
1 回答為什么不會停止?
1 回答為什么clearInterval停止不了?
1 回答為什么動畫無法停止呢
2 回答關(guān)于動畫停止
3 回答關(guān)于動畫停止
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-12-27
我按照你的代碼重新寫了一遍,可以運(yùn)動,沒毛病。
你可以參考下。
<!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(){
//如果當(dāng)前值為0(即目標(biāo)值)則清除計(jì)時器
if(oDiv.offsetLeft === 0){
clearInterval(timer);
}else{
//offsetLeft當(dāng)前值
oDiv.style.left = oDiv.offsetLeft+10+'px';
}
},30);
}
</script>
</head>
<body>
<div id="div1"><span id="share">分享</span></div>
</body>
</html>
2016-12-27
老師不是說了嗎,每次執(zhí)行這個函數(shù)時,清除定時器啊