jQuery无缝轮播图

 JQuery代码

function RotationMap(content, btn_r, btn_l, page, width, btn) {
    var content = $("." + content)
    var btn = $("." + btn)
    var index = 1, temp = index
    function img_change(target, index, time) {
        target.css({
            transform: "translateX(" + -width * index + "px)",
            transition: time + "s"
        });
    }
    function autoplay() {
        index++;
        index = Math.abs(index + page) % page;
        if (index == page - 1) {
            index = 0;
            img_change(content, index, 0);
            setTimeout(autoplay, 1);
        }
        else if (index == 0) {
            index = page - 1;
            img_change(content, index, 0);
            index -= 2;
            setTimeout(autoplay, 1);
        }
        else {
            img_change(content, index, 0.3);
        }
        btn.eq(temp - 1).removeClass("selected");
        btn.eq(index - 1).addClass("selected");
        temp = index;
    }
    var btn_r = $("." + btn_r)
    var btn_l = $("." + btn_l)
    var time = setInterval(autoplay, 2000);
    $(content).add(btn_l).add(btn_r).add(btn).hover(function () {
        time = clearInterval(time);
    }, function () {
        if (!time) {
            time = setInterval(autoplay, 2000);
        }
    })
    btn_r.click(autoplay);
    btn_l.click(function () {
        index -= 2;
        autoplay();
    })
    btn.click(function () {
        index = $(this).index();
        autoplay();
    })
}

RotationMap("content", "btn_r", "btn_l", $(".content").children().length, $(".content").children().width(), "btn")
//content为img外盒子 btn_r,btn_l为两侧按钮 page为img个数 width为img大小 btn为外部切换按钮(可不用)

 HTML代码

<div class="container">
    <div class="btn fl selected">...</div>
    <div class="btn fl">...</div>
    <div class="btn fl">...</div>
    <div class="wrapper">
        <div class="btn_r">></div>
        <div class="btn_l"><</div>
        <div class="content">
            <img src="../img/build.jpg" alt="" class="fl">    //与最后一张相同
            <img src="../img/Iceland.jpg" alt="" class="fl">    //第一张
            <img src="../img/huang.jpg" alt="" class="fl">    //第二张
            <img src="../img/build.jpg" alt="" class="fl">    //第三张
            <img src="../img/Iceland.jpg" alt="" class="fl">    //与第一张相同
        </div>
    </div>
</div>

 CSS代码

* {
    padding: 0;
    margin: 0;
    user-select: none;
}

.fl {
    float: left;
}

.container {
    width: 600px;
    height: 800px;
    box-sizing: border-box;
    padding: 50px;
    margin: 0 auto;
}

.wrapper {
    width: 500px;
    height: 382px;
    margin: 100px auto;
    overflow: hidden;
    border-radius: 15px;
    position: relative;
}

.btn{
    user-select: none;
    width: 33%;
    height: 40px;
    padding: 5px 0;
    line-height: 40px;
    text-align: center;
    font-size: 24px;
    color: #000;
}

.btn:hover{
    cursor: pointer;
}

.selected{
    color: #fff;
    background: linear-gradient(orange, red);
}

.content {
    width: 2500px;
    height: 382px;
    transform: translateX(-500px);
}

.content>img {
    width: 500px;
}

.btn_l,
.btn_r {
    font-size: 20px;
    text-align: center;
    width: 70px;
    height: 100%;
    line-height: 382px;
    position: absolute;
    z-index: 1;
}

.btn_l {
    left: 0px;
    color: white;
}

.btn_r {
    right: 0px;
    color: white;
}

.btn_l:hover {
    cursor: pointer;
    background: linear-gradient(to left, transparent, rgba(0, 0, 0, 0.7));
}

.btn_r:hover {
    cursor: pointer;
    background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.7));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值