詳解JavaScript如何創(chuàng)建一個(gè)非自動(dòng)播放的GIF網(wǎng)絡(luò)組件
今天,我將向您展示如何創(chuàng)建一個(gè)允許您的用戶決定是否要播放 gif 的 Web 組件!讓我們開(kāi)始吧。
一些很可愛(ài)的測(cè)試數(shù)據(jù)
這里用的gif是小駱駝和貓的這種可愛(ài)互動(dòng):

哇,太可愛(ài)了!我可以看一天這個(gè)
構(gòu)建 Web 組件
對(duì)于這個(gè) Web 組件,我們需要一些東西:
- 畫(huà)布(“縮略圖”所在的位置)
- 一張圖片(實(shí)際的 gif)
- 標(biāo)有“gif”的標(biāo)簽
- 一些造型
讓我們這樣做:
const noAutoplayGifTemplate = document.createElement('template')
noAutoplayGifTemplate.innerHTML = `
<style>
.no-autoplay-gif {
--size: 30px;
cursor: pointer;
position: relative;
}
.no-autoplay-gif .gif-label {
border: 2px solid #000;
background-color: #fff;
border-radius: 100%;
width: var(--size);
height: var(--size);
text-align: center;
font: bold calc(var(--size) * 0.4)/var(--size) sans-serif;
position: absolute;
top: calc(50% - var(--size) / 2);
left: calc(50% - var(--size) / 2);
}
.no-autoplay-gif .hidden {
display: none;
}
</style>
<div class="no-autoplay-gif">
<canvas />
<span class="gif-label" aria-hidden="true">GIF</span>
<img class="hidden">
</div>接下來(lái),我們將創(chuàng)建一個(gè)派生自 HTMLElement 的類。 此類稍后將包含播放/停止切換行為。
class NoAutoplayGif extends HTMLElement {
constructor() {
super()
// 在此處添加設(shè)置
}
loadImage() {
// 在此處添加渲染
}
static get observedAttributes() {
return ['src', 'alt'];
}
attributeChangedCallback(name, oldVal, newVal) {
if (oldVal !== newVal || oldVal === null) {
this.loadImage()
}
}
}這里還有一些樣板:一個(gè)空的渲染函數(shù),它將加載圖像并顯示縮略圖,以及一個(gè)構(gòu)造函數(shù)和一些特定于 Web 組件的方法。
好的,這已經(jīng)是很多代碼了。讓我解釋。
該loadImage函數(shù)不會(huì)自動(dòng)調(diào)用,我們需要自己調(diào)用。該函數(shù)attributeChangedCallback讓我們定義當(dāng)任何指定屬性發(fā)生observedAttributes變化時(shí)會(huì)發(fā)生什么。在這種情況下:加載圖像并顯示它。瀏覽器大致做的是這樣的:
- 遇到 web 組件
- 調(diào)用它的構(gòu)造函數(shù)(調(diào)用
constructor()) - 將其屬性一一設(shè)置為 DOM 中的設(shè)置(因此,
src="llama.gif"調(diào)用.setAttribute('src', 'llama.gif') attributeChangedCallback對(duì)每個(gè)更改的屬性執(zhí)行
簽入構(gòu)造函數(shù)時(shí),這些屬性一開(kāi)始是空的,稍后才會(huì)填充。如果我們需要一個(gè)或多個(gè)屬性來(lái)實(shí)際進(jìn)行渲染,那么如果我們 知道 這些屬性不存在,那么調(diào)用該loadImage函數(shù)是沒(méi)有意義的。所以我們不在構(gòu)造函數(shù)中調(diào)用它,但只有在有可能存在屬性時(shí)才調(diào)用它。**
為了完成樣板化,讓我們將這個(gè)類定義為我們的自定義 Web 組件:
class NoAutoplayGif extends HTMLElement {
// ...
}
window.customElements.define('no-autoplay-gif', NoAutoplayGif)我們現(xiàn)在可以像這樣使用這個(gè)組件:
<no-autoplay-gif src="..." alt="Llama and cat" />
邏輯
有趣的來(lái)了。我們需要添加noAutoplayGifTemplate作為組件的shadow DOM。src這已經(jīng)可以渲染 DOM,但是如果沒(méi)有andalt屬性,我們?nèi)匀徊荒茏龊芏嗍虑?。因此我們只?shadow DOM 中收集一些我們稍后需要的元素,并且已經(jīng)附加了一個(gè)單擊偵聽(tīng)器來(lái)切換啟動(dòng)/停止模式。
class NoAutoplayGif extends HTMLElement {
constructor() {
super()
// 添加 shadow DOM
this._shadowRoot = this.attachShadow({ mode: 'open' })
// 從上面添加模板
this._shadowRoot.appendChild(
noAutoplayGifTemplate.content.cloneNode(true)
)
// 我們稍后會(huì)需要這些
this.canvas = this._shadowRoot.querySelector('canvas')
this.img = this._shadowRoot.querySelector('img')
this.label = this._shadowRoot.querySelector('.gif-label')
this.container = this._shadowRoot.querySelector('.no-autoplay-gif')
// 使整個(gè)東西可點(diǎn)擊
this._shadowRoot.querySelector('.no-autoplay-gif').addEventListener('click', () => {
this.toggleImage()
})
}
// ...
}為了不遇到未定義的方法錯(cuò)誤,我們還添加了這三個(gè)方法:
class NoAutoplayGif extends HTMLElement {
// ...
toggleImage(force = undefined) {
this.img.classList.toggle('hidden', force)
// We need to check for undefined values, as JS does a distinction here.
// We cannot simply negate a given force value (i.e. hiding one thing and unhiding another)
// as an undefined value would actually toggle the img, but
// always hide the other two, because !undefined == true
this.canvas.classList.toggle('hidden', force !== undefined ? !force : undefined)
this.label.classList.toggle('hidden', force !== undefined ? !force : undefined)
}
start() {
this.toggleImage(false)
}
stop() {
this.toggleImage(true)
}
// ...
}start/stop 方法允許我們強(qiáng)制啟動(dòng)或強(qiáng)制停止 gif。理論上,我們現(xiàn)在可以這樣做:
const gif = document.querySelector('no-autoplay-gif')
gif.start()
gif.stop()
gif.toggleImage()最后,我們可以添加圖片加載部分。讓我們先做一些驗(yàn)證:
class NoAutoplayGif extends HTMLElement {
// ...
loadImage() {
const src = this.getAttribute('src')
const alt = this.getAttribute('alt')
if (!src) {
console.warn('A source gif must be given')
return
}
if (!src.endsWith('.gif')) {
console.warn('Provided src is not a .gif')
return
}
// More stuff
}
// ...
}最后一步,我們可以加載圖像,設(shè)置一些寬度和高度并使用畫(huà)布:
class NoAutoplayGif extends HTMLElement {
// ...
loadImage() {
// Validation
this.img.onload = event => {
const width = event.currentTarget.width
const height = event.currentTarget.height
// Set width and height of the entire thing
this.canvas.setAttribute('width', width)
this.canvas.setAttribute('height', height)
this.container.setAttribute('style', `
width: ${width}px;
height: ${height}px;
`)
// "Draws" the gif onto a canvas, i.e. the first
// frame, making it look like a thumbnail.
this.canvas.getContext('2d').drawImage(this.img, 0, 0)
}
// Trigger the loading
this.img.src = src
this.img.alt = alt
}
// ...
}我們完成了!
結(jié)果
演示地址:haiyong.site/gif

以上就是詳解JavaScript如何創(chuàng)建一個(gè)非自動(dòng)播放的GIF網(wǎng)絡(luò)組件的詳細(xì)內(nèi)容,更多關(guān)于JavaScript播放GIF的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js獲取url參數(shù)的使用擴(kuò)展實(shí)例
js獲取參數(shù)的具體應(yīng)用,歡迎加入腳本之家交流群2007-12-12
ES6新數(shù)據(jù)結(jié)構(gòu)Map功能與用法示例
這篇文章主要介紹了ES6新數(shù)據(jù)結(jié)構(gòu)Map功能與用法,結(jié)合實(shí)例形式分析了Map的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03
JavaScript實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
echarts餅圖自定義設(shè)置顏色的3種實(shí)現(xiàn)方式
ECharts餅圖的顏色可以通過(guò)多種方式進(jìn)行設(shè)置,下面這篇文章主要給大家介紹了關(guān)于echarts餅圖自定義設(shè)置顏色的3種實(shí)現(xiàn)方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
javascript full screen 全屏顯示頁(yè)面元素的方法
要想讓頁(yè)面的某個(gè)元素全屏顯示,就像在網(wǎng)頁(yè)上看視頻的時(shí)候,可以全屏觀看一樣,該怎么實(shí)現(xiàn)呢2013-09-09
javascript 局部頁(yè)面打印實(shí)現(xiàn)代碼
Web打印有很多方式,水晶報(bào)表使用的恐怕比較多,但這東西是收費(fèi)軟件,老板說(shuō)不能用:(。2009-08-08

