如何强制Firefox预加载带显示的图像:无?

问题描述 投票:-1回答:1

Firefox不会加载带有显示的图像:只有在需要显示给用户时,才显示。

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

文件较小时,如果尚未加载图像,则会短暂闪烁。如果文件较大,则延迟会明显得多。

我曾尝试使用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>
html image browser preload
1个回答
0
投票

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

我使用一个名为“ 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>
© www.soinside.com 2019 - 2024. All rights reserved.