swiper嵌套的swiper怎样实现鼠标滚动切换,当嵌套的切换完之后,在继续切换外面的swiper

图片描述

<!DOCTYPE html>
<!-- saved from url=(0044)http://www.swiper.com.cn/demo/11-nested.html -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <title>Swiper demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">

    <!-- Link Swiper's CSS -->
    <link rel="stylesheet" href="css/swiper.min.css">

    <!-- Demo styles -->
    <style>
    html, body {
        position: relative;
        height: 100%;
    }
    body {
        background: #eee;
        font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
        font-size: 14px;
        color:#000;
        margin: 0;
        padding: 0;
    }
    .swiper-container {
        width: 100%;
        height: 100%;
    }
    .swiper-slide {
        text-align: center;
        font-size: 18px;
        background: #fff;

        /* Center slide text vertically */
        display: -webkit-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;
    }
    .swiper-container-v {
        background: #eee;
    }
    </style>
</head>
<body>
    <!-- Swiper -->
    <div class="swiper-container swiper-container-h swiper-container-horizontal">
        <div class="swiper-wrapper" style="transform: translate3d(-1400px, 0px, 0px); transition-duration: 0ms;">
            <div class="swiper-slide swiper-slide-prev" style="width: 1350px; margin-right: 50px;">Horizontal Slide 1</div>
            <div class="swiper-slide swiper-slide-active" style="width: 1350px; margin-right: 50px;">
                <div class="swiper-container swiper-container-v swiper-container-vertical">
                    <div class="swiper-wrapper" style="transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;">
                        <div class="swiper-slide swiper-slide-active" style="height: 312px; margin-bottom: 50px;">Vertical Slide 1</div>
                        <div class="swiper-slide swiper-slide-next" style="height: 312px; margin-bottom: 50px;">Vertical Slide 2</div>
                        <div class="swiper-slide" style="height: 312px; margin-bottom: 50px;">Vertical Slide 3</div>
                        <div class="swiper-slide" style="height: 312px; margin-bottom: 50px;">Vertical Slide 4</div>
                        <div class="swiper-slide" style="height: 312px; margin-bottom: 50px;">Vertical Slide 5</div>
                    </div>
                    <div class="swiper-pagination swiper-pagination-v swiper-pagination-clickable swiper-pagination-bullets"><span class="swiper-pagination-bullet swiper-pagination-bullet-active"></span><span class="swiper-pagination-bullet"></span><span class="swiper-pagination-bullet"></span><span class="swiper-pagination-bullet"></span><span class="swiper-pagination-bullet"></span></div>
                </div>
            </div>
            <div class="swiper-slide swiper-slide-next" style="width: 1350px; margin-right: 50px;">Horizontal Slide 3</div>
            <div class="swiper-slide" style="width: 1350px; margin-right: 50px;">Horizontal Slide 4</div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination swiper-pagination-h swiper-pagination-clickable swiper-pagination-bullets"><span class="swiper-pagination-bullet"></span><span class="swiper-pagination-bullet swiper-pagination-bullet-active"></span><span class="swiper-pagination-bullet"></span><span class="swiper-pagination-bullet"></span></div>
    </div>

    <!-- Swiper JS -->
    <script src="js/swiper.min.js"></script>

    <!-- Initialize Swiper -->
    <script>
    var swiperH = new Swiper('.swiper-container-h', {
        pagination: '.swiper-pagination-h',
        paginationClickable: true,
        mousewheelControl : true,
        spaceBetween: 50
    });
    var swiperV = new Swiper('.swiper-container-v', {
        pagination: '.swiper-pagination-v',
        paginationClickable: true,
        direction: 'vertical',
        mousewheelControl : true,
        mousewheelReleaseOnEdges : true,
        spaceBetween: 50
    });
    </script>

</body></html>
阅读 13.4k
2 个回答
var swiperH = new Swiper('.swiper-container-h', {
    pagination: '.swiper-pagination-h',
    paginationClickable: true,
    spaceBetween: 50,

});
var swiperV = new Swiper('.swiper-container-v', {
    pagination: '.swiper-pagination-v',
    paginationClickable: true,
    nested:true,//阻止父级的切换
    //direction: 'vertical',
    spaceBetween: 50,
});

这个问题官方已经想到了:
swiper官方api-配置选项-nested

=======================2017.09.27 12:27更新======================
抱歉,上面的代码滚轮事件是无效的,仅支持touch事件,需要对滚轮事件特殊处理:

var swiperH = new Swiper('.swiper-container-h', {
    pagination: '.swiper-pagination-h',
    paginationClickable: true,
    mousewheelControl: true,
    direction: 'vertical',
    spaceBetween: 50,
    onSlideChangeStart:function(swiper){//滑动父级需要激活滚轮事件
        swiper.enableMousewheelControl();
    }
});
var swiperV = new Swiper('.swiper-container-v', {
    pagination: '.swiper-pagination-v',
    paginationClickable: true,
    mousewheelControl: true,
    nested: true,//阻止父级的切换(该属性只能禁止touch事件,如果不需要滑动阻止,可以注释掉,滚轮事件依然生效)
    //direction: 'vertical',
    spaceBetween: 50,
    onSlideChangeStart: function (swiper) {
        if (swiper.activeIndex > 0 && swiper.activeIndex < swiper.bullets.length - 1) {
            swiperH.disableMousewheelControl();
        }
    },
    onSlideChangeEnd: function (swiper) {
        if (!(swiper.activeIndex > 0 && swiper.activeIndex < swiper.bullets.length - 1)) {
            swiperH.enableMousewheelControl();
        }
    }
});

代码详见:http://runjs.cn/detail/0aolyclz

官方的是滑动的吧,我想实现滚动事件。如何操作呢?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏