亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么往左只回了一下,然后就開始來回動,求大神幫忙解答

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>速度動畫</title>
</head>
<style>

?? ?body,div,span{
?? ??? ?margin:0;
?? ??? ?padding:0;
?? ?}
?? ?#div1{
?? ??? ?width:200px;
?? ??? ?height:200px;
?? ??? ?background:red;
?? ??? ?position:relative;
?? ??? ?left:-200px;
?? ??? ?top:0;
?? ?}
?? ?#share{
?? ??? ?width:20px;
?? ??? ?height:50px;
?? ??? ?background:blue;
?? ??? ?position:absolute;
?? ??? ?left:200px;
?? ??? ?top:75px;
?? ?}
</style>

<script>
?? ?window.onload=function(){
?? ??? ?var oDiv=document.getElementById('div1');
?? ??? ?oDiv.onmouseover=function(){
?? ??? ??? ?startMove();
?? ??? ?}
?? ??? ?oDiv.onmouseout=function(){
?? ??? ??? ?startMove1();
?? ??? ?}
?? ?}
?? ?var timer=null;
?? ?
?? ?function startMove(){
?? ??? ?clearInterval(timer);//防止每次鼠標經過的時候都要出發定時器
?? ??? ?var oDiv=document.getElementById('div1');
?? ??? ?setInterval(function(){//定時器
?? ??? ??? ?if(oDiv.offsetLeft>=0){
?? ??? ??? ??? ?clearInterval(timer);//清空/暫停定時器
?? ??? ??? ?}else{
?? ??? ??? ??? ?oDiv.style.left=oDiv.offsetLeft+10+'px';
?? ??? ??? ?}
?? ??? ?},30)
?? ?}
?? ?
?? ?function startMove1(){
?? ??? ?clearInterval(timer);//防止每次鼠標經過的時候都要出發定時器
?? ??? ?var oDiv=document.getElementById('div1');
?? ??? ?setInterval(function(){//定時器
?? ??? ??? ?if(oDiv.offsetLeft<=-200){
?? ??? ??? ??? ?clearInterval(timer);//清空/暫停定時器
?? ??? ??? ?}else{
?? ??? ??? ??? ?oDiv.style.left=oDiv.offsetLeft-10+'px';
?? ??? ??? ?}
?? ??? ?},30)
?? ?}
?? ?
</script>


<body>
?? ?<div id="div1">
?? ??? ?<span id="share">分享</span>
?? ?</div>
</body>
</html>

正在回答

2 回答

你的代碼應該修改為

<!DOCTYPE?html>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
<title>速度動畫</title>
</head>
<style>

????body,div,span{
????????margin:0;
????????padding:0;
????}
????#div1{
????????width:200px;
????????height:200px;
????????background:red;
????????position:relative;
????????left:-200px;
????????top:0;
????}
????#share{
????????width:20px;
????????height:50px;
????????background:blue;
????????position:absolute;
????????left:200px;
????????top:75px;
????}
</style>

<script>
????window.onload=function(){
????????var?oDiv=document.getElementById('div1');
????????oDiv.onmouseover=function(){
????????????startMove();
????????}
????????oDiv.onmouseout=function(){
????????????startMove1();
????????}
????}
????var?timer=null;
????
????function?startMove(){
????????clearInterval(timer);//防止每次鼠標經過的時候都要出發定時器
????????var?oDiv=document.getElementById('div1');
?????????timer=setInterval(function(){//定時器//開啟定時器
????????????if(oDiv.offsetLeft==0){
????????????????clearInterval(timer);//清空/暫停定時器
????????????}else{
????????????????oDiv.style.left=oDiv.offsetLeft+10+'px';
????????????}
????????},30)
????}
????
????function?startMove1(){
????????clearInterval(timer);//防止每次鼠標經過的時候都要出發定時器
????????var?oDiv=document.getElementById('div1');
????????timer=setInterval(function(){//定時器
????????????if(oDiv.offsetLeft==-200){
????????????????clearInterval(timer);//清空/暫停定時器
????????????}else{
????????????????oDiv.style.left=oDiv.offsetLeft-10+'px';
????????????}
????????},30)
????}
????
</script>


<body>
????<div?id="div1">
????????<span?id="share">分享</span>
????</div>
</body>
</html>


0 回復 有任何疑惑可以回復我~
#1

y年華游你 提問者

非常感謝!
2016-03-03 回復 有任何疑惑可以回復我~

你這個錯誤很低級,你要清除定時器,可你的timer是啟動的定時器嗎?你需要將啟動定時給timer才能清除。timer=setInterval();然后clearInterval(timer);

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么往左只回了一下,然后就開始來回動,求大神幫忙解答

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號