JQuery banner轮播

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-1.8.0.js"></script>
    <style>
        *{ margin: 0; padding: 0;}
        .banner{width: 1080px; height: 432px; position: relative; overflow: hidden; margin: 50px auto;}
        .box{ width: 4320px; height: 432px; }
        .box div{ float: left;}
        .but div{ position: absolute; top: 200px; cursor: pointer; padding: 5px 15px; font-size: 25px; background-color: rgba(102,215,255,0.5); color: white; z-index: 2;}
        .next{ right: 50px;}
        .prev{ left: 50px;}
        .Dots{ position: absolute; bottom: 30px; right: 50px;}
        .Dots a{ padding: 1px 9px; background-color: rgba(102,215,255,0.7); margin-left: 8px; border-radius: 50%;}
        .Dots a.active{ background-color: white;}
    </style>
</head>
<body>
    <div class="banner">
        <!--img-->
        <div class="box">
            <div><img src="img/1.jpg"></div>
            <div><img src="img/2.jpg"></div>
            <div><img src="img/1.jpg"></div>
            <div><img src="img/2.jpg"></div>
        </div>
        <!--Dots-->
        <div class="Dots"></div>
        <!--next-prev-->
        <div class="but">
            <div class="prev">Prev</div>
            <div class="next">Next</div>
        </div>
    </div>
<script>
    var index = 0;
    var Numn = "";

    //获取图片的个数
    var imgNum = $(".box").find('img').length;

    //获取每张图片的宽度
    var imgWidth = $(".box").find('img').width();

    //计算所有图片的宽度
    var AllImgWidth = imgNum*imgWidth;
//        alert(AllImgWidth);

    function selectimg(index) {
        $(".box").animate({"margin-left":"-" + imgWidth*index + "px"},600);
        $(".Dots a").eq(index).addClass("active").siblings('a').removeClass('active');

    }

    //点击prev的按钮
    $(".but .prev").click(function(){
       index -= 1;
        if(index < 0){
            index = imgNum - 1;
        }
        selectimg(index);
    });

    //点击next按钮
    $(".but .next").click(function(){
       index += 1;
        if( index > 3){
            index = 0;
        }
        selectimg(index);
    });

    //鼠标放上去暂停轮播
    $('.banner').hover(function(){
        clearInterval(p);
    },function () {
        pic();
    });

    //自动轮播
    // 1、轮播切换图片函数,不断的改变index的值
    // 2、然后乘以宽度(总宽度)
    function pic(){

        p = setInterval(function(){
            index++;
            if(index >= imgNum){
                index = 0;
            }
            selectimg(index);
        },5000);

    }

    //获取按钮的个数;向页面添加按钮
    for(var icon = 0; icon < imgNum; icon++){
        Numn +="<a href='javascript:;'></a>";
    }
    $('.Dots').html(Numn);
    $('.Dots a').eq(0).addClass('active');

    //点击.Dots a切换到对应的图
    $(".Dots a").each(function(index){
        $(this).click(function(){
            $('.box').animate({'margin-left':'-' + imgWidth*index + 'px'},600);
            $(".Dots a").eq(index).addClass("active").siblings("a").removeClass("active");
        })
    });

    pic();
</script>
</body>
</html>


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值