如何强制浏览器预加载带有显示的图像:无?

Firefox不会加载带有显示的图像:在需要向用户显示之前,不会加载任何图像。铬将一直停滞,直到图像加载完毕,然后再显示。

文件较小时,如果图像尚未在Firefox上加载,则会短暂闪烁。如果文件较大,则延迟会更长,这也将使Chromium的停顿变得明显。

我想显示带有图像的图像:未预加载图像,因此显示图像时没有延迟。

我尝试使用Javascript声明新的Image对象,但是 不适用于Firefox Chromium。

在此示例中,您可以使用左右箭头键在图像之间循环。

<!DOCTYPE html>
<html>
<head>
<style>
body,html {
    height: 100%;
    margin: 0;
    padding: 0;
}   

.imgs {
    width:50%;
    height: 50%;
}
</style>
</head>
<body>

<img src="https://picsum.photos/200/100" style="display: block;" class="imgs">
<img src="https://picsum.photos/200/200" style="display: none;" class="imgs">

<script>
imgs = document.getElementsByClassname("imgs");

// THIS DOES NOT WORK
//~ function preloadImage(url)
//~ {
    //~ var img = new Image();
    //~ img.src = url;
//~ }

//preloadImage("myImg.jpg"); THIS DOES NOT WORK

document.onkeydown = checkKey; // directional keys

function checkKey(e) {

    if (document.activeElement.tagName != "INPUT") {

        e = e || window.event;

        switch (e.keyCode) {

            case 38:
                // up arrow
                break;

            case 40:
                // down arrow
                break;

            case 37:
                // left arrow
                imgs[0].style.display = "block";
                imgs[1].style.display = "none";
                break;

            case 39:
                // right arrow
                imgs[0].style.display = "none";
                imgs[1].style.display = "block";
                break;
        }
    }
}

</script>
</body>
</html>
i850528 回答:如何强制浏览器预加载带有显示的图像:无?

我已经找到了解决这个问题的方法。

我使用一个名为“ imgBuffer”的类来保存需要立即显示的图像。此类图像将包含需要立即显示的图像源,但不会显示图像本身。

棘手的部分是确保隐藏的图像位于屏幕上不会影响布局的位置。

请注意,隐藏的缓冲图像的宽度和高度与显示图像的宽度和高度匹配。如果您调整隐藏图像的尺寸,页面的行为可能会有所不同,例如在不必要时添加滚动条。

<!DOCTYPE html>
<html>
<head>
<style>
body,html {
    height: 100%;
    margin: 0;
    padding: 0;
}   

.imgs {
    width:50%;
    height: 50%;
}

.imgBuffer {
    position: absolute;
    height: 50%;
    width: 50%;
    visibility: hidden;
    z-index: -1;
}
</style>
</head>
<body>

<img src="https://picsum.photos/200/100" style="display: block;" class="imgs">
<img src="https://picsum.photos/200/200" style="display: none;" class="imgs">

<img src="https://picsum.photos/200/100" class="imgBuffer">
<img src="https://picsum.photos/200/200" class="imgBuffer">

<script>
imgs = document.getElementsByClassName("imgs");

// THIS DOES NOT WORK
//~ function preloadImage(url)
//~ {
    //~ var img = new Image();
    //~ img.src = url;
//~ }

//preloadImage("myImg.jpg"); THIS DOES NOT WORK

document.onkeydown = checkKey; // directional keys

function checkKey(e) {

    if (document.activeElement.tagName != "INPUT") {

        e = e || window.event;

        switch (e.keyCode) {

            case 38:
                // up arrow
                break;

            case 40:
                // down arrow
                break;

            case 37:
                // left arrow
                imgs[0].style.display = "block";
                imgs[1].style.display = "none";
                break;

            case 39:
                // right arrow
                imgs[0].style.display = "none";
                imgs[1].style.display = "block";
                break;
        }
    }
}

</script>
</body>
</html>
本文链接:https://www.f2er.com/2782392.html

大家都在问