查看: 2223|回复: 8
收起左侧

[其他] 下面这段js,怎么修改获取指定的img元素

[复制链接]
killers002
发表于 2015-9-3 12:22:08 | 显示全部楼层 |阅读模式
是一个图片延迟加载的js
结果这个页面上的所有img元素都应用了,这不是我想要的效果
我想要的是class为image样式的图片应用这个延迟加载
那么该如何修改下面这段js呢      

因本人对js一窍不通  麻烦好心人讲解的劲量详细些




<script>


var LazyLoad = new Class({

Implements: [Options,Events],

/* additional options */
options: {
range: 200,
elements: "img",
container: window,
mode: "vertical",
realSrcAttribute: "data-src",
useFade: true
},

/* initialize */
initialize: function(options) {

// Set the class options
this.setOptions(options);

// Elementize items passed in
this.container = document.id(this.options.container);
this.elements = $$(this.options.elements);

// Set a variable for the "highest" value this has been
this.largestPosition = 0;

// Figure out which axis to check out
var axis = (this.options.mode == "vertical" ? "y": "x");

// Calculate the offset
var offset = (this.container != window && this.container != document.body ? this.container : "");

// Find elements remember and hold on to
this.elements = this.elements.filter(function(el) {
// Make opacity 0 if fadeIn should be done
if(this.options.useFade) el.setStyle("opacity",0);
// Get the image position
var elPos = el.getPosition(offset)[axis];
// If the element position is within range, load it
if(elPos < this.container.getSize()[axis] + this.options.range) {
this.loadImage(el);
return false;
}
return true;
},this);

// Create the action function that will run on each scroll until all images are loaded
var action = function(e) {

// Get the current position
var cpos = this.container.getScroll()[axis];

// If the current position is higher than the last highest
if(cpos > this.largestPosition) {

// Filter elements again
this.elements = this.elements.filter(function(el) {

// If the element is within range...
if((cpos + this.options.range + this.container.getSize()[axis]) >= el.getPosition(offset)[axis]) {

// Load the image!
this.loadImage(el);
return false;
}
return true;

},this);

// Update the "highest" position
this.largestPosition = cpos;
}

// relay the class" scroll event
this.fireEvent("scroll");

// If there are no elements left, remove the action event and fire complete
if(!this.elements.length) {
this.container.removeEvent("scroll",action);
this.fireEvent("complete");
}

}.bind(this);

// Add scroll listener
this.container.addEvent("scroll",action);
},
loadImage: function(image) {
// Set load event for fadeIn
if(this.options.useFade) {
image.addEvent("load",function(){
image.fade(1);
});
}
// Set the SRC
image.set("src",image.get(this.options.realSrcAttribute));
// Fire the image load event
this.fireEvent("load",[image]);
}
});


});
});

</script>
翼风Fly
发表于 2015-9-3 12:53:57 | 显示全部楼层
只是简单看了一下。。。

[mw_shl_code=javascript,true]elements: "img",[/mw_shl_code]
你把这句改成
[mw_shl_code=javascript,true]elements: ".image",[/mw_shl_code]
看看行不?


话说楼主,图片延迟完全可以用JQuery啊
比方说有lazyloa.js这个JQuery的插件
随便搜的:http://zmingcx.com/jquery-lazy-to-achieve-picture.html

你把这句:
[mw_shl_code=javascript,true]$("img").lazyload({[/mw_shl_code]
改成:
[mw_shl_code=javascript,true]$(".image").lazyload({[/mw_shl_code]

建议楼主学习一下JQuery,并不复杂
有问题在这里找:
http://www.w3school.com.cn/
希望学习去这里:
http://www.imooc.com/

加油~
killers002
 楼主| 发表于 2015-9-3 13:13:26 | 显示全部楼层
本帖最后由 killers002 于 2015-9-3 13:17 编辑
翼风Fly 发表于 2015-9-3 12:53
只是简单看了一下。。。


本来是用的JQuery    结果JQuery和网站本身用的mootools冲突   虽然可以解决  但是我想就用一种库   

试了下 改成  [mw_shl_code=css,true]elements: ".image"[/mw_shl_code]      还是不行
翼风Fly
发表于 2015-9-3 13:28:56 | 显示全部楼层
本帖最后由 翼风Fly 于 2015-9-3 13:30 编辑
killers002 发表于 2015-9-3 13:13
本来是用的JQuery    结果JQuery和网站本身用的mootools冲突   虽然可以解决  但是我想就用一种库   
...


原来是这样。。。
mootools我不了解,抱歉帮不了太多
经过你这么描述,我搜索了 “mootools 延时 图片”,终于发现你的代码源于这里:
http://davidwalsh.name/js/lazyload
他也有解释:
elements: (defaults to "img") Images to consider for lazy loading.
那么应该就是我2楼说的
.image
就是这样吧。。。
killers002
 楼主| 发表于 2015-9-3 13:40:45 | 显示全部楼层
翼风Fly 发表于 2015-9-3 13:28
原来是这样。。。
mootools我不了解,抱歉帮不了太多
经过你这么描述,我搜索了 “mootools 延时 图 ...

我把实例源码传上来  麻烦你看看

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?快速注册

x
翼风Fly
发表于 2015-9-4 17:06:40 | 显示全部楼层
killers002 发表于 2015-9-3 13:40
我把实例源码传上来  麻烦你看看

抱歉了,水平有限,无能为力
现在唯一知道可行的是:
$$('样式名');
调试过,确实能选中要选的类
但是这段代码不知道怎么才能改好
killers002
 楼主| 发表于 2015-9-5 11:56:42 | 显示全部楼层
翼风Fly 发表于 2015-9-4 17:06
抱歉了,水平有限,无能为力
现在唯一知道可行的是:
$$('样式名');

多谢朋友了        我也是在网上找了好多,没找到办法    看来只能用JQuery 了     很担心和mootools共存会导致稳定方面的问题
翼风Fly
发表于 2015-9-5 12:05:38 | 显示全部楼层
killers002 发表于 2015-9-5 11:56
多谢朋友了        我也是在网上找了好多,没找到办法    看来只能用JQuery 了     很担心和mootools共存 ...

刚才搜了一下,$冲突可能要重点关注一下
网上也有对应的冲突解决方案

话说为什么不能全部图片延迟加载呢?
killers002
 楼主| 发表于 2015-9-5 12:34:29 | 显示全部楼层
本帖最后由 killers002 于 2015-9-5 12:37 编辑
翼风Fly 发表于 2015-9-5 12:05
刚才搜了一下,$冲突可能要重点关注一下
网上也有对应的冲突解决方案


普通的图片链接格式是 src=
而图片延迟加载的格式是 data-original=

全站图片延迟的话  就要吧所有的src改成data-original   如果是静态页面  改起来也快   
但是网站后台上传图片默认的就是src    这个如果要改  我就不知道怎么改了  

如果不改  那么src的图片就显示不出来
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2025-5-7 21:08 , Processed in 0.132742 second(s), 17 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

快速回复 客服 返回顶部 返回列表