javascript瀑布流代码实例

javascript瀑布流代码实例:
现在瀑布流效果大行其道,各种网站都有应用,尤其是专业的图片类型的网站,本站在特效下载专区也有此应用,当然实现此效果的方法有多种,下面是一段瀑布流代码实例供大家参考。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>原生Js页面滚动延迟加载图片</title>
<style type="text/css">
*
{
  margin:0;
  padding:0
}
img.scrollLoading
{
  border:1px solid #ccc;
  display:block;
  margin-top:10px;
}
</style>
</head>
<body>
<div id="content"></div>
</body>
</html>
<script type="text/javascript">
var _CalF = {
  $:function(object){//选择器
    if(object === undefined ) return;
    var getArr = function(name,tagName,attr){
      var tagName = tagName || ‘*‘,
      eles = document.getElementsByTagName(tagName),
      clas = (typeof document.body.style.maxHeight === "undefined") ? "className" : "class";//ie6
      attr = attr || clas,
      Arr = [];
     for(var i=0;i<eles.length;i++){
       if(eles[i].getAttribute(attr)==name){
         Arr.push(eles[i]);
       }
     }
     return Arr;
   };
   if(object.indexOf(‘#‘) === 0){ //#id
     return document.getElementById(object.substring(1));
   }
   else if(object.indexOf(‘.‘) === 0){ //.class
     return getArr(object.substring(1));
   }
   else if(object.match(/=/g)){ //attr=name
     return getArr(object.substring(object.search(/=/g)+1),null,object.substring(0,object.search(/=/g)));
   }
   else if(object.match(/./g)){ //tagName.className
     return getArr(object.split(‘.‘)[1],object.split(‘.‘)[0]);
   }
 },
 getPosition : function(obj) { //获取元素在页面里的位置和宽高
   var top = 0,
   left = 0,
   width = obj.offsetWidth,
   height = obj.offsetHeight;
   while(obj.offsetParent){
     top += obj.offsetTop;
     left += obj.offsetLeft;
     obj = obj.offsetParent;
   }
   return {"top":top,"left":left,"width":width,"height":height};
  }
}; 

//添加图片list
var _temp = [];
for (var i = 1; i < 21; i ++) {
  _temp.push(‘<img class="scrollLoading" data-src="http://images.cnblogs.com/cnblogs_com/Darren_code/311197/o_‘ + i + ‘.jpg" src="http://images.cnitblog.com/blog/150659/201306/23160223-c81dd9aa9a2a4071a47b0ced2c9118bc.gif" /><br />图片‘ + i);
}
_CalF.$("#content").innerHTML = _temp.join(""); 

function scrollLoad(){
  this.init.apply(this, arguments);
}
scrollLoad.prototype ={
  init : function(className){
  var className = "img."+className,
  imgs = _CalF.$(className),
  that = this;
  this.imgs = imgs;
  that.loadImg();
  window.onscroll = function(){
    that.time = setTimeout(function(){
    that.loadImg();
    },400);
  }
},
loadImg : function(){
var imgs = this.imgs.reverse(), //获取数组翻转
len = imgs.length;
if(imgs.length===0){
  clearTimeout(this.time);
  return;
}
for(var j=len-1;j>=0;j--){ //递减
  var img = imgs[j],
  imgTop = _CalF.getPosition(img).top,
  imgSrc = img.getAttribute("data-src"),
  offsetPage = window.pageYOffset ? window.pageYOffset : window.document.documentElement.scrollTop,//滚动条的top值
  bodyHeight = document.documentElement.clientHeight; //body的高度
  if((offsetPage+bodyHeight/2) > (imgTop-bodyHeight/2)){
   img.src = imgSrc;
   this.imgs.splice(j,1);
  }
 }
 }
} 

var img1 = new scrollLoad("scrollLoading");
</script>

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=7975

更多内容可以参阅:http://www.softwhy.com/javascript/

时间: 2024-12-21 16:45:54

javascript瀑布流代码实例的相关文章

jquery实现的瀑布流代码实例

jquery实现的瀑布流代码实例:瀑布流现在很多网站都有应用,本站的特效下载专区也有应用,图片的加载会根据下拉条滚动实现的,实现了需要多少加载多少的功能,可以有效的提高网站的执行效率,下面是一段网络上的代码实例,供大家参考.代码实例如下: function lazyload(option){ var settings = { defObj: null, defHeight: 0 }; settings = $.extend(settings, option || {}); var defHeig

瀑布流代码,简洁版 带分页

接上一篇   瀑布流代码,简洁版 的功能之上添加分页的功能 Index.cshtml @using PagedList.Mvc @model PagedList.StaticPagedList<int> @{ ViewBag.Title = "瀑布流"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section header{ <script src="~/Scripts/jquery-ui

ios图片瀑布流代码

ios瀑布流,实现简单的瀑布流视图布局,可以显示网络图片,下拉刷新,上拉加载更多. 下载:http://www.huiyi8.com/sc/9087.html ios图片瀑布流代码,布布扣,bubuko.com

jquery 瀑布流代码 [简约]

HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>瀑布流代码</title> <link rel="stylesheet" type="text/css" href="static/css/style.css"> </

javascript瀑布流效果

<!doctype html> <html lang="en">  <head>   <meta charset="utf-8">   <title>瀑布流演示</title>   <link rel="stylesheet" type="text/css" href="pubu_css.css"/>   <scrip

jquery实现简单瀑布流代码

测试环境:ie8 ff13.0.1  chrome22 可以将分页获取的内容依次填入四个div中,瀑布流的分页可以以多页(比如5页)为单位二次分页,这样可以减少后台算法的复杂度 [html] view plaincopy <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&

瀑布流代码,简洁版

最近想实现数据的延迟加载,网上找一下有很的列子,看了Masonry的例子启发,自己写了一个很简单的例子(仅限于每列的宽固定高一致,并不算是真正意义的瀑布流). View页面. @{ ViewBag.Title = "瀑布流"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section header{ <script src="~/Scripts/jquery-ui-1.8.24.min.js"

javascript瀑布流

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>哇,瀑布流啊</title> <style type="text/css"> @media screen and (min-width:1550px){ .waterfall-container { width:1400px; } } @media screen a

javascript适合移动端的响应式瀑布流插件实例演示

在线预览 jQuery插件大全 实例代码 <div class="sucaihuo-container"> <div class="demo"> <div class="grid"> <div class="grid-item"></div> <div class="grid-item"></div> <div cla