简体   繁体   English

HTML加载图像:我的算法产生的流量过多吗?

[英]HTML Loading Images: Is my algorithm producing too much traffic?

today I've developed an algorithm which loads different images into a simple gallery. 今天,我开发了一种算法,可以将不同的图像加载到一个简单的图库中。 The script is creating image tags to "buffer" the images into. 该脚本正在创建图像标签以将图像“缓冲”到其中。 Then, JQuery is used to determine, whether the image finished loading. 然后,使用JQuery来确定图像是否完成加载。 If so, it hides my loading_animation.gif, removes the image tag and shows a div tag with the image set as it's background-image. 如果是这样,它将隐藏我的loading_animation.gif,删除image标签,并显示一个div标签,其中该图片设置为背景图片。 Basically I'm using the img tag to be able to use JQuery's .load() function. 基本上,我正在使用img标签来使用JQuery的.load()函数。 The div tag is used to display the image properly. div标签用于正确显示图像。

My question is: Is the image loaded twice from the net or is it cached by the browser? 我的问题是:图像是从网络加载两次还是由浏览器缓存? When I watched my script working, it seemed like the images got cached, but is it always the case? 当我看着脚本工作时,似乎图像已被缓存,但是总是这样吗? If people disable caching they'd load the images twice, wouldn't they? 如果人们禁用缓存,他们将加载两次图像,不是吗?

Here's my code: 这是我的代码:

CSS CSS

.imagewrapper .imageholder {
    width: 150px;
    height: 150px;
    background-size: cover;
    background-position: 50% 50%;
    background-repeat: no-repeat;
    float: left;
    overflow: hidden;
    display: none;
}
.imagewrapper .loading_animation {
    width: 150px;
    height: 150px;
    background-image: url(versuchsordner/img/loading_anim.gif);
    background-position: center;
    background-repeat: no-repeat;
    background-size: auto;
    float: left;
}
.image_preloader {
    display: none;
}

JQuery JQuery的

$(document).ready(function() {

    $('.imagewrapper .image_preloader').load(function() {

        $(this).siblings('.loading_animation').fadeOut(function() {
            $(this).siblings('.image_preloader').remove();
            $(this).siblings('.imageholder').fadeIn();
        });
    });         
});

PHP/HTML PHP / HTML

<div id="gallery">
        <?php
        $parser = new Parser("","basti",25);
        $entry_id = 87;
        $album = $parser->selectAlbumByEntryId($entry_id, "all");
        foreach ($album->pictures as $picture) {
            ?>
            <div class="imagewrapper">
                <img class="image_preloader" src="<?php echo $picture->originalPath; ?>" />
                <div class="imageholder" style="background-image:url('<?php echo $picture->originalPath; ?>');"></div>
                <div class="loading_animation"></div>
            </div>
            <?php
        }
        ?>
</div>

Your idea should work fine provided some conditions exist: 只要存在以下条件,您的想法就可以正常工作:

  • the server must set an expiration date (that is not in the past) 服务器必须设置一个到期日期(不是过去的日期)
  • client-side caching must be enabled and nothing interferes with it 必须启用客户端缓存,并且没有任何干扰

Edit: I think this method is better because it goes in line with the user's choice: if the user disables caching, than s/he wants to see the resources each time - and I think one should honour the user choice in this case. 编辑:我认为这种方法更好,因为它与用户的选择相符:如果用户禁用缓存,则他/她希望每次查看资源 -在这种情况下,我认为应该尊重用户的选择。 Don't forget that the user might be the developer himself checking out how the website loads the first time when things are not cached - something I do a lot. 不要忘记,用户可能是开发人员本人,当未缓存内容时,他会检查网站是如何首次加载的-我经常这样做。

In my opinion I think its not the best approach. 我认为这不是最好的方法。

If you like pre-loading images (that can be very tricky), you should use JS and declare 如果您喜欢预加载图像(这可能非常棘手),则应使用JS并声明

var img1023 = new Image();

If people disabled caching, well, its gonna download every time from server. 如果人们禁用了缓存,那么它每次都会从服务器下载。 But most users will get it in the cache (depends more on the cache tool installed on the web server.) 但是大多数用户会将其保存在缓存中(更多取决于Web服务器上安装的缓存工具。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM