判断浏览器是否支持WebP格式的图片

function supportWebp(){
    var webpTestsUri = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=';
    var image = new Image();
    function addResult(event) {
        // if the event is from 'onload', check the see if the image's width is
        // 1 pixel (which indiciates support). otherwise, it fails
        // 如果触发该事件的来源是“onload”并且图片的宽度是1,说明支持webp,否则,不支持webp
        window.isSupportWebp = event && event.type === 'load' ? image.width == 1 : false;
    }
    image.onerror = addResult;
    image.onload = addResult;
    image.src = webpTestsUri;
}

你可能感兴趣的:(web前端)