87,910
社区成员
发帖
与我相关
我的任务
分享
for(var i=0; i<2; i++)
{
var bone,btwo,bthree,shotCol;
bone = document.getElementById("cols1").offsetHeight;
btwo = document.getElementById("cols2").offsetHeight;
bthree = document.getElementById("cols3").offsetHeight;
var div = document.createElement("div");
div.className = "img_box";
div.innerHTML = '<img src="images/'+Math.ceil(Math.random()*7)+'.jpg" width="300px" />';
if(bone - btwo <= 0 && bone - bthree <= 0)
{
shotCol = "cols1";
}
else if(btwo - bone <= 0 && btwo - bthree <= 0)
{
shotCol = "cols2";
}
else
{
shotCol = "cols3";
}
document.getElementById(shotCol).appendChild(div);
}
var myTimeOutI=0,img,forNum=8;
function appendImg()
{
img = new Image();
img.src = 'images/'+Math.ceil(Math.random()*7)+'.jpg';
img.style.width="300px";
img.style.height="auto";
img.onload = addElement;
}
function addElement()
{
var bone = document.getElementById("cols1").offsetHeight, btwo = document.getElementById("cols2").offsetHeight, bthree = document.getElementById("cols3").offsetHeight, colList = {}, _min = Math.min.apply(null,[bone,btwo,bthree]);
colList[bone] = "cols1";
colList[btwo] = "cols2";
colList[bthree] = "cols3";
var div = document.createElement("div");
div.className = "img_box";
div.appendChild(img);
var shotCol = colList[_min];
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
document.title += myTimeOutI;
if(myTimeOutI<forNum)
{
appendImg();
}
if(myTimeOutI>=forNum)
{
window.onscroll = showPinterest;
}
}
appendImg();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>瀑布流测试</title>
<script>
window.onscroll = showPinterest;
function showPinterest()
{
if(reachBottom())
{
var waitingBox = document.getElementById("waitingBox");
waitingBox.innerHTML = '<img src="images/loading.gif" />';
//setTimeout用来模仿后台取数据
window.onscroll = "";
setTimeout(function(){
document.getElementById("waitingBox").innerHTML = "";
var IEnum = checkIeVersion()
var forNum = 8;
if( IEnum && IEnum <=8 )
{
//IE8一下用这个方法,包括IE8 这里因为IE8一下貌似对js的image对象支持有问题,导致加载出错
var myTimeOutI=0;
function addElement()
{
var bone = document.getElementById("cols1").offsetHeight, btwo = document.getElementById("cols2").offsetHeight, bthree = document.getElementById("cols3").offsetHeight, colList = {}, _min = Math.min.apply(null,[bone,btwo,bthree]);
colList[bone] = "cols1";
colList[btwo] = "cols2";
colList[bthree] = "cols3";
var div = document.createElement("div");
div.className = "img_box";
var shotCol = colList[_min];
div.innerHTML = '<img src="images/'+Math.ceil(Math.random()*7)+'.jpg" width="300px" style="height:auto;" />';
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
if(myTimeOutI<forNum)
{
setTimeout(addElement,50);
}
if(myTimeOutI>=forNum)
{
window.onscroll = showPinterest;
}
}
addElement();
}
else
{
//IE9以上或者FF chrome浏览器用这个方法
var myTimeOutI=0,img;
function appendImg()
{
img = new Image();
img.src = 'images/'+Math.ceil(Math.random()*7)+'.jpg';
img.style.width="300px";
img.style.height="auto";
img.onload = addElement;
}
function addElement()
{
var bone = document.getElementById("cols1").offsetHeight, btwo = document.getElementById("cols2").offsetHeight, bthree = document.getElementById("cols3").offsetHeight, colList = {}, _min = Math.min.apply(null,[bone,btwo,bthree]);
colList[bone] = "cols1";
colList[btwo] = "cols2";
colList[bthree] = "cols3";
var div = document.createElement("div");
div.className = "img_box";
div.appendChild(img);
var shotCol = colList[_min];
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
document.title += myTimeOutI;
if(myTimeOutI<forNum)
{
appendImg();
}
if(myTimeOutI>=forNum)
{
window.onscroll = showPinterest;
}
}
appendImg();
}
},1000);
}
}
//检测IE的版本并返回版本数字比如9/8/7,非IE返回false
function checkIeVersion()
{
if(window.ActiveXObject)
{
var vName = navigator.appVersion,vNum = parseInt(vName.split(';')[1].replace(/[a-z]+|[A-Z]+|\s+/ig,""));
return vNum;
}
else
{
return false;
}
}
//检测是否到达浏览器底部,是返回true,否返回false
function reachBottom()
{
var isBottom,sHeight,sTop,cHeight;
if(document.compatMode == "BackCompat")
{
//document.compatMode == "BackCompat" IE标准兼容模式关闭
sHeight = document.body.scrollHeight;
sTop = document.body.scrollTop;
cHeight = document.body.clientHeight;
}
else
{
//document.compateMode == "CSS1Compat" IE标准兼容模式开启,此时IE同FF,和chrome稍有不同
sHeight = document.documentElement.scrollHeight;
sTop = document.documentElement.scrollTop==0? document.body.scrollTop: document.documentElement.scrollTop;
cHeight = document.documentElement.clientHeight;
}
isBottom = sHeight - sTop - cHeight;
if(isBottom == 0)
{
return true;
}
else
{
return false;
}
}
</script>
<style>
.cols_left{width:300px;margin-right:30px;height:auto;overflow:auto;float:left;}
.cols_right{width:300px;height:auto;overflow:auto;float:left;}
.img_box{width:300px;height:auto;overflow:hidden;margin-bottom:15px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);}
</style>
</head>
<body>
<div style="width:960px;margin-left:auto;margin-right:auto;height:auto;overflow:auto;">
<div id="cols1" class="cols_left">
<div class="img_box"><img src="images/1.jpg" width="300px" /></div>
<div class="img_box"><img src="images/2.jpg" width="300px" /></div>
<div class="img_box"><img src="images/7.jpg" width="300px" /></div>
</div>
<div id="cols2" class="cols_left">
<div class="img_box"><img src="images/4.jpg" width="300px" /></div>
<div class="img_box"><img src="images/6.jpg" width="300px" /></div>
<div class="img_box"><img src="images/1.jpg" width="300px" /></div>
</div>
<div id="cols3" class="cols_right">
<div class="img_box"><img src="images/3.jpg" width="300px" /></div>
<div class="img_box"><img src="images/5.jpg" width="300px" /></div>
<div class="img_box"><img src="images/2.jpg" width="300px" /></div>
</div>
</div>
<div id="waitingBox" style="margin-left:auto;margin-right:auto;"></div>
</body>
</html>
var IEnum = checkIeVersion()
if( IEnum && IEnum <=8 )
{
//IE8一下用这个方法,包括IE8 这里因为IE8一下貌似对js的image对象支持有问题,导致加载出错
var myTimeOutI=0;
function addElement()
{
var bone,btwo,bthree,shotCol;
bone = document.getElementById("cols1").offsetHeight;
btwo = document.getElementById("cols2").offsetHeight;
bthree = document.getElementById("cols3").offsetHeight;
var div = document.createElement("div");
div.className = "img_box";
div.innerHTML = '<img src="http://www.sishoo.test/tmp/'+Math.ceil(Math.random()*2)+'.jpg" width="300px" />';
if(bone - btwo <= 0 && bone - bthree <= 0)
{
shotCol = "cols1";
}
else if(btwo - bone <= 0 && btwo - bthree <= 0)
{
shotCol = "cols2";
}
else if(bthree - bone <= 0 && bthree - btwo <= 0)
{
shotCol = "cols3";
}
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
if(myTimeOutI<2)
{
setTimeout(addElement,50);
}
if(myTimeOutI>=2)
{
window.onscroll = showPinterest;
}
}
addElement();
}
else
{
//IE9以上或者FF chrome浏览器用这个方法
var myTimeOutI=0,img;
function appendImg()
{
img = new Image();
img.src = 'http://www.sishoo.test/tmp/'+Math.ceil(Math.random()*2)+'.jpg';
img.style.width="300px";
img.style.height="auto";
img.onload = addElement;
}
function addElement()
{
var bone = document.getElementById("cols1").offsetHeight, btwo = document.getElementById("cols2").offsetHeight, bthree = document.getElementById("cols3").offsetHeight, colList = {}, _min = Math.min.apply(null,[bone,btwo,bthree]);
colList[bone] = "cols1";
colList[btwo] = "cols2";
colList[bthree] = "cols3";
var div = document.createElement("div");
div.className = "img_box";
div.appendChild(img);
var shotCol = colList[_min];
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
document.title += myTimeOutI;
if(myTimeOutI<2)
{
appendImg();
}
if(myTimeOutI>=2)
{
window.onscroll = showPinterest;
}
}
appendImg();
}
这个是我写的IE版本检测函数:
//检测IE的版本并返回版本数字比如9/8/7,非IE返回false
function checkIeVersion()
{
if(window.ActiveXObject)
{
var vName = navigator.appVersion,vNum = parseInt(vName.split(';')[1].replace(/[a-z]+|[A-Z]+|\s+/ig,""));
return vNum;
}
else
{
return false;
}
}
那么,图片加载兼容性问题解决了,遗留的一个小问题就是貌似IE8及以下对 var img = new Image()支持有问题,所以在代码里我才加了IE版本号判断。这个问题不知道有哪位亲遇到过么? 一会我继续写这次的问题总结。//添加子元素
var myTimeOutI=0;
function addElement()
{
var bone,btwo,bthree,shotCol;
bone = document.getElementById("cols1").offsetHeight;
btwo = document.getElementById("cols2").offsetHeight;
bthree = document.getElementById("cols3").offsetHeight;
var div = document.createElement("div");
div.className = "img_box";
div.innerHTML = '<img src="images/'+Math.ceil(Math.random()*7)+'.jpg" width="300px" />';
if(bone - btwo <= 0 && bone - bthree <= 0)
{
shotCol = "cols1";
}
else if(btwo - bone <= 0 && btwo - bthree <= 0)
{
shotCol = "cols2";
}
else if(bthree - bone <= 0 && bthree - btwo <= 0)
{
shotCol = "cols3";
}
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
if(myTimeOutI<10)
{
setTimeout(addElement,50);
}
if(myTimeOutI>=10)
{
window.onscroll = showPinterest;
}
}
addElement();
[/quote]
之前本人提供的方法的确是错误的,是因为后面测试时候加载图片没加上Math.random清除缓存。
研究下后纠正下LZ的总结:
chrome没效果是因为for循环太快,你图片没加载完时候你的循环就可能已经结束了,所以肯定获取不到正确的高度。
但在ie下面你在把img加入dom后浏览器根据你的width自动默认先给你分配一个height(比如width=300时,分配height=321,width=600则height=643),所以你下次循环能取到一个height。fiefox没测,估计也是这样的。
根据这个结论2#的方法延迟500ms也并不能保证正确,因为图片加载时间很可能超过500ms,LZ你可以在chrome实验下图片加载时间比较长的情况。
var img;
var i = 0;
function appendDiv(){
var bone,btwo,bthree,shotCol;
bone = document.getElementById("cols1").offsetHeight;
btwo = document.getElementById("cols2").offsetHeight;
bthree = document.getElementById("cols3").offsetHeight;
var div = document.createElement("div");
div.appendChild(img);
if(bone - btwo <= 0 && bone - bthree <= 0)
{
shotCol = "cols1";
}
else if(btwo - bone <= 0 && btwo - bthree <= 0)
{
shotCol = "cols2";
}
else
{
shotCol = "cols3";
}
document.getElementById(shotCol).appendChild(div)
i++;
if(i<2){
appendImg();
}
}
function appendImg(){
img = new Image();
//img.src = 'images/'+Math.ceil(Math.random()*7)+'.jpg';
img.src = '1.jpg';
img.style.height = "300px";
img.onload = appendDiv
};
appendImg();
var bone = document.getElementById("cols1").offsetHeight,
btwo = document.getElementById("cols2").offsetHeight,
bthree = document.getElementById("cols3").offsetHeight,
colList = {},
_min = Math.min.apply(null,[bone,btwo,bthree]);
colList[bone] = 'cols1';
colList[btwo] = 'cols2';
colList[bthree] = 'cols3';
var shotCol = colList[_min],
div = document.createElement("div");
//添加子元素
var myTimeOutI=0;
function addElement()
{
var bone,btwo,bthree,shotCol;
bone = document.getElementById("cols1").offsetHeight;
btwo = document.getElementById("cols2").offsetHeight;
bthree = document.getElementById("cols3").offsetHeight;
var div = document.createElement("div");
div.className = "img_box";
div.innerHTML = '<img src="images/'+Math.ceil(Math.random()*7)+'.jpg" width="300px" />';
if(bone - btwo <= 0 && bone - bthree <= 0)
{
shotCol = "cols1";
}
else if(btwo - bone <= 0 && btwo - bthree <= 0)
{
shotCol = "cols2";
}
else if(bthree - bone <= 0 && bthree - btwo <= 0)
{
shotCol = "cols3";
}
document.getElementById(shotCol).appendChild(div);
myTimeOutI++;
if(myTimeOutI<10)
{
setTimeout(addElement,50);
}
if(myTimeOutI>=10)
{
window.onscroll = showPinterest;
}
}
addElement();