jquery 鼠标控制并带有数字的图片循环滚动代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>基于jQuery的控制左右滚动及手动点击滚动演示效果_非凡图库</title>
    <meta name="keywords" content="控制左右,左右滚动,手动点击,点击滚动" />
    <meta name="description" content="非凡图库脚本频道提供完全免费焦点图,选项卡,图片相册,菜单导航,广告代码,文本链接,提示工具,弹出层对话框,时间日历,星级评价,统计图表,表格表单,滚动代码,其它代码,拾色器,按钮图标,是中国最新最全的脚本代码免费下载网站。" />
    <link type="text/css" rel="Stylesheet" href="http://www.ffpic.com/images/bar.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<style>
/*demo css*/
.ibox{width:850px; height:210px; background:#ddd; margin:1em auto; border:1em solid #ddd; position:relative; overflow:hidden}
.ibox .showbox{position:absolute;left:0;width:2550px; height:170px; background:#fff}
.ibox .showbox ul li{float:left;margin:7px}
.ibox .showbox ul li img{width:150px; height:150px; border:3px solid #a40000}
.ibox span{position:absolute;padding:2px 5px; background:#fff; cursor:pointer;}
.ibox span.pre{left:10px; bottom:0}
.ibox span.next{right:10px; bottom:0}
.ibox span.num{right:45%; bottom:0; background:#ddd;line-height:18px;}
.ibox span.num li{display:inline;margin:0 5px; padding:1px 5px; line-height:18px}
.ibox span.num .numcur{background:#a40000; color:#fff}
</style>
<script type="text/javascript" src="a.js"></script>
<script>
$(document).ready(function(){
          $.focus("#ibox","#showbox","5");
        $.focus("#ibox1","#showbox1","5");
    });

</script>
</head>
<body>
   

    <div id="content">
    
<br><br>
<!--DEMO start-->
<div id="ibox" class="ibox">
    <div id="showbox" class="showbox">
        <ul>
            <li><img src="images/f8a9dd53720bf31b5917915e8222c27370a3681c_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/15a1d7982c49b0e638ae3340cf86dc4469f955cc_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/68e9a7a8476e26a1022fa5e39ccdda669a5d4f08_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/56d1b9aaff5b424f1cdb08b80544d5150839b4de_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/56d1b9aaff5b424f1cdb08b80544d5150839b4de_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/20417dfc945bae6c720a660625e9652a154c2c94_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/f4a9f29299f3dba6e7af0ac3c5aff93e01f874d3_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/ca6f0c9ac0c6e3bfce827cd92833e1db620b6377_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/809fcfbb8a1f58d1d47a760fd4283556cce201ef_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/15a1d7982c49b0e638ae3340cf86dc4469f955cc_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/56d1b9aaff5b424f1cdb08b80544d5150839b4de_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/15a1d7982c49b0e638ae3340cf86dc4469f955cc_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/49d6ae8901500f473a2e3e58e5b5ddc0e788bc9d_m.jpg" alt="demo" title="demo" /></li>
        </ul>
    </div>
    <span class="pre">&laquo;向左滚动</span><span class="next">向右滚动&raquo;</span><span class="num"><ol><li class="numcur">1</li><li>2</li><li>3</li></ol></span>
</div>
<!--DEMO end-->

<div style="clear:both; overflow:hidden; height:150px;"></div>

<!--DEMO start-->
<div id="ibox1" class="ibox">
    <div id="showbox1" class="showbox">
        <ul>
            <li><img src="images/f8a9dd53720bf31b5917915e8222c27370a3681c_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/15a1d7982c49b0e638ae3340cf86dc4469f955cc_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/68e9a7a8476e26a1022fa5e39ccdda669a5d4f08_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/56d1b9aaff5b424f1cdb08b80544d5150839b4de_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/56d1b9aaff5b424f1cdb08b80544d5150839b4de_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/20417dfc945bae6c720a660625e9652a154c2c94_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/f4a9f29299f3dba6e7af0ac3c5aff93e01f874d3_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/ca6f0c9ac0c6e3bfce827cd92833e1db620b6377_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/809fcfbb8a1f58d1d47a760fd4283556cce201ef_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/15a1d7982c49b0e638ae3340cf86dc4469f955cc_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/56d1b9aaff5b424f1cdb08b80544d5150839b4de_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/15a1d7982c49b0e638ae3340cf86dc4469f955cc_m.jpg" alt="demo" title="demo" /></li>
            <li><img src="images/49d6ae8901500f473a2e3e58e5b5ddc0e788bc9d_m.jpg" alt="demo" title="demo" /></li>
        </ul>
    </div>
    <span class="pre">&laquo;向左滚动</span><span class="next">向右滚动&raquo;</span><span class="num"><ol><li class="numcur">1</li><li>2</li><li>3</li></ol></span>
</div>
<!--DEMO end-->


    </div>


</body>
</html>

 

网上找了好多相关的。但在项目中基本都不符合要求。后来找了一个。但是他不是封装的。自己修改了一下。可以重复使用了

原作者:http://mrthink.net/

 

代码如下:

$(function(){
	jQuery.focus = function(bigid,smallid,boxnum) {
    //@Mr.Think***变量
    var $cur = 1;//初始化显示的版面
    //var $i = 5;//每版显示数
	var $i = boxnum;
    var $len = $(smallid+">ul>li").length;//计算列表总长度(个数)
	
    var $pages = Math.ceil($len / $i);//计算展示版面数量
    var $w = $(bigid).width();//取得展示区外围宽度
    var $showbox = $(smallid);
    var $num = $(bigid+" span.num li")
    var $pre = $(bigid+" span.pre")
    var $next = $(bigid+" span.next");
 	//@Mr.Think***向前滚动
    $pre.click(function(){
        if (!$showbox.is(':animated')) {  //判断展示区是否动画
            if ($cur == 1) {   //在第一个版面时,再向前滚动到最后一个版面
                $showbox.animate({
                    left: '-=' + $w * ($pages - 1)
                }, 500); //改变left值,切换显示版面,500(ms)为滚动时间,下同
                $cur = $pages; //初始化版面为最后一个版面
            }
            else { 
                $showbox.animate({
                    left: '+=' + $w
                }, 500); //改变left值,切换显示版面
                $cur--; //版面累减
            }
            $num.eq($cur - 1).addClass('numcur').siblings().removeClass('numcur'); //为对应的版面数字加上高亮样式,并移除同级元素的高亮样式
        }
    });
    //@Mr.Think***向后滚动
    $next.click(function(){
        if (!$showbox.is(':animated')) { //判断展示区是否动画
            if ($cur == $pages) {  //在最后一个版面时,再向后滚动到第一个版面
                $showbox.animate({
                    left: 0
                }, 500); //改变left值,切换显示版面,500(ms)为滚动时间,下同
                $cur = 1; //初始化版面为第一个版面
            }
            else {
                $showbox.animate({
                    left: '-=' + $w
                }, 500);//改变left值,切换显示版面
                $cur++; //版面数累加
            }
            $num.eq($cur - 1).addClass('numcur').siblings().removeClass('numcur'); //为对应的版面数字加上高亮样式,并移除同级元素的高亮样式
        }
    });
    //@Mr.Think***数字点击事件
    $num.click(function(){
        if (!$showbox.is(':animated')) { //判断展示区是否动画
            var $index = $num.index(this); //索引出当前点击在列表中的位置值
            $showbox.animate({
                left: '-' + ($w * $index) 
            }, 500); //改变left值,切换显示版面,500(ms)为滚动时间
            $cur = $index + 1; //初始化版面值,这一句可避免当滚动到第三版时,点击向后按钮,出面空白版.index()取值是从0开始的,故加1
            $(this).addClass('numcur').siblings().removeClass('numcur'); //为当前点击加上高亮样式,并移除同级元素的高亮样式
        }
    })

	};
});

 

根据ID来加载相应的滚动区域。这里设置了。外框和内框两个部分,因为我前台页面中同样的结构。使用起来方便一点

 

 

posted @ 2012-07-09 10:23  黑武飞翔  阅读(3512)  评论(0编辑  收藏  举报