vue无限轮播插件代码实例


Posted in Javascript onMay 10, 2019

思路:

要实现无限轮播,需要在轮播图前后各加一张图片,加在前面的是轮播图的最后一张图片(重复的),加在后面的是轮播图的第一张图片(重复的)。例:

<div class="wrapper-content">
      <img class="wrapper-content_img" alt="4" src="img/4.jpg"/>
      <img class="wrapper-content_img" alt="1" src="img/1.jpg"/>
      <img class="wrapper-content_img" alt="2" src="img/2.jpg"/>
      <img class="wrapper-content_img" alt="3" src="img/3.jpg" />
      <img class="wrapper-content_img" alt="4" src="img/4.jpg" />
      <img class="wrapper-content_img" alt="1" src="img/1.jpg" />
 </div>

然后再用left来控制滑动,当顺向到达alt为4的图片时,下一张滑到第六张图片,alt为1,同时改变index为1.然后立即将left移到第二张图片,alt为1那张。这样就不会被察觉

好了,贴代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <style>
      *{margin: 0;padding: 0}
      .wrapper{position: relative;overflow: hidden;}
      .wrapper-content{position: absolute;left: 0;z-index: 1;}
      .wrapper-content_img{border: none;outline:none;float: left}
      .wrapper-buttons{position: absolute;width: 100px;height: 20px;text-align: center;bottom: 3px;z-index: 2;}
      .wrapper-button{float: left;width: 20px;height: 20px;border-radius: 10px;background: gray;margin: 0 2.5px;cursor: pointer;}
      .wrapper-arrow{position: absolute;width: 40px;height:40px;cursor: pointer;background-color: RGBA(0,0,0,.3); color: #fff;display: none;top:50%;line-height: 40px;font-size: 36px;text-align: center;z-index: 2;}
      .wrapper:hover .wrapper-arrow{display: block;background-color: rgba(0,0,0,.7);}
      .wrapper-prev{left:10px;}
      .wrapper-next{right:10px;}
      .wrapper_on{background-color: yellow}
      .wrapper_trans{transition: left .3s ease}
    </style>
  </head>
  <body>
  <div id="app"> 
    <div class="wrapper" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}" @mouseover="stop" @mouseout="play">
      <div class="wrapper-content" :class="{wrapper_trans:isTrans}" :style="{width:originalData.img_width*(originalData.num+2)+'px',height:originalData.img_height+'px',left:-originalData.img_width+'px'}" ref="wrapperContent">
        <img class="wrapper-content_img" alt="4" src="img/4.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
        <img class="wrapper-content_img" alt="1" src="img/1.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
        <img class="wrapper-content_img" alt="2" src="img/2.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
        <img class="wrapper-content_img" alt="3" src="img/3.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
        <img class="wrapper-content_img" alt="4" src="img/4.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
        <img class="wrapper-content_img" alt="1" src="img/1.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
      </div>
      <div class="wrapper-buttons" :style="{left:(originalData.img_width-100)/2+'px'}">
        <span :class="['wrapper-button',{'wrapper_on':index==1}]" @click="turnTo(1)"></span>
        <span :class="['wrapper-button',{'wrapper_on':index==2}]" @click="turnTo(2)"></span>
        <span :class="['wrapper-button',{'wrapper_on':index==3}]" @click="turnTo(3)"></span>
        <span :class="['wrapper-button',{'wrapper_on':index==4}]" @click="turnTo(4)"></span>
      </div>

      <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="wrapper-arrow wrapper-prev" :style="{marginTop:-originalData.btn_width/2+'px'}" @click="prev"><</a>
      <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="wrapper-arrow wrapper-next" :style="{marginTop:-originalData.btn_width/2+'px'}" @click="next">></a>
    </div>
  </div>
  <a href="#" rel="external nofollow" style="width: 50px;height: 50px;position: absolute;top: 500px;">aaa</a>

  <script type="text/javascript" src="https://unpkg.com/vue@2.1.10/dist/vue.js"></script>
  <script type="text/javascript">
    new Vue({
      el:'#app',
      data:{
        originalData:{
          img_width:350,
          img_height:350,
          btn_width:40,
          btn_height:40,
          num:4,
          delay:300
        },
        isTrans:true,//因为到最后一张图片,index为1时,需要立即跳到第二张index也为1的图片,这个用来是否给出transition
        index:1,
        timer:null,//setInterval
        clickdelay:false//用来防止连续点击
      },
      methods:{
        next(){
          if(this.clickdelay){
            return 
          }
          this.clickdelay=true
          if(this.index==this.originalData.num){
            this.index=1
          }else{

            this.index+=1
          }
          this.animate(this.originalData.img_width)

        },
        prev(){
          if(this.clickdelay){
            return 
          }
          this.clickdelay=true
          if(this.index==1){
            this.index=this.originalData.num
          }else{
            this.index-=1
          }
          this.animate(-this.originalData.img_width) 
        },
        animate(offset){
          var node=this.$refs.wrapperContent
          var self=this;
          var left=parseInt(node.style.left)-offset
          this.isTrans=true
          node.style.left=left+'px'
          setTimeout(function(){
            if(left<-(self.originalData.num*self.originalData.img_width)){
              self.isTrans=false
              node.style.left=-self.originalData.img_width+'px'
              self.clickdelay=false //当到达最后一张图片时 
            }
            if(left>-100){
              self.isTrans=false
              node.style.left=-self.originalData.num*self.originalData.img_width+'px'
              self.clickdelay=false //当到达第一张图片时 
            }
          },this.originalData.delay)
        },
        play(){

          var self=this;
          this.timer=setInterval(function(){
            self.next()
          },2000)
        },
        stop(){
          this.clickdelay=false//用来防止连续点击
          clearInterval(this.timer)
          this.timer=null
        },
        turnTo(flag){
          if(flag==this.index){
            return
          }else{
            var offset=(flag-this.index)*this.originalData.img_width
            this.index=flag
            this.animate(offset)
          }

        }
      },

      mounted(){
        /*下面是判断过渡动画是否完成*/ 
        var node=this.$refs.wrapperContent
        var transitions = {
           'transition':'transitionend',
           'OTransition':'oTransitionEnd',
           'MozTransition':'transitionend',
           'WebkitTransition':'webkitTransitionEnd'
          }
          var self=this

        for(var t in transitions){

          if( node.style[t] !== undefined ){
            var transitionEvent=transitions[t];
          }
        }
        transitionEvent && node.addEventListener(transitionEvent, function() {
          self.clickdelay=false       
        });
        this.play()
      }
    })
  </script>
  </body>
</html>

以上所述是小编给大家介绍的vue无限轮播插件详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
jQuery选择头像并实时显示的代码
Jun 27 Javascript
JS字符串累加Array不一定比字符串累加快(根据电脑配置)
May 14 Javascript
onclick与listeners的执行先后问题详细解剖
Jan 07 Javascript
js showModalDialog参数的使用详解
Jan 07 Javascript
JavaScript判断用户是否对表单进行了修改的方法
Mar 18 Javascript
JSON+Jquery省市区三级联动
Jan 13 Javascript
jQuery实现遮罩层登录对话框
Dec 29 Javascript
如何解决vue与传统jquery插件冲突
Mar 20 Javascript
html5+canvas实现支持触屏的签名插件教程
May 08 Javascript
js实现HTML中Select二级联动的实例
Jan 05 Javascript
vue+springmvc导出excel数据的实现代码
Jun 27 Javascript
vue中element 的upload组件发送请求给后端操作
Sep 07 Javascript
js中的深浅拷贝问题简析
May 10 #Javascript
解决cordova+vue 项目打包成APK应用遇到的问题
May 10 #Javascript
详解jQuery如何实现模糊搜索
May 10 #jQuery
JS匿名函数内部this指向问题详析
May 10 #Javascript
详解关于React-Router4.0跳转不置顶解决方案
May 10 #Javascript
Vue使用watch监听一个对象中的属性的实现方法
May 10 #Javascript
微信小程序获取用户信息并保存登录状态详解
May 10 #Javascript
You might like
汉字转化为拼音(php版)
2006/10/09 PHP
PHP 单引号与双引号的区别
2009/11/24 PHP
PHP简洁函数(PHP简单明了函数语法)
2012/06/10 PHP
discuz加密解密函数使用方法和中文注释
2014/01/21 PHP
php指定长度分割字符串str_split函数用法示例
2017/01/30 PHP
php和asp语法上的区别总结
2019/05/12 PHP
js event事件的传递与冒泡处理
2009/12/06 Javascript
Extjs学习笔记之四 工具栏和菜单
2010/01/07 Javascript
javascript setTimeout和setInterval计时的区别详解
2013/06/21 Javascript
Angularjs使用ng-repeat中$even和$odd属性的注意事项
2016/12/31 Javascript
jQuery实现所有验证通过方可提交的表单验证
2017/11/21 jQuery
JS实现的简单分页功能示例
2018/08/23 Javascript
koa+mongoose实现简单增删改查接口的示例代码
2019/05/13 Javascript
Python入门篇之数字
2014/10/20 Python
Python中使用异常处理来判断运行的操作系统平台方法
2015/01/22 Python
python验证码识别教程之利用滴水算法分割图片
2018/06/05 Python
对Python闭包与延迟绑定的方法详解
2019/01/07 Python
Python OpenCV对本地视频文件进行分帧保存的实例
2019/01/08 Python
Python selenium 自动化脚本打包成一个exe文件(推荐)
2020/01/14 Python
Python接口自动化判断元素原理解析
2020/02/24 Python
自定义Django默认的sitemap站点地图样式
2020/03/04 Python
Python操作Jira库常用方法解析
2020/04/10 Python
如何在mac下配置python虚拟环境
2020/07/06 Python
Expected conditions模块使用方法汇总代码解析
2020/08/13 Python
解决Pymongo insert时会自动添加_id的问题
2020/12/05 Python
解决canvas转base64/jpeg时透明区域变成黑色背景的方法
2016/10/23 HTML / CSS
澳大利亚新奇小玩意网站:Yellow Octopus
2017/12/28 全球购物
请介绍一下WSDL的文档结构
2013/03/17 面试题
公路绿化方案
2014/05/12 职场文书
公司人事专员岗位职责
2014/08/11 职场文书
销售顾问工作计划书
2014/09/15 职场文书
优秀党员自我评价范文
2014/09/15 职场文书
2014年单位工作总结范文
2014/11/27 职场文书
小学运动会开幕词
2015/01/28 职场文书
公司员工培训管理制度
2015/08/04 职场文书
基于python定位棋子位置及识别棋子颜色
2021/07/26 Python