使用swiper自定义分页点击跳转指定页面

目录
  • swiper自定义分页点击跳转指定页面
  • swiper自定义分页器
    • 解决动态加载数据滑动失效的问题
  • 总结

swiper自定义分页点击跳转指定页面

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

参数名 类型 是否必填 描述
index num 必选 指定将要切换到的slide的索引
speed num 可选 切换速度(单位ms)
runCallbacks boolean 可选 设置为false时不会触发transition回调函数

(更多方法见Swiper官网)

效果图如下:

<!--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);
                    })
                    
                })
        })
    })
}

记录下jquery的使用方法 ,方便后续的查看

之后运用到vue时再继续写

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Swiper自定义分页器使用详解

    Swiper自定义分页,供大家参考,具体内容如下 最终实现效果图: HTML DEMO(官网例子) <link rel="stylesheet" href="path/to/swiper.min.css"> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide&

  • 移动端触摸滑动插件swiper使用方法详解

    Swiper是移动端的一款非常强大的触摸滑动插件,下面代码只展示一些常用的配置,具体可以查看官网api <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="swiper.min.cs

  • uni-app使用swiper实现轮播图的方法

    目录 uni-app轮播图实现之swiper 补充:uniapp swiper 自定义轮播图指示点 总结 uni-app轮播图实现之swiper 首先在data中定义一个图片数据的对象数组 data() { return { rotation: [ { id: 1, url: 'https://imgcps.jd.com/ling4/100035927374/5Lqs6YCJ5aW96LSn/5L2g5YC85b6X5oul5pyJ/p-5bd8253082acdd181d02fa42/60f0

  • vue 点击按钮 路由跳转指定页面的实现方式

    目录 点击按钮 路由跳转指定页面 最终效果 vue跳转页面常用的方式 1:router-link跳转 2:this.$router.push() 3.this.$router.replace() 4.this.$router.go(n) ps : 区别 点击按钮 路由跳转指定页面 最终效果 点击指定按钮,跳转指定 /login 页面 代码: <button @click="gotolink" class="btn btn-success">点击跳转页面&

  • Vue登录拦截 登录后继续跳转指定页面的操作

    在开发中我们经常遇到这样的需求,需要用户登录后才可以访问该页面,如果用户没有登录点击该页面时则自动跳转到登录页面,登录后又跳转到链接的页面而不是首页,这种问题该如何去做呢? 1.在路由器router下的 index.js 的配置中,给需要拦截登录的页面的路由上加一个meta: {loginRequest: true} ,其中loginRequest 变量自己可以随意定义 2.在main.js文件里面添加beforeEach钩子函数 解释: router.beforeEach((to, from,

  • PHP使用Apache的伪静态功能实现“网页404时跳转指定页面

    需求: 1.例如我之前的网站域名是"www.jb51.net",有一个文章的链接是"www.jb51.net/article-5-1.html" 2.因为业务调整或其他原因,更改了域名和网站结构,域名变更为"www.jb51xxxx.net",那么别人访问"www.jb51.net/article-5-1.html"这个文章链接时就访问不到了.出现如下404情况: 解决方案:  1.在网站根目录下新建一个.htaccess伪静

  • asp.net网站首页根据IP自动跳转指定页面的示例

    对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有城市站点的功能,如果没有设置相应的城市站点,默认就是根据用户访问的IP地址的所在城市自动设置.本文主要通过自定义扩展IHttpModule接口,考虑到性能IP数据库主要采用QQwry纯真IP数据库,主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能(支持Nginx作为前端反向代理服务器),该WebsiteSkip组件核心代码如下: 复制代码 代码如下:

  • Android实现外部唤起应用跳转指定页面的方法

    前言 通常有这么一个场景,就是分享内容到微信朋友圈等,然后点击内容中的某个按钮就可以唤起自家应用. 这里要讲的也是使用 scheme 的方式去实现跳转,先捋一捋思路,首先如果要外部能唤醒 App ,那么 App 肯定要先注册一个全局的事件监听吧.然后,应该有一个页面来处理接受事件然后解析出具体的参数然后跳转具体的页面.就是这么简单. 思路捋好来,那么就来一一实现吧. 注册事件监听 这里需要使用到 Android Activity中的 <intent-filter> ,现在可以创建一个解析跳转的

  • swiper自定义分页器的样式

    本文实例为大家分享了swiper自定义分页器的样式代码,供大家参考,具体内容如下 js主要代码 pagination: { // 自定义分页器的类名----必填项 el: '.custom-pagination', // 是否可点击----必填项 clickable: true, // 分页的类型是自定义的----必填项 type: 'custom', // 自定义特殊类型分页器,当分页器类型设置为自定义时可用. renderCustom: function (swiper, current,

  • Swift实现可自定义分页宽度的UIScrollView

    最近在开发新版的APP时需要一个可自定义分页宽度的图片轮播组件.刚开始自己觉得在这个万能的互联网上早就应该有一个大侠为我们封装好了,我只需要下载.拷贝.粘贴.修改一下代码就可以了.谁知一圈下来,不知道是自己没搜到,还是什么其它原因,根本就找不到.所以,一狠心就自己开干了. 我们先看一下需要的效果,如下图: 总起来说,所需要功能有如下几项: 可自定义分页的宽度,并且在两边可以显示相邻两项的一部分,从而用户知道可以滑动: 可以无限轮播: 可以自动轮播. 对于无限轮播和自动轮播在网上一搜索一大把.这里

  • asp.net webform自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: <style type="text/css"> .pager-m-l { margin-left: 10px; } .pager { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; fo

  • 在ASP.NET 2.0中操作数据之二十六:排序自定义分页数据

    导言 和默认翻页方式相比,自定义分页能提高几个数量级的效率.当我们的需要对大量数据分页的时候就需要考虑自定义分页,然而实现自定义分页相比默认分页需要做更多工作.对于排序自定义分页数据也是这样,在本教程中我们就会扩展前面的例子来实现自定义分页数据的排序. 注意:既然本教程是基于前一个的,因此我们需要把前面教程示例页面EfficientPaging.aspx的<asp:Content>元素中的代码复制到本教程SortParameter.aspx示例页面中.关于如何进行这样的复制操作请参看为删除数据

随机推荐