vue实现无缝轮播效果(跑马灯)


Posted in Vue.js onMay 14, 2021

本文实例为大家分享了vue实现无缝轮播效果的具体代码,供大家参考,具体内容如下

1.首先创建两个vue组件Sweiper.vue和SweiperItem.vue;

2.将两个组件引入页面,Sweiper.vue中用v-model传参(v-model 其实是语法糖,默认属性value和默认事件input);
代码中我是通过v-model的selcted将值传给Sweiper(子组件),自动轮播时子组件再通过触发input事件将即将显示的值传回给父组件

3.核心是要让selected的值传到SweiperItem中,与SweiperItem中的name值相等判该显示哪张图片;

<template>
  <div>
    <Sweiper v-model="selected">
      <!--v-model是个语法糖,相当于value和input事件-->
      <Sweiper-item  name="item1">
        <div class="item">
          <img :src="getImg('01')" alt="">
        </div>
      </Sweiper-item>
      <Sweiper-item name="item2">
        <div class="item">
          <img :src="getImg('02')" alt="">
        </div>
      </Sweiper-item>
      <Sweiper-item name="item3">
        <div class="item">
          <img :src="getImg('03')" alt="">
        </div>
      </Sweiper-item>
    </Sweiper>
  </div>
</template>
这里的图片没有通过数组用v-for循环,方便大家看其结构形式
<script>
  import Sweiper from "../components/Sweiper.vue";
  import SweiperItem from "../components/SweiperItem.vue";
  export default {
    name: "mySweiper",
    components: {
      Sweiper,
      SweiperItem
    },
    data() {
      return {
        selected: "item1",//默认第一张
      }
    },
    methods:{
      getImg(url){
        return "img/"+url+".jpg"
      },

    },
    mounted(){
      /*setInterval(()=>{
       this.selected="item2"
  },3000)
  此时因为mounted只执行一次,所以还是不变,需要在Sweiper写一个watch监听
    }*/这一步注释是因为换到Sweiper组件中写了
  }
</script>
<style >
  .item{
    /*border: 1px solid black;*/
  }
  .item>img{
    width: 100%;
    /*height: 0.1rem;*/
  }
</style>

Sweiper.vue

<template>
  <div class="Sweiper">
    <slot></slot>
  </div>
</template>
<script>

  export default {
    name: "Sweiper",
    data() {
      return{
        current:''
      }
    },
    props:{
      value:{
        type:String,
        default:""
      },
    },
    mounted(){
      //自动轮播时查找name值用indexOf的方法遍历出当前轮播的下表
      this.names=this.$children.map(child=>{
       return child.name
      });
      this. showImg();
      this. paly()
    },
    methods:{
      showImg(){
        this.current=this.value||this.$children[0].name;
        //当前实例的直接子组件
        this.$children.map(vm=>{
          vm.selected=this.current
        })
      },

      paly(){
        //每次轮播把图片做调整
        this.timer=setInterval(()=>{
          //indexOf返回某个指定字符串首次出现的位置
          const index=this.names.indexOf(this.current);
          let newIndex=index+1;
          //严谨一点
          if (newIndex===this.names.length){
             newIndex=0;
          }
          this.$emit("input",this.names[newIndex])
        },3000)
      }
    },
    watch:{
      //监听value值,发生变化就改变selected
      value(){
        this. showImg()
      }
    },
    beforeDestroy() {
      //实列销毁前
      clearInterval(this.timer)
    }
  };
</script>
<style>
  .Sweiper{
    /*border: 1px solid black;*/
    width: 100%;
    height: 4rem;
    overflow: hidden;
    margin: 0 auto;
    position: relative;
  }
</style>

SweiperItem.vue

<template>
  <transition>
    <div class="Sweiper-item" v-show="isShow">
      <slot></slot>
    </div>
  </transition>
</template>
<script>
  export  default {
    name:"SweiperItem",
    data(){
      return{
        selected:""
      }
    },
    props:{
      name:{
        type:String,
        required:true
      },
    },
    mounted(){

    },
    computed:{
      isShow(){
        return this.selected===this.name;
      }
    }
  };

</script>
<style>
  .v-enter-active,.v-leave-active{
    transition: all 1s linear;
  }
  .v-leave-to{
    transform:translate(-100%);
  }
  .v-enter{
    transform: translate(100%);
  }
  .v-enter-active{
    position: absolute;
    top:0;
    left: 0;
  }
</style>

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

Vue.js 相关文章推荐
详解vue实现坐标拾取器功能示例
Nov 18 Vue.js
vue实现两个区域滚动条同步滚动
Dec 13 Vue.js
vue使用element-ui实现表单验证
Dec 13 Vue.js
vscode自定义vue模板的实现
Jan 27 Vue.js
vue 动态添加的路由页面刷新时失效的原因及解决方案
Feb 26 Vue.js
Vue-router编程式导航的两种实现代码
Mar 04 Vue.js
vue中data改变后让视图同步更新的方法
Mar 29 Vue.js
vue项目两种方式实现竖向表格的思路分析
Apr 28 Vue.js
vue3使用vue-router的完整步骤记录
Jun 20 Vue.js
vue实现锚点定位功能
Jun 29 Vue.js
Vue.js中v-for指令的用法介绍
Mar 13 Vue.js
ant design vue的form表单取值方法
Jun 01 Vue.js
Vue接口封装的完整步骤记录
Vue全家桶入门基础教程
vue实现可拖拽的dialog弹框
vue如何批量引入组件、注册和使用详解
vue组件的路由高亮问题解决方法
Vue通过懒加载提升页面响应速度
Vue详细的入门笔记
You might like
SONY SRF-M100的电路分析
2021/03/02 无线电
php常用hash加密函数
2014/11/22 PHP
PHP创建PowerPoint2007文档的方法
2015/12/10 PHP
Zend Framework实现将session存储在memcache中的方法
2016/03/22 PHP
WordPress伪静态规则设置代码实例
2020/12/10 PHP
使用JQUERY Tabs插件宿主IFRAMES
2010/01/01 Javascript
基于jquery的不规则矩形的排列实现代码
2012/04/16 Javascript
分享精心挑选的23款美轮美奂的jQuery 图片特效插件
2012/08/14 Javascript
JavaScript String.replace函数参数实例说明
2013/06/06 Javascript
jQuery+css3实现文字跟随鼠标的上下抖动
2015/07/31 Javascript
JavaScript数据结构与算法之集合(Set)
2016/01/29 Javascript
浅谈MVC+EF easyui dataGrid 动态加载分页表格
2016/11/10 Javascript
jQuery自定义插件详解及实例代码
2016/12/29 Javascript
NodeJS基础API搭建服务器详细过程记录
2017/04/01 NodeJs
利用Angular2的Observables实现交互控制的方法
2018/12/27 Javascript
微信小程序云开发如何使用云函数生成二维码
2019/05/18 Javascript
简单的抓取淘宝图片的Python爬虫
2014/12/25 Python
Python中常见的数据类型小结
2015/08/29 Python
Python 基础教程之包和类的用法
2017/02/23 Python
python实现m3u8格式转换为mp4视频格式
2018/02/28 Python
对Python字符串中的换行符和制表符介绍
2018/05/03 Python
解决pycharm安装后代码区不能编辑的问题
2018/10/28 Python
python使用pdfminer解析pdf文件的方法示例
2018/12/20 Python
Python 转换文本编码实现解析
2019/08/27 Python
CSS3实现淘宝留白的方法
2020/06/05 HTML / CSS
借助HTML5 Canvas来绘制三角形和矩形等多边形的方法
2016/03/14 HTML / CSS
AutoShack.com加拿大:北美主要的汽车零部件零售商
2019/07/24 全球购物
荷兰时尚精品店:Labels Fashion
2020/03/22 全球购物
车间组长岗位职责
2013/12/20 职场文书
房屋公证委托书
2014/04/03 职场文书
团委竞选演讲稿
2014/04/24 职场文书
乡镇党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
教师先进个人材料
2014/12/17 职场文书
2015年小学教导处工作总结
2015/05/26 职场文书
疾病证明书
2015/06/19 职场文书
新郎结婚感言
2015/07/31 职场文书