【问题标题】:Loading image delayed display after loaded加载后加载图像延迟显示
【发布时间】:2019-02-04 05:19:37
【问题描述】:

所以我有这个 html / css / jQuery / js 代码,它们一起显示一个非常大的图像宽度。在加载和设置图像之前,基本元素应该是不可见的。然后通过一个小的淡入淡出使基础元素可见。

但是行为有点不同。 加载图像后,我可以看到小文本淡入,并且需要几秒钟才能弹出一次图像(而不是像从上到下出现的加载样式)

这是我使用的简化代码:

<script>
    $(function() {

        $("#base").hide();
        var baseHeight = $(window).height();
        var backLayerSRC = $('#img').attr('data-src');
        $('#base').height(baseHeight);

        var img = new Image();
        var imgWidth, imgHeight;

        img.onload = function() {
            imgHeight = this.height;
            imgWidth = this.width;

            var factor = 1 * baseHeight / imgHeight;
            totalWidth = Math.round(factor * imgWidth);

            currentWidth = totalWidth;
            $('#base').width(totalWidth);
            $('#img').attr('src', backLayerSRC);
            $('#img').height(baseHeight);

            $('#base').fadeIn(500);



        };

        img.src = backLayerSRC;

    });
</script>

<style>
    #base {
        position:absolute;
        left:0px;
        height:100%;
    }

    #base #img {
        position:absolute;
        left:0px;
        top:0px;
    }
</style>

<div id='base'>
    some tekst
    <img src='' id='img' data-src='path_to_very_large/image.png' />
</div>

现在,它怎么会随着图像不淡入呢?

这是一个带有图像的示例,请检查一下,以便清楚我的意思 http://jsfiddle.net/uz8mvtap/3

【问题讨论】:

  • 它看起来像你在图像加载时淡入。这意味着它的褪色,而图像可能不可见。尝试使用 img.load 代替该部分。
  • @JanWillemHuising 根据其他一些网站,您所说的不正确。加载图像后触发 img.onload。

标签: javascript jquery css image imagesloaded


【解决方案1】:

这可能取决于脚本加载后浏览器需要渲染图像的时间。

我用你的小提琴演奏了一点,然后想出了这个:

$(function() {

    $("#base").css({opacity: 0});
    var baseHeight = $(window).height();
    var backLayerSRC = $('#img').attr('data-src');
    $('#base').height(baseHeight);

    var img = new Image();
    var imgWidth, imgHeight;

    img.onload = function() {
        imgHeight = this.height;
        imgWidth = this.width;

        var factor = 1 * baseHeight / imgHeight;
        totalWidth = Math.round(factor * imgWidth);

        currentWidth = totalWidth;
        $('#base').width(totalWidth);
        $('#img').attr('src', backLayerSRC);
        $('#img').height(baseHeight);

        $('#base').delay(1000).animate({opacity: 1.0},500);



    };

    img.src = backLayerSRC;

});

基本上将不透明度用于此目的会更好,因为#base 会继续占用相同的空间,不会破坏页面。并且延迟是针对浏览器的,我认为渲染一个大图像需要时间,所以让我们给它。

【讨论】:

  • 是的,这听起来更像我需要的。太糟糕了,没有办法检测图像何时渲染?无论如何,谢谢,我现在会使用延迟
【解决方案2】:

你可以这样做。

$('<img />').load( function(){
  console.log('loaded');
}).attr('src', imgUrl);

加载后,它会调用一个回调,使用该回调来执行自定义功能。告诉我进展如何。

或者,顺便说一句。您可以将图像宽度和图像高度输入到自定义变量中,并在加载时进行比较,一旦达到您想要的宽度和高度,将其淡入。

【讨论】:

    【解决方案3】:

    您似乎稍微误解了代码的时间线:

    1. 页面加载,(文本在屏幕上可见)
    2. jQuery 启动,隐藏 #base(文本在屏幕上不可见)
    3. jQuery 淡入#base(文本再次出现在屏幕上)

    这就是为什么您在图像加载之前会短暂看到文本的原因 - 因为它最初并未隐藏。

    添加:

    display: none;

    在您的 #base 样式中,添加保持其余代码不变。


    在您添加上述 CSS 后,我还创建了一个替代 JavaScript 解决方案:

    $(function() {
        const base = $("#base"),
            img = $("#img"),
            backLayerSRC = img.attr('data-src'),
            baseHeight = $('#base').height();
    
        img.attr('src', backLayerSRC);
    
        img.one("load", () => {
            img.height(baseHeight).promise().done(() => { base.fadeIn(2500); });
        });
    });
    

    【讨论】:

    • 谢谢,但结果相同,我更新了我的问题中的链接,并用一个有效的链接来举例说明我的意思:jsfiddle.net/uz8mvtap/3
    • @Mart 请查看此 Codepen 以查看加载后淡入的图像:codepen.io/Tiarhai/pen/QVKxVx
    • @Mart 哎呀!我的代码中有一个错误,它立即调用了fadeIn() 函数。请查看上面编辑过的代码和 Codepen。
    • 这是个好方法,我不知道 promise()。但是它似乎并没有解决我电脑上的问题,它只是褪色时间更长。
    【解决方案4】:

    这是我创建的代码的工作示例:

    https://codebrace.com/editor/b16cabfee

    您的 baseHeight 变量未定义。 也许你应该改变

    $('#base').height(baseHeight);
    

    到

    var baseHeight = $('#base').height();
    

    更新:

    我已经更新了解决方案。在这个解决方案中,我将 div 包裹在文本和图像周围。环绕图像的 div 设置为 position:relative; 以防止图像(设置为绝对位置)覆盖文本。

    https://codebrace.com/editor/b16f33afb

    【讨论】:

    • 链接已损坏。可能您需要注册才能保存。
    • 所以您希望文本在图像加载之前保持可见?
    • 不可见,与图片不同时显示
    • 这样的?
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2013-10-24
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多