简体   繁体   中英

Keep images loaded in a scrolling DIV

I have many images inside of a scrolling DIV for an image gallery and the images are only loading when they become visible in the DIV after you scroll down. This is causing the DIV to freeze trying to load the images. Is there any way that I can fix this? I'm sure it would probably be javascript related.

Just my simple DIV.

<div style="width:275;height:400;overflow-x:scroll;">

content

</div>

You need to preload the images. Try this way if you don't want to use Javascript.

Yes, and in fact there are many related libs for this purpose. mostly for bandwidth saving purpose for old browsers, but will do the same work as you wanted here.

http://yuilibrary.com/yui/docs/imageloader/

http://www.appelsiini.net/projects/lazyload

at the same time, if your image are very big in size and you have a very long line of them, remember to hide (display: none) them after they become invisible from view.

Try using this lazy load script which will load the images before fixed number of pixels before they reach the visible area. This has many options

http://www.appelsiini.net/projects/lazyload

function expandDiv(idOfDivElement) {
    divObj = document.getElementById(idOfDivElement);
    var imageObj = document.createElement('img');
    imageObj.setAttribute('src', 'path/example_image.jpg');
    divObj.appendChild(imageObj);
    // your expand div code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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