jq圖片無限循環滾動
剛開始寫的這段代碼沒有加判斷false出現錯誤,即鼠標第二次放上去不會停止滾動且移開之後滾動速度加快。加了之後就變正常了,banner滾動也一樣。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css" media="screen">
*{padding: 0;margin: 0;}
.box a{text-decoration: none;color: #666;}
.box{width: 1920px;margin: 0 auto;height: 420px;overflow: hidden;}
.indome{width: 99999px;}
.box .slider1,.box .slider2{list-style: none;float: left;}
.box .slider1 li,.box .slider2 li{float:left;}
.page a{display: inline-block;width: 40px;height: 68px;text-align: center;line-height: 68px;background: rgba(0,0,0,0.2);font-size: 24px;color: #fff;top: 45%;position: absolute;z-index: 99;}
.page a.pre{left: 0;}
.page a.next{right: 0;}
</style>
</head>
<body>
<div class="box">
<div class="indome">
<ul class="slider1">
<li><a href="" title=""><img src="banner1.jpg" ></a></li>
<li><a href="" title=""><img src="banner2.jpg" ></a></li>
<li><a href="" title=""><img src="banner3.jpg" ></a></li>
</ul>
<ul class="slider2">
</ul>
</div>
</div>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
var t=1;
var li_length=$(".slider1 li img").width();
var box=$(".box");
var slider1=$(".slider1")
var slider2=$(".slider2")
var length=$(".slider1 li").length;
var flase=false;
var timer
$(".slider1").css("width",length*li_length)
$(".slider2").css("width",length*li_length)
var slider1Html=$(".slider1").html();
$(".slider2").append(slider1Html);
function slider(){
if (box.scrollLeft()>=slider1.offsetWidth){
box.scrollLeft(0);
}
else{
box.scrollLeft(box.scrollLeft()+1);
}
}
function star(){
if(!flase){
flase=true;
timer=setInterval(slider,t)
}
}
function stop(){
flase=false;
clearInterval(timer);
}
star();
$(".box").hover(function(){
stop();
},function(){
star();
})
</script>
</body>
</html>
jq圖片無限循環滾動