简体   繁体   English

在div的视口中溢出时异步加载图像:

[英]Loading images asynchronously when in viewport in div with overflow: scroll;

I have this structure for my content: 我的内容具有以下结构:

<div id="content" style="overflow:scroll;height:500px; width: 500px;">
    <div style="width:500px;height:100px;>
     <img src='http://graph.facebook.com/user1/picture?width=50&height=50'>
    </div>
    <div style="width:500px;height:100px;>
     <img src='http://graph.facebook.com/user2/picture?width=50&height=50'>
    </div>
    ...
    <div style="width:500px;height:100px;>
     <img src='http://graph.facebook.com/userN/picture?width=50&height=50'>
    </div>
</div>

So basically I have a div with overflow:scroll; 所以基本上我有一个div具有overflow:scroll; which contains pictures of all the Facebook user's friends. 其中包含所有Facebook用户朋友的图片。 For performance reasons, I think it's better to load images when they become visible in the viewport. 出于性能原因,我认为最好在视口中可见时加载图像。 I've tried everything including jquery.lazyload but I think because the images are inside my div with overflow:scroll the plugin doesn't function correctly. 我已经尝试了包括jquery.lazyload在内的所有内容,但我认为由于图像位于我的div中,并且overflow:scroll ,插件无法正常运行。

If you think its because of the divs, why not remove them: 如果您认为是由于div引起的,为什么不删除它们:

<div id="content" style="overflow:scroll;height:500px; width: 500px;">
     <img height='100' width='500' src='http://graph.facebook.com/user1/picture?width=50&height=50'>
     <img height='100' width='500' src='http://graph.facebook.com/user2/picture?width=50&height=50'>
    ...
</div>

I used this post to elaborate the following code. 我用这篇文章来阐述以下代码。

First of all change your html to get image only from the first: 首先,将您的html更改为仅从第一个获取图像:

<div id="content" style="overflow:scroll;height:100px; width: 100px;">
    <div style="width:500px;height:100px;">
        <img src='http://graph.facebook.com/user1/picture?width=50&height=50' />
    </div>
    <div style="width:500px;height:100px;">
        <img msrc='http://graph.facebook.com/user2/picture?width=50&height=50'/>
    </div>
    ...
    <div style="width:500px;height:100px;">
        <img msrc='http://graph.facebook.com/userN/picture?width=50&height=50'/>
    </div>
</div>

Then use this js to trigger scroll event from #content div and check if all the image is visible: 然后使用此js触发#content div中的滚动事件,并检查所有图像是否可见:

$(function(){
    var content = $("#content");

    function isScrolledIntoView(elem)
    {
        var divViewTop = content.scrollTop();
        var divViewBottom = divViewTop + content.height();

        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();

        return ((elemBottom <= divViewBottom) && (elemTop >= divViewTop));
    }

    content.scroll(function(e){
        content.find("img").each(function(i,e){
            if(isScrolledIntoView(e)){
                $(e).attr("src",$(e).attr("msrc"));
            }
        });
    });
});

Please don't use this in production, the code should be optimized first. 请不要在生产中使用此代码,应首先优化代码。

Here is a link to the jsfiddle : 这是jsfiddle的链接:

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

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