亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue實現(xiàn)無縫輪播效果(跑馬燈)

 更新時間:2021年09月10日 15:30:29   作者:Webwancy  
這篇文章主要為大家詳細介紹了vue實現(xiàn)無縫輪播效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)無縫輪播效果的具體代碼,供大家參考,具體內(nèi)容如下

1.首先創(chuàng)建兩個vue組件Sweiper.vue和SweiperItem.vue;

2.將兩個組件引入頁面,Sweiper.vue中用v-model傳參(v-model 其實是語法糖,默認屬性value和默認事件input);
代碼中我是通過v-model的selcted將值傳給Sweiper(子組件),自動輪播時子組件再通過觸發(fā)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>
這里的圖片沒有通過數(shù)組用v-for循環(huán),方便大家看其結(jié)構形式
<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只執(zhí)行一次,所以還是不變,需要在Sweiper寫一個watch監(jiān)聽
    }*/這一步注釋是因為換到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(){
        //每次輪播把圖片做調(diào)整
        this.timer=setInterval(()=>{
          //indexOf返回某個指定字符串首次出現(xiàn)的位置
          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:{
      //監(jiān)聽value值,發(fā)生變化就改變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>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • nuxt.js中間件實現(xiàn)攔截權限判斷的方法

    nuxt.js中間件實現(xiàn)攔截權限判斷的方法

    這篇文章主要介紹了nuxt.js中間件實現(xiàn)攔截權限判斷的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Vue實現(xiàn)滑動驗證功能

    Vue實現(xiàn)滑動驗證功能

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)滑動驗證功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Vue解決移動端彈窗滾動穿透問題

    Vue解決移動端彈窗滾動穿透問題

    這篇文章主要介紹了Vue解決移動端彈窗滾動穿透問題的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • vue-cli4創(chuàng)建項目導入Element-UI踩過的坑及解決

    vue-cli4創(chuàng)建項目導入Element-UI踩過的坑及解決

    這篇文章主要介紹了vue-cli4創(chuàng)建項目導入Element-UI踩過的坑及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue 代碼高亮插件全面對比測評

    vue 代碼高亮插件全面對比測評

    這篇文章主要介紹了vue 代碼高亮插件全面對比測評的相關資料,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下
    2021-03-03
  • Vue?Echarts實現(xiàn)帶滾動效果的柱形圖

    Vue?Echarts實現(xiàn)帶滾動效果的柱形圖

    這篇文章主要為大家詳細介紹了Vue?Echarts實現(xiàn)帶滾動效果的柱形圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • VUE微信H5生成二維碼海報保存在本地相冊的實現(xiàn)

    VUE微信H5生成二維碼海報保存在本地相冊的實現(xiàn)

    本文主要介紹了VUE微信H5生成二維碼海報保存在本地相冊的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Vue.js實現(xiàn)數(shù)據(jù)響應的方法

    Vue.js實現(xiàn)數(shù)據(jù)響應的方法

    這篇文章主要介紹了Vue.js實現(xiàn)數(shù)據(jù)響應的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • vuex中mapActions的概念及基本用法

    vuex中mapActions的概念及基本用法

    mapActions 就是將組件中的函數(shù)映射為對應的action,通過本文我們了解了mapActions 大概是用來干什么的,接下來介紹一下 mapActions 的具體用法,感興趣的朋友一起看看吧
    2023-09-09
  • vue自定義指令實現(xiàn)元素滑動移動端適配及邊界處理

    vue自定義指令實現(xiàn)元素滑動移動端適配及邊界處理

    這篇文章主要為大家介紹了vue自定義指令實現(xiàn)元素滑動移動端適配及邊界處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09

最新評論