溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue實(shí)現(xiàn)無縫輪播效果的示例代碼

發(fā)布時(shí)間:2021-05-17 09:23:12 來源:億速云 閱讀:269 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下vue實(shí)現(xiàn)無縫輪播效果的示例代碼,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(nèi)容如下

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

2.將兩個(gè)組件引入頁面,Sweiper.vue中用v-model傳參(v-model 其實(shí)是語法糖,默認(rèn)屬性value和默認(rèn)事件input);
代碼中我是通過v-model的selcted將值傳給Sweiper(子組件),自動(dòng)輪播時(shí)子組件再通過觸發(fā)input事件將即將顯示的值傳回給父組件

3.核心是要讓selected的值傳到SweiperItem中,與SweiperItem中的name值相等判該顯示哪張圖片;

<template>
  <div>
    <Sweiper v-model="selected">
      <!--v-model是個(gè)語法糖,相當(dāng)于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é)構(gòu)形式
<script>
  import Sweiper from "../components/Sweiper.vue";
  import SweiperItem from "../components/SweiperItem.vue";
  export default {
    name: "mySweiper",
    components: {
      Sweiper,
      SweiperItem
    },
    data() {
      return {
        selected: "item1",//默認(rèn)第一張
      }
    },
    methods:{
      getImg(url){
        return "img/"+url+".jpg"
      },

    },
    mounted(){
      /*setInterval(()=>{
       this.selected="item2"
  },3000)
  此時(shí)因?yàn)閙ounted只執(zhí)行一次,所以還是不變,需要在Sweiper寫一個(gè)watch監(jiān)聽
    }*/這一步注釋是因?yàn)閾Q到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(){
      //自動(dòng)輪播時(shí)查找name值用indexOf的方法遍歷出當(dāng)前輪播的下表
      this.names=this.$children.map(child=>{
       return child.name
      });
      this. showImg();
      this. paly()
    },
    methods:{
      showImg(){
        this.current=this.value||this.$children[0].name;
        //當(dāng)前實(shí)例的直接子組件
        this.$children.map(vm=>{
          vm.selected=this.current
        })
      },

      paly(){
        //每次輪播把圖片做調(diào)整
        this.timer=setInterval(()=>{
          //indexOf返回某個(gè)指定字符串首次出現(xiàn)的位置
          const index=this.names.indexOf(this.current);
          let newIndex=index+1;
          //嚴(yán)謹(jǐn)一點(diǎn)
          if (newIndex===this.names.length){
             newIndex=0;
          }
          this.$emit("input",this.names[newIndex])
        },3000)
      }
    },
    watch:{
      //監(jiān)聽value值,發(fā)生變化就改變selected
      value(){
        this. showImg()
      }
    },
    beforeDestroy() {
      //實(shí)列銷毀前
      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是什么

Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。

以上是“vue實(shí)現(xiàn)無縫輪播效果的示例代碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI