来,封装个图片左右切换 ,swiper导航滑动

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            text-decoration: none;
        }

        .container {
            position: relative;
            width: 600px;
            height: 400px;
            margin: 100px auto 0 auto;
            box-shadow: 0 0 5px green;
            overflow: hidden;
        }

        .container .wrap {
            position: absolute;
            width: 4200px;
            height: 400px;
            z-index: 1;
            transition: all .5s;
        }

        .container .wrap img {
            float: left;
            width: 600px;
            height: 400px;
        }

        .container .buttons {
            position: absolute;
            right: 5px;
            bottom: 40px;
            width: 150px;
            height: 10px;
            z-index: 2;
        }

        .container .buttons span {
            margin-left: 5px;
            display: inline-block;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: green;
            text-align: center;
            color: white;
            cursor: pointer;
        }

        .container .buttons span.on {
            background-color: red;
        }

        .container .arrow {
            position: absolute;
            top: 35%;
            color: green;
            padding: 0px 14px;
            border-radius: 50%;
            font-size: 50px;
            z-index: 2;
            display: none;
        }

        .container .arrow_left {
            left: 10px;
        }

        .container .arrow_right {
            right: 10px;
        }

        .container:hover .arrow {
            display: block;
        }

        .container .arrow:hover {
            background-color: rgba(0, 0, 0, 0.2);
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="wrap" style="left: -600px;" id="wrap">
            <img src="http://m.beijing.xuefu.com/beijing/img/mcd-index-002xa.png" alt="">
            <img src="http://beijing.xuefu.com/g-img/index-banner/index-mwh-bn4.png" alt="">
            <img src="http://m.exuefu.com/m/g-img/ez-ban.png" alt="">
        </div>
      
        <a class="arrow arrow_left" id="prev">&lt;</a>
        <a class="arrow arrow_right" id="next">&gt;</a>
    </div>
    var imgSlides = {
            el: function (x, y, z, m) {
                //得到要进行切换值的元素和左右切换按钮
                var offsetTarget = document.getElementById(x) || document.getElementsByClassName(x) || document.querySelector(x);
                var prev = document.getElementById(y) || document.getElementsByClassName(y) || document.querySelector(y);
                var next = document.getElementById(z) || document.getElementsByClassName(z) || document.querySelector(z);
                var initOffsetX = parseInt(offsetTarget.style.left);//得到元素初始偏移量 -600
                var offsetTargetChild = offsetTarget.getElementsByTagName(m);
                // 克隆元素/插入元素
                var cloneEl = {
                    firstChild: offsetTarget.childNodes[1].cloneNode(true), //true表示深复制
                    lastChild: offsetTarget.childNodes[wrap.childNodes.length - 2].cloneNode(true),
                    cloneInset: function () {
                        offsetTarget.insertBefore(this.lastChild, offsetTarget.childNodes[0]);
                        offsetTarget.append(this.firstChild);
                        // 执行左右切换事件
                        {
                            next.onclick = function () { nexSlide(); }
                            prev.onclick = function () { prevSlide(); }

                            var offsetXsum = null; //img元素的总宽度
                            for (var i = 0; i < offsetTargetChild.length; i++) {
                                offsetXsum += offsetTargetChild[i].offsetWidth; //将每个img的宽度求和赋值给offsetXsum(总宽度)
                            }

                            function nexSlide() {
                                var newLeft;
                                offsetTarget.style.left === ("-" + (offsetXsum + initOffsetX) + "px") ? (newLeft = initOffsetX) : (newLeft = parseInt(offsetTarget.style.left) + initOffsetX);
                                offsetTarget.style.left = newLeft + "px";
                            }
                            function prevSlide() {
                                var newLeft;
                                offsetTarget.style.left === ("0px" || "" || null || undefined) ? (newLeft = (initOffsetX * (offsetTargetChild.length - 3))) : (newLeft = parseInt(offsetTarget.style.left) - initOffsetX);
                                offsetTarget.style.left = newLeft + "px";
                            }
                        }
                    }
                }
                cloneEl.cloneInset();
            }
        }

        imgSlides.el('wrap', 'prev', 'next', 'img');//调用  只需要传这四个元素即可

            </script>
</body>

</html>


swiper导航点击滑动

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <script src="http://jiaoyu.60design.cn:8081/pZ/js/jquery.1.8.3min.js"></script>
    <script src="http://m.yytvip.com.cn/g-js/swiper4.js"></script>
    <link rel="stylesheet" href="http://m.beijing.xuefu.com/beijing/css/swiper4.3.5.min.css">
    <title>Document</title>
<style type="text/css">
.swiper-container {
        width: 100%;
        height: 300px;
        margin: 50px auto;
    }
    .swiper-slide {
        background: #f1f1f1;
        color:#000;
        text-align: center;
        line-height: 40px;
        height: 40px;
        width:100px;
    }</style>
</head>
<body>
<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide" >Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
</div>
<script>
        var swiper = new Swiper('.swiper-container', {
            spaceBetween: 50,
            slidesPerView: 2,
            centeredSlides: true,
            slideToClickedSlide: true,
            grabCursor: true,
            history: true
        });
        
        
    2.
    给导航添加class
      function setCurrentSlide(ele, index) {
        var els = document.getElementsByClassName('swiper-slide');
        for (var i = 0; i < els.length; i++) {
            els[i].classList.remove('active')
        }
        ele.classList.add("active");
    }
  var swiper1 = new Swiper('.list .swiper-container', {
                slidesPerView: 'auto',
                paginationClickable: true,
                spaceBetween: 0,
                freeMode: true,
                loop: false,
                onTab: function(swiper) {
                    var n = swiper1.clickedIndex;
                }
            });
            swiper1.slides.each(function(index, val) {
                var ele = this;
                ele.onclick = function() {
                    setCurrentSlide(ele, index);
                    swiper1.slideTo(index);
                }
            });

</script>
</body>
</html>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值