JS轮播图

1:html的代码

<div id="slideshow">
    <div class="swiper" id="swiper">
        <div class="swiper-time b"></div>
        <div class="swiper-time a"></div>
        <div class="swiper-time dd"></div>
        <div class="swiper-time dd"></div>
        <div class="swiper-time dd"></div>
        <div class="swiper-time dd"></div>
        <div class="swiper-time dd"></div>
        <div class="swiper-time dd"></div>
        <div class="swiper-time c"></div>
    </div>
</div>

 2:然后是css

#slideshow{
            width: 620px;
            height: 300px;
            float: left;
            margin-top: 20px;
            overflow: hidden;
            background-color: rgb(252, 252, 252);
        }
        .swiper, #swiper {
            width: 620px;
            height: 300px;
            position: relative;
        }
        .swiper div {
            display: block;
            position: absolute;
            width: 295px;
            height: 170px;
            overflow: hidden;
            left: 164px;
            top: 0;
            transition: 0.5s;
            color: #fff;
            font-size: 50px;
            text-align: center;
            line-height: 200px;
        }
        .swiper div:nth-child(1) {
            background-image: url("../image/w_1.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(2) {
            background-image: url("../image/w_2.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(3) {
            background-image: url("../image/w_3.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(4) {
            background-image: url("../image/w_4.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(5) {
            background-image: url("../image/w_5.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(6) {
            background-image: url("../image/w_6.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(7) {
            background-image: url("../image/w_7.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(8) {
            background-image: url("../image/w_8.jpeg");
            background-size: cover;
        }
        .swiper div:nth-child(9) {
            background-image: url("../image/w_9.jpeg");
            background-size: cover;
        }
        .swiper .a {
            opacity: 1;
            z-index: 23;
            -webkit-transform: translateX(255px) translateZ(-300px) rotateY(-45deg);
            -ms-transform: translateX(255px) translateZ(-300px) rotateY(-45deg);
            transform: perspective(500px) translateX(300px) translateZ(-253px) rotateY(-45deg);
            -webkit-box-reflect: below 10px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(30%, transparent), to(rgba(250, 250, 250, 0.3)));
        }
        .swiper .b {
            opacity: 1;
            z-index: 33;
            -webkit-box-reflect: below 10px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(30%, transparent), to(rgba(250, 250, 250, 0.3)));
            transform: translateX(0) translateZ(-100px) rotateY(0deg)
        }
        .swiper .c {
            opacity: 1;
            z-index: 23;
            -webkit-box-reflect: below 10px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(30%, transparent), to(rgba(250, 250, 250, 0.3)));
            -webkit-transform: translateX(255px) translateZ(-300px) rotateY(-45deg);
            -ms-transform: translateX(255px) translateZ(-300px) rotateY(-45deg);
            transform: perspective(500px) translateX(-300px) translateZ(-253px) rotateY(45deg);
        }
        .swiper .dd {
            opacity: 0;
            z-index: -1;
            -webkit-transform: translateX(0) translateZ(-300px) rotateY(0);
            -ms-transform: translateX(0) translateZ(-300px) rotateY(0);
            transform: perspective(500px) translateX(0) translateZ(-253px) rotateY(0);
        }

3:最后就是js的代码了

function slideshow() {
        const time = 3000;  //自动播放速度
        var index = 0  // 索引
        const swiperitem = document.getElementById('swiper') //获取父元素
        const swiper = swiperitem.getElementsByTagName('div') //获取合集
        //    自动轮播
        var setTime = setInterval(() => {
            if (index < swiper.length - 1) {
                index++
            } else {
                index = 0
            }
            style()
        }, time)
        // 点解切换
        for (let i = 0; i < swiper.length; i++) {
            swiper[i].onclick = function () {
                if (i === index) return
                index = i
                style()
            }
        }
        // 鼠标移入暂停
        swiperitem.onmouseover = function () {
            clearInterval(setTime)
        }
        // 鼠标移出继续轮播
        swiperitem.onmouseout = function () {
            setTime = setInterval(() => {
                if (index < swiper.length - 1) {
                    index++
                } else {
                    index = 0
                }
                style()
            }, time)
        }

        // 滚动事件
        function style() {
            for (let i = 0; i < swiper.length; i++) {
                swiper[i].className = 'swiper-time dd'
            }
            if (index === swiper.length - 1) {
                swiper[index].className = 'swiper-time b'
                swiper[0].className = 'swiper-time a'
                swiper[index - 1].className = 'swiper-time c'
            } else if (index === 0) {
                swiper[index].className = 'swiper-time b'
                swiper[index + 1].className = 'swiper-time a'
                swiper[swiper.length - 1].className = 'swiper-time c'
            } else {
                swiper[index].className = 'swiper-time b'
                swiper[index + 1].className = 'swiper-time a'
                swiper[index - 1].className = 'swiper-time c'
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值