div+css+jQuery图⽚横向滚动代码(带左右点击按钮)
⾸先感谢Blue⽼师的javascript教程,给了我很多的启发,这是我在看完这节视频后,⾃⼰试着⽤jQuery重新改写了⼀下代码,感觉⾄少⽐百度搜出来的那⼀坨靠谱多了,上代码:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src = "jquery_v1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function (){
/*=============
Author:Gino
Blog:wwwblogs/ginowang42
Thanks:@Blue--www.zhinengshe/video.html#1
Name:jQuery插件图⽚左右⽆缝滚动
Arguments:
@leftBtn:向左滚动按钮DOM引⽤
@rightBtn:向右滚动按钮DOM引⽤
@speed:滚动速度(每次滚动像素数)
CSS keyed Attribute:
#noSeamScroll{position:relative;overflow:hidden;}
#noSeamScroll ul{position:absolute;}
#noSeamScroll ul li {float:left;}
===============*/
$.fn.extend({noSeamScroll:function (leftBtn,rightBtn,speed){
var timeFlag = speed = speed || 4;;
var timer = null;
var _this = this;//把this重新保存在⼀个私有变量⾥⾯,以防⽌setInterval误把this指向了window
this.oUl = $("ul",this);
this.oUl.html(this.oUl.html() + this.oUl.html());//把li复制⼀份
this.oLis = $("ul li",this);//之后再变量保存li的全部节点
this.oUl.width(this.oLis.eq(0).outerWidth(true)*this.oLis.length + "px");
var fnMove = function (){
$("ul",_this).css("left",function (){
if ($(this).position().left > 0){//这⾥的this指的是$("ul",element)
return -$(this).outerWidth(true)/2 + "px";
}
if ($(this).position().left < -$(this).outerWidth(true)/2 ){
return "0px";
}
return $(this).position().left + timeFlag + "px";
})
}
timer = setInterval(fnMove,30);
leftBtn.click(function (){
clearInterval(timer);
timeFlag = -speed;
timer = setInterval(fnMove,30);
})
rightBtn.click(function (){
clearInterval(timer);
timeFlag = speed;
timer = setInterval(fnMove,30);
})
}
});
//test
$("#noSeamScroll").noSeamScroll($("#leftArr"),$("#rightArr"),2);
})
</script>
<style type="text/css">
*{margin:0;padding:0;}
#noSeamScroll{width:752px;height:108px;margin:20px auto;position:relative;overflow:hidden;} #noSeamScroll ul{position:absolute;list-style:none;}
#noSeamScroll ul li {float:left;width:178px;height:108px;margin-right:10px;}
</style>
</head>
<body>
<a href="javascript:;" id="leftArr">向左移动</a>
<a href="javascript:;" id="rightArr">向右移动</a>
<div id = "noSeamScroll">
<ul>
<li><img src="images/1.jpg" alt=""/></li>
<li><img src="images/2.jpg" alt=""/></li>
jquery在线图片<li><img src="images/3.jpg" alt=""/></li>
<li><img src="images/4.jpg" alt=""/></li>
</ul>
</div>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论