基于jQuery的图片展示(横向滚动,可左右控制)

时间:2022-08-22 10:06:13

今天在网上看了一个图片展示的实例,就整理一下贴了出来,为的是以后使用方便,也可以和大家一起分享。

下面是整个页面,如果要使用,只需把整个页面拷贝过去,然后修改图片路径,把jQuery的包也要放进去。

展示效果:

基于jQuery的图片展示(横向滚动,可左右控制)

页面代码:

<!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=gb2312">
<title>jQuery 横向图片滚动,带左右控制</title>
<style type="text/css">
<!--
*{margin:0; padding:0}
li{list-style:none}
img{border:0}
body{background-color:#fff; font-size:12px; color:#2c2c2c; font-family:Arial, Helvetica, sans-serif}
a{color:#444; text-decoration:none}
a:hover{color:#900; text-decoration:underline}
.con{overflow:hidden; margin:30px auto; width:442px; height:100%; padding:10px 15px; border:solid 1px #FCBA79; background-color:#FFFBEC;}
#carousel_container{position:relative; height:100px; overflow:hidden;}
#carousel_inner{width:391px; height:95px; overflow: hidden; position:absolute;left:25px; top:5px;}
#left_scroll{position: absolute;left:0;top:43px;width:9px;height:9px;cursor: pointer;cursor: hand; background:url(images/left.gif) no-repeat;}
#right_scroll{position: absolute;top:43px;right:0; width: 9px;height: 9px;cursor: pointer;cursor: hand; background: url(images/right.gif) no-repeat;}
#carousel_ul{width:9999px; height:95px; position:relative;}
#carousel_ul li{float: left;width:84px; height:86px; border:solid 1px #FEDDAB; margin-right:15px; display:inline;}
-->
</style>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
<!--
var SellerScroll = function(options){
this.SetOptions(options);
this.lButton = this.options.lButton;
this.rButton = this.options.rButton;
this.oList = this.options.oList;
this.showSum = this.options.showSum;

this.iList = $("#" + this.options.oList + " > li");
this.iListSum = this.iList.length;
this.iListWidth = this.iList.outerWidth(true);
this.moveWidth = this.iListWidth * this.showSum;

this.dividers = Math.ceil(this.iListSum / this.showSum);//共分为多少块
this.moveMaxOffset = (this.dividers - 1) * this.moveWidth;
this.LeftScroll();
this.RightScroll();
};
SellerScroll.prototype = {
SetOptions: function(options){
this.options = {
lButton: "left_scroll",
rButton: "right_scroll",
oList: "carousel_ul",
showSum: 4//一次滚动多少个items
};
$.extend(this.options, options || {});
},
ReturnLeft: function(){
return isNaN(parseInt($("#" + this.oList).css("left"))) ? 0 : parseInt($("#" + this.oList).css("left"));
},
LeftScroll: function(){
if(this.dividers == 1) return;
var _this = this, currentOffset;
$("#" + this.lButton).click(function(){
currentOffset = _this.ReturnLeft();
if(currentOffset == 0){
for(var i = 1; i <= _this.showSum; i++){
$(_this.iList[_this.iListSum - i]).prependTo($("#" + _this.oList));
}
$("#" + _this.oList).css({ left: -_this.moveWidth });
$("#" + _this.oList + ":not(:animated)").animate( { left: "+=" + _this.moveWidth }, { duration: "slow", complete: function(){
for(var j = _this.showSum + 1; j <= _this.iListSum; j++){
$(_this.iList[_this.iListSum - j]).prependTo($("#" + _this.oList));
}
$("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 1) });
} } );
}else{
$("#" + _this.oList + ":not(:animated)").animate( { left: "+=" + _this.moveWidth }, "slow" );
}
});
},
RightScroll: function(){
if(this.dividers == 1) return;
var _this = this, currentOffset;
$("#" + this.rButton).click(function(){
currentOffset = _this.ReturnLeft();
if(Math.abs(currentOffset) >= _this.moveMaxOffset){
for(var i = 0; i < _this.showSum; i++){
$(_this.iList[i]).appendTo($("#" + _this.oList));
}
$("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 2) });

$("#" + _this.oList + ":not(:animated)").animate( { left: "-=" + _this.moveWidth }, { duration: "slow", complete: function(){
for(var j = _this.showSum; j < _this.iListSum; j++){
$(_this.iList[j]).appendTo($("#" + _this.oList));
}
$("#" + _this.oList).css({ left: 0 });
} } );
}else{
$("#" + _this.oList + ":not(:animated)").animate( { left: "-=" + _this.moveWidth }, "slow" );
}
});
}
};
$(document).ready(function(){
var ff = new SellerScroll();
});
//-->
</script>
</head>
<body>
<div class="con">
<div id="carousel_container">
<div id="left_scroll"></div>
<div id="carousel_inner">
<ul id="carousel_ul">
<li><img src="images/wall_s1.jpg" border="0"></li>
<li><img src="images/wall_s2.jpg" border="0"></li>
<li><img src="images/wall_s3.jpg" border="0"></li>
<li><img src="images/wall_s4.jpg" border="0"></li>
<li><img src="images/wall_s5.jpg" border="0"></li>
<li><img src="images/wall_s6.jpg" border="0"></li>
<li><img src="images/wall_s7.jpg" border="0"></li>
<li><img src="images/wall_s1.jpg" border="0"></li>
<li><img src="images/wall_s2.jpg" border="0"></li>
<li><img src="images/wall_s3.jpg" border="0"></li>
<li><img src="images/wall_s4.jpg" border="0"></li>
<li><img src="images/wall_s5.jpg" border="0"></li>
<li><img src="images/wall_s1.jpg" border="0"></li>
<li><img src="images/wall_s2.jpg" border="0"></li>
<li><img src="images/wall_s3.jpg" border="0"></li>
</ul>
</div>
<div id="right_scroll"></div>
</div>
</div>
</body>
</html>