今天小編給大家分享的是使用ant-design中Carousel加載多個iframe的優化方法。小編覺得挺實用的,為此分享給大家做個參考。一起跟隨小編過來看看吧。
項目中遇到用ant-design 的 Carousel 加載很多個iframe,因為一次生成很多iframe,會使瀏覽器很慢
思路:只在當先顯示的項里面加載iframe,其余的都默認加載一個截圖,再切換之前用beforeChange改變 變量idx,把即將要顯示的內容由圖片變成視頻
代碼如下
<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變成即將要顯示的那個項的idx,這樣渲染的時候就會渲染iframe,而舊的也會銷毀
this.idx = to
}
}
以上就是使用ant-design中Carousel加載多個iframe的優化方法,代碼示例簡單明了,如果在日常工作遇到此問題。通過這篇文章,希望你能有所收獲,更多詳情敬請關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。