在JavaScript中,使用jQuery实现延迟加载(Lazy Loading)通常是为了提高页面加载速度,只加载用户即将看到的图像或内容。以下是一个简单的例子,展示如何使用jQuery实现图片的延迟加载:
1. 确保你的页面中已经引入了jQuery库。
```html
```
2. 使用`img`标签的`data-src`属性来存储图片的真实路径,而不是直接在`src`属性中设置。
```html
```
3. 使用jQuery监听窗口的滚动事件,并检查每个图片是否进入视口。
```javascript
$(window).on('scroll', function() {
$('img[data-src]').each(function() {
var image = $(this);
if (image.visible(true)) {
image.attr('src', image.data('src'));
image.removeData('src');