-
Notifications
You must be signed in to change notification settings - Fork 7
/
__webp__.js
24 lines (22 loc) · 777 Bytes
/
__webp__.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(function () {
function checkWebp() {
try {
return (document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0)
} catch (err) {
return false
}
}
var supportWebp = checkWebp()
var htmlEl = document.querySelector('html')
if (supportWebp) htmlEl.className = htmlEl.className + ' __webp__'
window.addEventListener('DOMContentLoaded', function () {
var imgEls = document.querySelectorAll('img[data-src]')
for (var i = 0; i < imgEls.length; i++) {
var imgSrc = imgEls[i].getAttribute('data-src')
imgEls[i].removeAttribute('data-src')
if (supportWebp) imgSrc = imgSrc.replace(/(\.[^\.]+)$/, '.webp')
imgEls[i].src = imgSrc
imgEls[i].style.visibility = 'visible'
}
})
})()