亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用swiper自定義分頁點擊跳轉指定頁面

發布時間:2023-04-15 16:58:53 來源:億速云 閱讀:228 作者:iii 欄目:開發技術

這篇文章主要介紹“怎么使用swiper自定義分頁點擊跳轉指定頁面”,在日常操作中,相信很多人在怎么使用swiper自定義分頁點擊跳轉指定頁面問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用swiper自定義分頁點擊跳轉指定頁面”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

    swiper自定義分頁點擊跳轉指定頁面

    mySwiper.slideTo(index, speed, runCallbacks),控制Swiper切換到指定slide。

    參數名類型是否必填描述
    indexnum必選指定將要切換到的slide的索引
    speednum可選切換速度(單位ms)
    runCallbacksboolean可選設置為false時不會觸發transition回調函數

    代碼如下:

    <!--banner開始-->
    <div class="banner">
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <div class="swiper-slide">
                    <img src="Static/Images/banner_1.jpg" alt="banner">
                </div>
                <div class="swiper-slide">
                    <img src="Static/Images/banner_1.jpg" alt="banner">
                </div>
            </div>
            <div class="swiper-button-prev"></div><!--左箭頭。如果放置在swiper-container外面,需要自定義樣式。-->
            <div class="swiper-button-next"></div><!--右箭頭。如果放置在swiper-container外面,需要自定義樣式。-->
            <!--分頁器 -->
            <div class="swiper-pagination"></div>
        </div>
    </div>
    <script>
        var mySwiper = new Swiper('.swiper-container', {
            autoplay: true,//可選選項,自動滑動
            loop: true, // 循環模式選項,true 循環播放
            observer: true,//實時檢測,動態更新
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },//前進后退箭頭
            pagination: {//自定義分頁
                el: '.swiper-pagination',
                type: 'custom',
                autoplayDisableOnInteraction: false,
                renderCustom: function (swiper, current, total) {
                    var paginationHtml = " ";
                    for (var i = 0; i < total; i++) {
                        // 判斷是不是激活焦點,是的話添加active類,不是就只添加基本樣式類
                        if (i === (current - 1)) {
                            paginationHtml += '<span class="swiper-pagination-customs swiper-pagination-customs-active"><p class="swiper-pagination-li"></p></span>';
                        } else {
                            paginationHtml += '<span class="swiper-pagination-customs"><p class="swiper-pagination-li"></p></span>';
                        }
                    }
                    return paginationHtml;
                },
            },
        });
        $('.swiper-pagination').on('click','span',function(){
            var index = $(this).index()+1 ;
            mySwiper.slideTo(index, 1000, false)//切換到對應的slide,速度為1秒
    
        });
    
    
    </script>
    <!--banner結束-->
    /*banner*/
    .banner {
        position: relative;
    }
    
    .swiper-container {
        margin-left: auto;
        margin-right: auto;
        position: relative;
        overflow: hidden;
        list-style: none;
        padding: 0;
        z-index: 1;
    }
    
    .swiper-button-next, .swiper-button-prev {
        position: absolute;
        top: 50%;
        width: 32px;
        height: 32px;
        margin-top: -22px;
        z-index: 10;
        cursor: pointer;
        -moz-background-size: 27px 44px;
        -webkit-background-size: 27px 44px;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat
    }
    
    .swiper-button-next {
        background-image: url("../Images/banner_right.png");
        right: 10px;
    
    }
    
    .swiper-button-prev {
        background-image: url("../Images/banner_left.png");
        left: 10px;
    }
    
    .swiper-button-next.swiper-button-disabled, .swiper-button-prev.swiper-button-disabled {
        opacity: .35;
        cursor: auto;
        pointer-events: none
    }
    
    .swiper-wrapper {
        position: relative;
        width: 100%;
        height: 100%;
        z-index: 1;
        display: flex;
        transition-property: transform;
        box-sizing: content-box
    }
    
    .swiper-slide {
        flex-shrink: 0;
        width: 100%;
        height: 100%;
        position: relative;
        transition-property: transform
    }
    
    .swiper-slide img {
        width: 100%;
    }
    
    .swiper-pagination {
        position: absolute;
        text-align: center;
        transition: .3s opacity;
        transform: translate3d(0, 0, 0);
        z-index: 10
    }
    
    .swiper-pagination-custom {
        bottom: 12%;
        left: 0;
        width: 100%;
        height: 20px;
        text-align: center;
    }
    
    .swiper-pagination-li {
        width: 6px;
        height: 6px;
        background-color: #fff;
        position: absolute;
        top: 6px;
        left: 6px;
        border-radius: 50%;
    }
    
    .swiper-pagination-customs {
        width: 18px;
        height: 18px;
        display: inline-block;
        cursor: pointer;
        background: none;
        opacity: 1;
        border-radius: 50%;
        margin: 0 5px;
        outline: 0;
        position: relative;
    }
    
    .swiper-pagination-customs-active {
        opacity: 1;
        border: 1px solid #fff;
        background: none;
    }
    
    .banner-container {
        position: absolute;
        z-index: 999;
        top: 25%;
        left: 25%;
        width: 50%;
        height: 50%;
        text-align: center;
        color: #fff;
    }
    
    .banner-container img {
        width: 80px;
        height: auto;
        display: table-cell;
        margin: 0 auto;
    }
    
    .banner-container .big-title {
        font-size: 44px;
        text-transform: uppercase;
        font-weight: 700;
        margin-top: 16px;
    }
    
    .banner-container .small-title {
        font-size: 20px;
        letter-spacing: 5px;
        margin: 14px 0;
    }
    
    .banner-btn {
        display: flex;
        justify-content: space-around;
        width: 45%;
        margin: 0 auto;
        margin-top: 30px;
    }
    
    .banner-btn .btn {
        width: 120px;
        height: 36px;
        border: 1px solid #fff;
        line-height: 36px;
        border-radius: 36px;
        font-size: 14px;
        transition: all 0.5s;
    }
    
    .banner-btn .btn:hover {
        width: 120px;
        height: 36px;
        border: 1px solid #fff;
        line-height: 36px;
        border-radius: 36px;
        font-size: 14px;
        color: #000000;
        background: #fff;
        font-weight: 600;
        cursor: pointer;
    }
    
    /*banner*/

    swiper自定義分頁器

    html部分

    <div class="swiper-container">
         <div class="swiper-wrapper">
            <div class="swiper-slide">
                <img src="">
            </div>
         </div>
         <!-- 如果需要分頁器 -->
         <div class="swiper-pagination"></div>
    </div>

    js部分

    <script type="text/javascript" src="js/swiper-bundle.min.js"> </script>
     
    var mySwiper = new Swiper(".swiper-container", {
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
            type:'custom',   //自定義分頁器
            renderCustom: function (swiper, current, total) {  
                var paginationHtml = " ";
                for (var i = 0; i < total; i++) {
                     // 判斷是不是激活焦點,是的話添加active類,不是就只添加基本樣式類
                     if (i === (current - 1)) {
                          paginationHtml += '<span class="swiper-pagination-customs swiper-pagination-customs-active" ></span>';                                                                                                 
                    }else{
                         paginationHtml += '<span class="swiper-pagination-customs" ></span>';
                    }                      
                }
                return paginationHtml;
            },
        }
    });
                       
    //點擊分頁器跳轉到對應頁面
    $(".swiper-pagination").on("click","span",function(){
            var index = $(this).index();
            mySwiper.slideTo(index);
    })

    css部分

    .swiper-pagination-custom {
        height: 34px;
        text-align: end !important;  //這里設置分頁器的位置 放在行的末尾
    }
    /*自定義分頁器的樣式*/
    .swiper-pagination-customs {
        width: 34px;
        height: 34px;
        display:inline-block;
        border-radius: 5px;
        margin: 0 3px;
        outline: 0;
        box-sizing: border-box;
    }
    .swiper-pagination-customs:last-child{
        margin-right: 16px;
    }
    /*自定義分頁器激活時的樣式表現*/
    .swiper-pagination-customs-active {
        border: 2px solid #fcb916;
        width: 36px;
        height: 36px;
    }

    解決動態加載數據滑動失效的問題

    1. 在swiper初始化加兩行代碼

    var mySwiper = new Swiper('.swiper-container', { 
     
     observer:true,//修改swiper自己或子元素時,自動初始化swiper 
     observeParents:true//修改swiper的父元素時,自動初始化swiper 
     
    });

    2.在數據請求后初始化swiper

    function getMess(){
        globalParams = {
            //發送請求的參數
        }
        api.post2("xxx/xxx/xxx", globalParams, function(res) {  //ajax請求
            var list = res.data.list;
            list.forEach((item) => {
                    var itm = item.formModel.cgformFieldList
                    var imgMess = itm[10].propertyLabel.split(",")
                    var msg = ""      // 輪播詳情
                    imgMess.forEach((item) => {
                        msg += `
                            <div class="swiper-slide">
                                <img src="https://qiniu.hollysmart.com.cn/${item}">
                            </div>`
                        $(".swiper-wrapper").html(msg);//動態加載輪播項
     
                          //初始化輪播組件
                        var mySwiper = new Swiper(".swiper-container", {
                            pagination: {
                                el: '.swiper-pagination',
                                clickable: true,
                                type:'custom',
                                renderCustom: function (swiper, current, total) {
                                    var paginationHtml = " ";
                                    for (var i = 0; i < total; i++) {
                                        // 判斷是不是激活焦點,是的話添加active類,不是就只添加基本樣式類
                                        //要求是分頁器為縮小的輪播圖片 將圖片插入到元素中
                                        if (i === (current - 1)) {
                                            paginationHtml += 
                                            '<span class="swiper-pagination-customs swiper-pagination-customs-active" >' + `<img src="https://xxx.com.cn/${imgMess[i]}">` + '</span>';
                                        }else{
                                            paginationHtml += '<span class="swiper-pagination-customs" >'+  `<img src="https://xxx.com.cn/${imgMess[i]}">` +'</span>';
                                        }                      
                                    }
                                    return paginationHtml;
                                },
                            }
                        });
                       
                        //點擊分頁器跳轉到對應頁面
                        $(".swiper-pagination").on("click","span",function(){
                            var index = $(this).index();
                            mySwiper.slideTo(index);
                        })
                        
                    })
            })
        })
    }

    到此,關于“怎么使用swiper自定義分頁點擊跳轉指定頁面”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    福清市| 英德市| 宽城| 天全县| 衡山县| 南涧| 旬阳县| 勐海县| 海盐县| 阿拉善盟| 忻州市| 会宁县| 体育| 北海市| 扬中市| 绥棱县| 武威市| 得荣县| 自贡市| 平远县| 上饶市| 镇江市| 三河市| 泽库县| 湖南省| 永修县| 泊头市| 景洪市| 青冈县| 广汉市| 怀安县| 罗甸县| 安泽县| 仁怀市| 定襄县| 方城县| 同德县| 盐山县| 南安市| 浦北县| 渝中区|