基于vue实现swipe分页组件实例

 更新时间:2017年05月25日 10:23:58   作者:louisanikita  
本篇文章主要介绍了基于vue实现swipe分页组件实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

项目背景

图片轮播是前端项目必有项,当前有很多效果很酷炫的轮播插件,例如 Swiper 。

但是当我们项目中的图片轮播只需要一个很简单的轮播样式,比如这样的

我们引用这样一个 110k 的大插件,就大材小用了。再安利一下,swiper2.x和swiper3.x对移动和PC端支持情况如下图

当当当当~~~

我们今天的主角登场了, thebird/Swipe ,这个插件完成了图片轮播需要的基本功能,只有 14.2k ,真真的 轻量级 啊。还有,还有

翻译一下,就是俺们全支持,不管你是PC端(IE7+)还是移动端浏览器。此处应该有掌声,哈哈~

简而言之,就是当需要一个简单的轮播时,可以选用 thebird/Swipe ,自己写一个组件。

举个栗子,就是我实现的这个—— 基于 vue 实现swipe分页组件 ,移动端和PC端均适用哦。

Result

Usage

一般情况,轮播图片因为是要经常换的,故在后台定制,定制内容如下

<div><a href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" "/></a></div>
<div><a href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" "/></a></div>
<div><a href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" "/></a></div>

没有定制,必须在代码里写的话,也是可以的,造一个data数组swipeInfo

<!--js-->
data:{
  swipeInfo:[{
    href:"http://www.baidu.com",
    imgSrc:""
  },{
    href:"http://www.baidu.com",
    imgSrc:""
  },{
    href:"http://www.baidu.com",
    imgSrc:""
  }]
},
components: {
  'swipe-module': require('pagination-swipe'),
},

在html中绑定该数据

<!--html-->
<swipe-module :swipeinfo="swipeInfo"></swipe-module>

pagination-swipe组件内容

按照swipe构造html框架,添加了pagination块

<!--template.html-->
<div v-el:swipe class='swipe bar-slider'>
  <div class='swipe-wrap'>
    <div v-for="item in swipeinfo"><a :href=item.href><img :src=item.imgSrc /></a></div>
  </div>
  <!-- 分页 -->
  <div class="pagination">
    <span class="swipe-pagination-switch swipe-active-switch" @click="slideToCur(0)"></span>
    <span class="swipe-pagination-switch" @click="slideToCur($index+1)" v-for="item in slideNum"></span>
  </div>
</div>

vue构造组件

//index.js
require('./style.less');
var Swipe = require('swipe');

Vue.component('pagination-swipe',{
  props: ['swipeinfo'],
  template: require('raw!./template.html'),
  data: function() {
    return {
      mySwipe: {},
      slideNum: {},
    };
  },
  ready: function() {
    var self = this;
    //获取子组件中分页小圈圈
    var slides = self.$els.swipe.getElementsByClassName('swipe-pagination-switch');
    self.mySwipe = new Swipe(self.$els.swipe, {
      startSlide: 0,
      continuous: true,
      speed: 1000,
      auto: 4000,
      stopPropagation: false,
      callback: function(index, elem) {
        //渲染分页小圈圈
        for (var i = 0; i < slides.length; i++) {
          if (i != index) {
            slides[i].style.opacity = "0.2";
            slides[i].style.background = "#000";
          } else {
            slides[index].style.opacity = "1";
            slides[index].style.background = "#ee3a4a";
          }
        }
      },
    });
    self.slideNum = self.mySwipe.getNumSlides() - 1;
  },
  methods: {
    //点击底部小圈圈,跳到其所对应页
    slideToCur: function(index) {
      var self = this;
      self.mySwipe.slide(index, 300);
    },
  }
});

<!--style.less-->
.swipe {
  overflow: hidden;
  visibility: hidden;
  position: relative;
  height: 200/@rem;
  .swipe-wrap {
    position: relative;
    overflow: hidden;
    height: 100%;
    div {
      float: left;
      width: 100%;
      position: relative;
      margin: 0;
      a {
        width: 100%;
        height: 100%;
        background-position: center 0;
        background-repeat: no-repeat;
        background-color: transparent;
        display: block;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
  .pagination {
    text-align: center;
    position: relative;
    bottom: 40/@rem;
    cursor: pointer;
  }
  .swipe-pagination-switch {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 100%;
    background: #000;
    opacity: 0.2;
    margin: 0 8px;
    z-index: 10;
    &:first-child {
      background: #ee3a4a;
    }
  }
  .swipe-active-switch {
    opacity: 1;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 在vue2项目中使用dart-sass的问题及解决方法

    在vue2项目中使用dart-sass的问题及解决方法

    在Vue2项目中,使用dart-sass替代node-sass可以解决安装困难和环境兼容问题,VueCLI3+用户可直接使用,而VueCLI2用户需升级VueCLI和项目,具体方法包括修改package.json依赖并使用.scss文件编写样式,此更改有助于提升项目的开发效率和跨平台兼容性
    2024-09-09
  • 前端数据存储常用工具Vuex、Pinia、Redux详解

    前端数据存储常用工具Vuex、Pinia、Redux详解

    Redux、Vuex 和 Pinia 都是用于状态管理的流行框架,这篇文章主要介绍了前端数据存储常用工具Vuex、Pinia、Redux的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-04-04
  • 浅谈vue+vite项目部署会遇到的几个问题

    浅谈vue+vite项目部署会遇到的几个问题

    本文主要介绍了vue+vite项目部署会遇到的几个问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 使用vue构建多页面应用的示例

    使用vue构建多页面应用的示例

    这篇文章主要介绍了使用vue构建多页面应用的示例,帮助大家更好的理解和使用vue,感兴趣的朋友可以了解下
    2020-10-10
  • vue3.x中apollo的使用案例代码

    vue3.x中apollo的使用案例代码

    这篇文章主要介绍了vue3.x中apollo的使用,通过前端自身直接获取到apollo的配置目前看到官方支持的客户端是没有vue的,本文给大家介绍了前端获取到apollo数据的过程,需要的朋友可以参考下
    2023-02-02
  • Vue Element UI 中 el-table 树形数据 tree-props 多层级使用避坑指南

    Vue Element UI 中 el-table 树形数据 

    这篇文章主要介绍了Vue Element UI 中 el-table 树形数据 tree-props 多层级使用避坑指南,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • vue中利用pinyin-pro纯前端实现拼音的模糊搜索功能

    vue中利用pinyin-pro纯前端实现拼音的模糊搜索功能

    这篇文章主要介绍了vue中利用pinyin-pro纯前端实现拼音的模糊搜索,实现思路很简单,通过安装配置插件编写工具类,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • vue el-upload手动上传实现过程

    vue el-upload手动上传实现过程

    这篇文章主要介绍了vue el-upload手动上传实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • Vue事件总线的使用问题及解读

    Vue事件总线的使用问题及解读

    这篇文章主要介绍了Vue事件总线的使用问题及解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • vue2和vue3的v-if与v-for优先级对比学习

    vue2和vue3的v-if与v-for优先级对比学习

    这篇文章主要介绍了vue2和vue3的v-if与v-for优先级对比学习,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10

最新评论