課程
/前端開發
/JavaScript
/瀑布流布局
我json數據 寫了24個圖片, 為什么滑動起來那么卡?
是因為哪些方面沒有進行優化嗎? 這樣的頁面用戶體驗很糟糕阿 。
2016-05-27
源自:瀑布流布局 3-2
正在回答
不會呀,我試了跟jquery代碼的引用有關,有的版本比較流暢,有的就比較卡
老師的這個函數是每次一觸發就把json里的數據全加載了??梢远x一個數來逐漸加載就不用遍歷循環了
$(window).on('load',function(){?waterfall();var ind = 0;var al = 0;?var dataInt = {"data":[{"src":"image/P_01.jpg"},{"src":"image/P_02.jpg"},{"src":"image/P_03.jpg"},{"src":"image/P_04.jpg"},{"src":"image/P_05.jpg"},{"src":"image/P_06.jpg"}]};?$(window).on('scroll',function(){????if(checkScrollSlide()){???if(ind==dataInt.data.length){????if(al==0){????alert("圖片已加載完畢!");????al=1;}????}else{???var oBox = $("<div>").addClass("box").appendTo($('#main'));???var oPic = $('<div>').addClass("pic").appendTo(oBox);???var oImg = $('<img>').attr('src',dataInt.data[ind].src).appendTo(oPic);???waterfall();???ind++;???if(ind==6){ind=0};???console.log(ind);}???}??});?})
? 那應該如何修改源碼?
$(document).ready(function()?{ ????$("#container").on('mouseenter?mouseleave',?'div.photo',?function(event)?{ ????????if?(event.type?==?'mouseenter')?{ ????????????$(this).find('div.details').fadeTo('fast',?0.5) ????????}?else?{ ????????????$(this).find('div.details').fadeOut('fast') ????????} ????}); ????$('#more-photos').click(function()?{ ????????$(this).trigger('nextPage',[true]); ????????return?false; ????}) ????function?checkscrollslide(){ ????????var?$height=$(window).scrollTop()+$(window).height(); ????????if?($('#container').height()<=$height)?{ ????????????$(document).trigger('nextPage'); ????????} ????} ????$(window).scroll(checkscrollslide).trigger('scroll') ????var?pagenum?=?1; ????$(document).on('nextPage',?function(event,scrolltovisible)?{ ????????var?$href?=?$('#more-photos').attr('href'); ????????$.get($href,?function(data)?{ ????????????var?$data=$(data).appendTo('#gallery'); ????????????if?(scrolltovisible)?{ ????????????????var?newtop=$data.offset().top; ????????????????$(window).scrollTop(newtop) ????????????} ????????????checkscrollslide() ????????},?'html') ????????if?(pagenum?<?20)?{ ????????????pagenum++; ????????????$('#more-photos').attr('href',?'pages/'?+?pagenum?+?'.html'); ????????}?else?{ ????????????$('#more-photos').remove(); ????????????$(document).off('nextPage'); ????????} ????}) });
因為每次加載新的,前面的節點還會被重新遍歷一邊。應該從當前新創建的節點開始往后遍歷。
舉報
瀑布流布局是網站比較流行的一種布局方式,教你實現三大方式
1 回答滑動滾動條后,頁面內容增加了,但是為什么滾動條的位置一直是置頂的scrollTop一直為0呢?求大神們幫忙看下~
1 回答為什么我一刷新圖片就堆疊到一起了
2 回答浮動的元素有嵌套啊,為什么
2 回答哪位大神幫忙看下為什么圖片出不來,滾動加載不了奇怪??
1 回答為什么我的圖片沒有加載出來
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-07-14
不會呀,我試了跟jquery代碼的引用有關,有的版本比較流暢,有的就比較卡
2016-06-03
老師的這個函數是每次一觸發就把json里的數據全加載了??梢远x一個數來逐漸加載就不用遍歷循環了
$(window).on('load',function(){
?waterfall();var ind = 0;var al = 0;
?var dataInt = {"data":[{"src":"image/P_01.jpg"},{"src":"image/P_02.jpg"},{"src":"image/P_03.jpg"},{"src":"image/P_04.jpg"},{"src":"image/P_05.jpg"},{"src":"image/P_06.jpg"}]};
?$(window).on('scroll',function(){
??
??if(checkScrollSlide()){
???if(ind==dataInt.data.length){
????if(al==0){
????alert("圖片已加載完畢!");
????al=1;}
????}else{
???var oBox = $("<div>").addClass("box").appendTo($('#main'));
???var oPic = $('<div>').addClass("pic").appendTo(oBox);
???var oImg = $('<img>').attr('src',dataInt.data[ind].src).appendTo(oPic);
???waterfall();
???ind++;
???if(ind==6){ind=0};
???console.log(ind);}
???}
??});
?})
2016-05-30
? 那應該如何修改源碼?
2016-05-28
因為每次加載新的,前面的節點還會被重新遍歷一邊。應該從當前新創建的節點開始往后遍歷。