请大佬看看,要怎么解决这个bug呢。<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> <style>img{border: none;}#demo {position: relative;background: red;overflow:hidden;width: 500px;}#indemo {float: left;width: 800%;}#demo1 {float: left;}#demo2 {float: left;}</style> <body> <div id="demo"><div id="indemo"><div id="demo1"><a href="#"><img src="img/1.png" border="0" /></a><a href="#"><img src="img/2.png" border="0" /></a><a href="#"><img src="img/3.png" border="0" /></a><a href="#"><img src="img/4.png" border="0" /></a><a href="#"><img src="img/5.png" border="0" /></a><a href="#"><img src="img/6.png" border="0" /></a></div><div id="demo2"></div></div></div><div> <p id="tab"></p> <p id="tab2"></p></div> <script>var speed=10; //数字越大速度越慢var tab=document.getElementById("demo");var tab1=document.getElementById("demo1");var tab2=document.getElementById("demo2");tab2.innerHTML=tab1.innerHTML;function Marquee(){ document.getElementById('tab').innerHTML=tab.scrollLeft document.getElementById('tab2').innerHTML=tab2.offsetWidth if(tab2.offsetWidth-tab.scrollLeft<=0)tab.scrollLeft-=tab1.offsetWidthelse{tab.scrollLeft++;}}var MyMar=setInterval(Marquee,speed);tab.onmouseover=function() {clearInterval(MyMar)};tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};</script> </body></html>
1 回答
已采纳
stone310
TA贡献361条经验 获得超191个赞
测试了一下,scrollleft的变化值,当页面缩小时,只有取缩小的整数倍才发生变化(例如缩小2倍,就要2的倍数);具体原理可能跟scrollleft属性相关;建议做无缝还是用absolute然后left控制更稳定
写了个把你的代码修改了下,可以看的更清楚,你可以测试一下
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> <style> img{border: none;} #demo { position: relative; background: red; overflow:hidden; width: 500px; } #indemo { float: left; width: 800%; } #demo1 { float: left; } #demo2 { float: left; } </style> </head> <body> <div id="demo"> <div id="indemo"> <div id="demo1"> <a href="#"><img src="img/1.png" border="0" /></a> <a href="#"><img src="img/2.png" border="0" /></a> <a href="#"><img src="img/3.png" border="0" /></a> <a href="#"><img src="img/4.png" border="0" /></a> <a href="#"><img src="img/5.png" border="0" /></a> <a href="#"><img src="img/6.png" border="0" /></a> </div> <div id="demo2"></div> </div> </div> <div> <p id="tab"></p> <p id="tab2"></p> </div> <p style="font-size:30px"><b>用键盘左右键控制以下拉杆</b></p> <input type="range" min="0" max="100" id="range1" style="width:1000px;" value="0" autofocus/> <script> var speed=10; //数字越大速度越慢 var tab=document.getElementById("demo"); var tab1=document.getElementById("demo1"); var tab2=document.getElementById("demo2"); var range1=document.getElementById("range1") tab2.innerHTML=tab1.innerHTML; var range=0 var a range1.onchange=function(){ range=parseInt(range1.value) } function Marquee(){ a=tab.scrollLeft; if(tab2.offsetWidth-tab.scrollLeft<=0){ tab.scrollLeft-=tab1.offsetWidth } else{ tab.scrollLeft=tab.scrollLeft+range; document.getElementById('tab').innerHTML="当前设置的移动的速度是:"+range document.getElementById('tab2').innerHTML="scrollLeft对应变化值是"+( tab.scrollLeft-a) } } var MyMar=setInterval(Marquee,speed); tab.onmouseover=function() {clearInterval(MyMar)}; tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)}; </script> </body> </html>
添加回答
举报
0/150
提交
取消