博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mootools_MooTools简介LazyLoad
阅读量:2515 次
发布时间:2019-05-11

本文共 4044 字,大约阅读时间需要 13 分钟。

mootools

David Walsh MooTools LazyLoad

Once concept I'm very fond of is lazy loading. Lazy loading defers the loading of resources (usually images) until they are needed. Why load stuff you never need if you can prevent it, right? I've created LazyLoad, a customizable MooTools plugin that allows you to only load images when the user scrolls down near them.

我非常喜欢的概念是延迟加载。 延迟加载推迟了资源(通常是图像)的加载,直到需要它们为止。 如果可以防止,为什么装载不需要的东西,对吗? 我创建了LazyLoad,这是一个可自定义的MooTools插件,该插件可让您仅在用户向下滚动图像时才加载图像。

MooTools JavaScript类 (The MooTools JavaScript Class)

/* lazyload */var LazyLoad = new Class({		Implements: [Options,Events],		/* additional options */	options: {		range: 200,		image: 'blank.gif',		resetDimensions: true,		elements: 'img',		container: window	},		/* initialize */	initialize: function(options) {				/* vars */		this.setOptions(options);		this.container = $(this.options.container);		this.elements = $$(this.options.elements);		this.containerHeight = this.container.getSize().y;		this.start = 0;			/* find elements remember and hold on to */		this.elements = this.elements.filter(function(el) {			/* reset image src IF the image is below the fold and range */			if(el.getPosition(this.container).y > this.containerHeight + this.options.range) {				el.store('oSRC',el.get('src')).set('src',this.options.image);				if(this.options.resetDimensions) {					el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''});				}				return true;			}		},this);				/* create the action function */		var action = function() {			var cpos = this.container.getScroll().y;			if(cpos > this.start) {				this.elements = this.elements.filter(function(el) {					if((this.container.getScroll().y + this.options.range + this.containerHeight) >= el.getPosition(this.container).y) {						if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); }						if(this.options.resetDimensions) {							el.set({								width: el.retrieve('oWidth'),								height: el.retrieve('oHeight') 							});						}						this.fireEvent('load',[el]);						return false;					}					return true;				},this);				this.start = cpos;			}			this.fireEvent('scroll');			/* remove this event IF no elements */			if(!this.elements.length) {				this.container.removeEvent('scroll',action);				this.fireEvent('complete');			}		}.bind(this);				/* listen for scroll */		this.container.addEvent('scroll',action);	}});

Options for LazyLoad include:

LazyLoad的选项包括:

  • range: (defaults to 200) The amount of space from the container's bottom position that you want to look for images to load.

    范围:( 默认为200)要查找要加载的图像的容器底部位置的空间量。

  • image: (defaults to "blank.gif") The image to replace the original image.

    图像:( 默认为“ blank.gif”)用于替换原始图像的图像。

  • resetDimensions: (defaults to true) Removes the image's width and height attributes.

    resetDimensions:( 默认为true)删除图像的width和height属性。

  • elements: (defaults to "img") Images to consider for lazy loading.

    elements :( 默认为“ img”)要考虑延迟加载的图像。

  • container: (defaults to window) The container for which to look for images within.

    container :( 默认为window)在其中查找图像的容器。

Events for LazyLoad include:

LazyLoad的事件包括:

  • onComplete: Fires when all images have been loaded.

    onComplete:加载所有图像时触发。

  • onLoad: Fires on each individual image once it has been loaded.

    onLoad:加载每个单独的图像时触发。

  • onScroll: Fires when the container is scrolled.

    onScroll:滚动容器时触发。

样本用法 (The Sample Usage)

/* very simple usage */var lazyloader = new LazyLoad();/* more customized usage */var lazyloader = newLazyLoad({	range: 120,	image: 'logo.gif',	resetDimensions: false,	elements: 'img.lazyme'});

增强功能 (Enhancements)

This class isn't perfect -- it does not defer loading of images above the page's starting point. If you work on a website with a lot of anchor links and you'd like lazyloading of images above the fold, you'll need to add a bit to this class. Also note that due to , Safari will continue to load the originally images despite the plugin.

此类并非十全十美-不会延迟在页面起点上方加载图像。 如果您在具有许多锚点链接的网站上工作,并且希望在首屏上延迟加载图像,则需要向此类添加一些内容。 另请注意,由于 ,尽管有插件,Safari仍将继续加载原始图像。

Have any more suggestions? Share them!

还有其他建议吗? 分享他们!

翻译自:

mootools

转载地址:http://hkpwd.baihongyu.com/

你可能感兴趣的文章
mxnet系列 全连接层代码阅读
查看>>
0715
查看>>
USB各种模式 解释
查看>>
数据访问-----ADO.NET 小结和练习
查看>>
Linux lsof详解
查看>>
子组件给父组件传数据
查看>>
unix/linux下的共享内存、信号量、队列信息管理
查看>>
Hilbert先生旅馆的故事
查看>>
[家里蹲大学数学杂志]第394期分组求积分因子法
查看>>
[唐诗]送杜少府之任蜀州-王勃
查看>>
华中科技大学数学专业考研试题参考解答
查看>>
Objective-C数组初识
查看>>
appium(10)-iOS predictate
查看>>
程序的优化(PHP)
查看>>
Function.prototype.toString 的使用技巧
查看>>
Zookeeper+websocket实现对分布式服务器的实时监控(附源码下载)
查看>>
Oracle to_char 转换数值
查看>>
selinux-网络服务安全
查看>>
urllib
查看>>
NIOS II 中用结构体指示灯终于正常了
查看>>