Last active
December 28, 2023 23:00
-
-
Save Protoff/d6643387f03d47b44b2d7c3cf7b3e0a0 to your computer and use it in GitHub Desktop.
Тест браузера на поддержку webp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded', function() { | |
testWebP(document.body) | |
}) | |
function testWebP(elem) { | |
const webP = new Image(); | |
webP.src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA'; | |
webP.onload = webP.onerror = function () { | |
webP.height === 2 ? elem.classList.add('webp-true') : elem.classList.add('webp-false') | |
} | |
console.log(webP) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Такой тест нужен, если изображение задаётся через css. Этот код просто добавит нужный класс в зависимости от результата. Так я делаю фон с webp