1.实现原理直接见代码,需要一张转圈的小图片,需要预览的所有图片默认的位置全是这张小图片,滚轮滚到原图需要出现的位置时候,预览加载替换小图片.实现效果
复制代码 代码如下:
<div style="height: 2500px;" id="txtScrollTop">
</div>
<div id="galleryList">
</div>
复制代码 代码如下:
var util = {
$: function (id) {
return document.getElementById(id);
},
getElementsByClassName: function (oElm, strTagName, strClassName) {
var arrElements = oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/-/g, "\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for (var i = 0; i < arrElements.length; i++) {
oElement = arrElements[i];
if (oRegExp.test(oElement.className)) {
arrReturnElements.push(oElement);
}
}
return arrReturnElements;
},
getXY: function (obj) {
var sumTop = 0, sumLeft = 0;
while (obj != document.body) {
sumLeft += obj.offsetLeft;
sumTop += obj.offsetTop;
obj = obj.offsetParent;
}
return { x: sumLeft, y: sumTop }
}
};
var GalleryViewer = {
GalleryContainer: {},
smallImgs: [], //小图片数组
orginalImgs: [], //原始图片数组
init: function (id, imgs, classname, smallImgSrc) {
this.GalleryContainer = util.$(id);
this.orginalImgs = imgs;
for (var i = 0; i < imgs.length; i++) { //追加所有预览小图片
var img = document.createElement("img");
img.src = smallImgSrc;
img.className = classname;
this.GalleryContainer.appendChild(img);
}
this.smallImgs = util.getElementsByClassName(util.$(id), "img", classname);
if (util.getXY(GalleryViewer.GalleryContainer).y < window.screen.height) {//如果初始在页面开始部位直接预览加载
this.preloadImages();
}
var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
if (document.attachEvent) //if IE (and Opera depending on user setting)
document.attachEvent("on" + mousewheelevt, GalleryViewer.orginalImgsAppear);
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, GalleryViewer.orginalImgsAppear, false);
},
preloadImages: function () {
for (var i = 0; i < GalleryViewer.orginalImgs.length; i++) {
(function (i) {
var imagePreload = new Image();
imagePreload.src = GalleryViewer.orginalImgs[i]; //预加载大图片
if (imagePreload.complete) { // 如果图片已经存在于浏览器缓存,直接调用回调函数
GalleryViewer.ImgsChange(i, imagePreload);
return; // 直接返回,不用再处理onload事件
}
imagePreload.onload = GalleryViewer.ImgsChange(i, imagePreload); //加载完成替换
})(i);
}
},
ImgsChange: function (i, obj) {
this.smallImgs[i].src = obj.src;
},
orginalImgsAppear: function () {
//alert(getXY(this.GalleryContainer).y - window.screen.height);
if (document.documentElement.scrollTop > util.getXY(GalleryViewer.GalleryContainer).y - window.screen.height) {
GalleryViewer.preloadImages();
}
}
};
(function () {
var imgs1 = ['../Content/images/gb_tip_layer.png', '../Content/images/gb_tip_layer.png', '../Content/images/gb_tip_layer.png'];
GalleryViewer.init("Div1", imgs1, "smallImgs1", "../Content/images/preload.gif");
//参数函数分别为div.id容器,原图地址数组,小图的类名,小图地址,具体图片地址请参照自己的项目
})();
复制代码 代码如下:
<div style="height: 2500px;" id="txtScrollTop">
</div>
<div id="galleryList">
</div>
复制代码 代码如下:
var util = {
$: function (id) {
return document.getElementById(id);
},
getElementsByClassName: function (oElm, strTagName, strClassName) {
var arrElements = oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/-/g, "\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for (var i = 0; i < arrElements.length; i++) {
oElement = arrElements[i];
if (oRegExp.test(oElement.className)) {
arrReturnElements.push(oElement);
}
}
return arrReturnElements;
},
getXY: function (obj) {
var sumTop = 0, sumLeft = 0;
while (obj != document.body) {
sumLeft += obj.offsetLeft;
sumTop += obj.offsetTop;
obj = obj.offsetParent;
}
return { x: sumLeft, y: sumTop }
}
};
var GalleryViewer = {
GalleryContainer: {},
smallImgs: [], //小图片数组
orginalImgs: [], //原始图片数组
init: function (id, imgs, classname, smallImgSrc) {
this.GalleryContainer = util.$(id);
this.orginalImgs = imgs;
for (var i = 0; i < imgs.length; i++) { //追加所有预览小图片
var img = document.createElement("img");
img.src = smallImgSrc;
img.className = classname;
this.GalleryContainer.appendChild(img);
}
this.smallImgs = util.getElementsByClassName(util.$(id), "img", classname);
if (util.getXY(GalleryViewer.GalleryContainer).y < window.screen.height) {//如果初始在页面开始部位直接预览加载
this.preloadImages();
}
var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
if (document.attachEvent) //if IE (and Opera depending on user setting)
document.attachEvent("on" + mousewheelevt, GalleryViewer.orginalImgsAppear);
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, GalleryViewer.orginalImgsAppear, false);
},
preloadImages: function () {
for (var i = 0; i < GalleryViewer.orginalImgs.length; i++) {
(function (i) {
var imagePreload = new Image();
imagePreload.src = GalleryViewer.orginalImgs[i]; //预加载大图片
if (imagePreload.complete) { // 如果图片已经存在于浏览器缓存,直接调用回调函数
GalleryViewer.ImgsChange(i, imagePreload);
return; // 直接返回,不用再处理onload事件
}
imagePreload.onload = GalleryViewer.ImgsChange(i, imagePreload); //加载完成替换
})(i);
}
},
ImgsChange: function (i, obj) {
this.smallImgs[i].src = obj.src;
},
orginalImgsAppear: function () {
//alert(getXY(this.GalleryContainer).y - window.screen.height);
if (document.documentElement.scrollTop > util.getXY(GalleryViewer.GalleryContainer).y - window.screen.height) {
GalleryViewer.preloadImages();
}
}
};
(function () {
var imgs1 = ['../Content/images/gb_tip_layer.png', '../Content/images/gb_tip_layer.png', '../Content/images/gb_tip_layer.png'];
GalleryViewer.init("Div1", imgs1, "smallImgs1", "../Content/images/preload.gif");
//参数函数分别为div.id容器,原图地址数组,小图的类名,小图地址,具体图片地址请参照自己的项目
})();
标签:
批量图片,预览加载
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“纯JS实现的批量图片预览加载功能”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。