1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script> <script src="top.js"></script> <script>
goTop();
var clock, star=1, len=6;
$(window).on("scroll",function(){ if(clock){ clearTimeout(clock) } clock=setTimeout(function(){ playShow() },300) });
playShow();
function playShow(){ console.log("开始判断") if(isVisble($(".getMore"))){ playAjax() } }
function isVisble(node){ var height=$(window).height(); var getH=node.offset().top; console.log (getH) var curH=$(window).scrollTop(); if(height+curH>=getH){ return true; }else{ return false; } }
function playAjax(){ console.log("发送数据"); $.ajax({ url:'http://platform.sina.com.cn/slide/album_tech', type: 'get', dataType: 'jsonp', jsonp: 'jsoncallback', data: { app_key: '1271687855', num:len, page:star }, success:function(node){ var tem=node.data; star++; onSuccess(tem); }, error:function(){ onError(); } }); }
function onSuccess(node){ console.log("处理数据"); var $nodeShow=concat(node); var defereds=[] $nodeShow.find("img").each(function(){ var defer=$.Deferred(); $(this).load(function(){ defer.resolve(); }) defereds.push(defer) }); $.when.apply(null,defereds).done(function() { console.log("图片加载完成") waterFull($nodeShow); }); }
function onError(){ console.log("数据接收出错") }
function concat(items){ var tpl="", $nodes; for(var i=0;i<items.length;i++){ tpl += '<li class="ct-main">'; tpl += ' <a href="'+ items[i].url +'" ><img src="' + items[i].img_url + '" alt=""></a>'; tpl += ' <h4 >'+ items[i].short_name +'</h4><hr>'; tpl += '<p >'+items[i].short_intro+'</p>'; tpl += '</li>'; } $nodes=$(tpl); $(".ct").append($nodes); console.log("添加到页面上") return $nodes; }
var sumHeight=[], ctWidth=$(".ct").width(), mainWidth=$(".ct-main").outerWidth(true), sum=Math.floor((ctWidth/mainWidth))
if(sumHeight.length ===0){ for(var i=0;i<sum;i++){ sumHeight.push(0); } }
function waterFull(node){ $(node).each(function(){ var idx=0, minWidth=sumHeight[0], $cur = $(this); for(var i=0;i<sum;i++){ if(sumHeight[i]<minWidth){ idx=i; minWidth=sumHeight[i]; } } $cur.css({ top:minWidth, left:mainWidth*idx, }) sumHeight[idx]=sumHeight[idx]+$cur.outerHeight(true) $('.ct').height(Math.max.apply(null,sumHeight)); }) } </script>
|