Unsorted list: load images or divs on demand(未排序列表:按需加载图像或 div)
问题描述
我使用 iScroll 制作了一个水平滑块.
I made a horizontal slider using iScroll.
我想显示很多图像(或 div),我像这样添加了这些图像:
I want to show a lot of images (or divs), and I added those images like this:
<ul>
<li style="background: url(fotos/PabloskiMarzo2008.jpg) no-repeat; background-size: 100%; -moz-background-size: 100%; -o-background-size: 100%; -webkit-background-size: 100%; -khtml-background-size: 100%; "></li>
...
<ul>
但是加载每张图片需要很多时间(而不是图片,我将使用图像映射或 div).
But it gets a lot of time to load every image (instead of images I'm going to use images map or divs).
如何按需加载图像?当用户向左滑动时,我想加载下一张图片.
How can I do that load images on demand? When user swipes to left, I want to load the next image.
推荐答案
//setup list of images to lazy-load, also setup variable to store current index in the array
var listOfImages = ['fotos/zero.jpg', 'fotos/one.jpg', 'fotos/infinity.jpg'],
imageIndex = 0,
myScroll = new iScroll('my-element');
//bind to the swipeleft event on the list
$('ul').bind('swipeleft', function () {
//append a new list-item to the list, using the `listOfImages` array to get the next source
//notice the `++` that increments the `imageIndex` variable
$(this).append($('li', { style : 'background: url(' + listOfImages[imageIndex++] + ') no-repeat; background-size: 100%; -moz-background-size: 100%; -o-background-size: 100%; -webkit-background-size: 100%; -khtml-background-size: 100%;' }));
//since the dimensions of your scroller have changed, you have to let iScroll know
myScroll.refresh();
});
您不妨将大部分 CSS 放在一个影响元素的类中,这样您就不必将其内联添加到每个元素中:
You might as well put most of that CSS in a class that affects the elements so you don't have to add it inline to each element:
JS--
$(this).append($('li', { style : 'background-image : url(' + listOfImages[imageIndex++] + ')' }));
CSS --
#my-element li {
background-repeat : no-repeat;
background-size : 100%;
-moz-background-size : 100%;
-o-background-size : 100%;
-webkit-background-size : 100%;
-khtml-background-size : 100%;
}
这篇关于未排序列表:按需加载图像或 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未排序列表:按需加载图像或 div
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 动态更新多个选择框 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01