溫馨提示×

溫馨提示×

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

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

使用ant-design中Carousel加載多個(gè)iframe的優(yōu)化方法

發(fā)布時(shí)間:2020-05-26 09:48:53 來源:億速云 閱讀:1468 作者:Leah 欄目:web開發(fā)

今天小編給大家分享的是使用ant-design中Carousel加載多個(gè)iframe的優(yōu)化方法。小編覺得挺實(shí)用的,為此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

項(xiàng)目中遇到用ant-design 的 Carousel 加載很多個(gè)iframe,因?yàn)橐淮紊珊芏鄆frame,會(huì)使瀏覽器很慢
思路:只在當(dāng)先顯示的項(xiàng)里面加載iframe,其余的都默認(rèn)加載一個(gè)截圖,再切換之前用beforeChange改變 變量idx,把即將要顯示的內(nèi)容由圖片變成視頻
代碼如下

<template>
  <div class="morevideowarp">
    <template v-if="!hasvideo">
      <a-carousel :afterChange="afterChange" :beforeChange="beforeChange" arrows dotsClass="slick-dots slick-thumb scrollDivNew">
        <!-- 下面的縮略圖 -->
        <a slot="customPaging" slot-scope="props">
          <img :src="videoProList[props.i].picUrl" />
        </a>
        <!-- 向左的箭頭 -->
        <div
          slot="prevArrow"
          slot-scope="props"
          class="custom-slick-arrow"
          
        >
          <a-icon type="left-circle" />
        </div>
        <!-- 向右的箭頭 -->
        <div slot="nextArrow" slot-scope="props" class="custom-slick-arrow" >
          <a-icon type="right-circle" />
        </div>
        <!-- 主體部分 -->
        <div v-for="(item,index) in videoProList" :key="index">
                     <!-- 利用idx與index是否相等,決定渲染的是iframe或者img -->
          <template v-if="idx===index"><iframe :src="videoUrl(item)"></iframe></template>
          <template v-else><img :src="item.picUrl" /></template>
        </div>
      </a-carousel>
    </template>
    <div v-else class="noresult">暫無視頻</div>
  </div>
</template>

js邏輯部分

methods: {
    beforeChange (from, to) {
      // 切換之前把變量idx變成即將要顯示的那個(gè)項(xiàng)的idx,這樣渲染的時(shí)候就會(huì)渲染iframe,而舊的也會(huì)銷毀
      this.idx = to
    }
  }

以上就是使用ant-design中Carousel加載多個(gè)iframe的優(yōu)化方法,代碼示例簡單明了,如果在日常工作遇到此問題。通過這篇文章,希望你能有所收獲,更多詳情敬請關(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)容。

AI