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


Posted in Javascript onMay 25, 2017

项目背景

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

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

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

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

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

当当当当~~~

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

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

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

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

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

Result

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

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

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

Javascript 相关文章推荐
hover的用法及live的用法介绍(鼠标悬停效果)
Mar 29 Javascript
javascript使用for循环批量注册的事件不能正确获取索引值的解决方法
Dec 20 Javascript
Javascript实现汉字和拼音互转的终极方案
Oct 19 Javascript
AngularJS创建自定义指令的方法详解
Nov 03 Javascript
BootStrap Datetimepicker 汉化的实现代码
Feb 10 Javascript
Angular.Js中ng-include指令的使用与实现
May 07 Javascript
vue-router实现组件间的跳转(参数传递)
Nov 07 Javascript
记React connect的几种写法(小结)
Sep 18 Javascript
VUE-cli3使用 svg-sprite-loader
Oct 20 Javascript
fetch 如何实现请求数据
Dec 20 Javascript
VUE项目中加载已保存的笔记实例方法
Sep 14 Javascript
实现vuex与组件data之间的数据同步更新方式
Nov 12 Javascript
Javascript 实现匿名递归的实例代码
May 25 #Javascript
Kotlin学习第一步 kotlin语法特性
May 25 #Javascript
jQuery Masonry瀑布流布局神器使用详解
May 25 #jQuery
jQuery模拟实现天猫购物车动画效果实例代码
May 25 #jQuery
jquery.masonry瀑布流效果
May 25 #jQuery
Node.js操作redis实现添加查询功能
May 25 #Javascript
浅谈struts1 &amp; jquery form 文件异步上传
May 25 #jQuery
You might like
php存储过程调用实例代码
2013/02/03 PHP
LNMP部署laravel以及xhprof安装使用教程
2017/09/14 PHP
PHP实现支持CURL字符串证书传输的方法
2019/03/23 PHP
jquery蒙版控件实现代码
2010/12/08 Javascript
jQuery div层的放大与缩小简单实现代码
2013/03/28 Javascript
jQuery焦点控制图层展示延迟隐藏的方法
2015/03/09 Javascript
JavaScript动态修改网页元素内容的方法
2015/03/21 Javascript
js实现仿Discuz文本框弹出层效果
2015/08/13 Javascript
基于jQuery的网页影音播放器jPlayer的基本使用教程
2016/03/08 Javascript
Bootstrap弹出框modal上层的输入框不能获得焦点问题的解决方法
2016/12/13 Javascript
利用Js+Css实现折纸动态导航效果实例源码
2017/01/25 Javascript
一个简单的node.js界面实现方法
2018/06/01 Javascript
layui自定义验证,用ajax查询后台是否有重复数据,form.verify的例子
2019/09/06 Javascript
vue3.0 加载json的方法(非ajax)
2020/10/26 Javascript
用map函数来完成Python并行任务的简单示例
2015/04/02 Python
python链接oracle数据库以及数据库的增删改查实例
2018/01/30 Python
Python实现的FTP通信客户端与服务器端功能示例
2018/03/28 Python
Python学习_几种存取xls/xlsx文件的方法总结
2018/05/03 Python
python中ASCII码字符与int之间的转换方法
2018/07/09 Python
python去掉 unicode 字符串前面的u方法
2018/10/21 Python
python Event事件、进程池与线程池、协程解析
2019/10/25 Python
pymysql 插入数据 转义处理方式
2020/03/02 Python
如何基于Python代码实现高精度免费OCR工具
2020/06/18 Python
CSS3 Columns分列式布局方法简介
2014/05/03 HTML / CSS
HTML5中使用postMessage实现两个网页间传递数据
2016/06/22 HTML / CSS
最新的小工具和卓越的产品设计:Oh That Tech!
2019/08/07 全球购物
护士实习自我鉴定
2013/10/22 职场文书
求职推荐信
2013/10/28 职场文书
师范生实习自我鉴定
2013/11/01 职场文书
中专药剂专业应届毕的自我评价
2013/12/27 职场文书
公司采购主管岗位职责
2014/06/17 职场文书
教师个人成长总结
2015/02/11 职场文书
死者家属慰问信
2015/03/24 职场文书
2015领导干部廉洁自律工作总结
2015/07/23 职场文书
spring注解 @PropertySource配置数据源全流程
2022/03/25 Java/Android
Python之Matplotlib绘制热力图和面积图
2022/04/13 Python