天天看點

js設定html滾動條位置不變,JavaScript實作元素滾動條到達一定位置循環追加内容...

Document

body{

background-color: #eee;

}

#contents{

margin:30px auto;

width: 960px;

height:300px;

overflow:auto;

}

#list{

margin: 0;

padding: 0;

}

#list li{

color:#666;

list-style-type: none;

background-color: #ddd;

margin: 0;

margin-top:10px;

border-bottom: solid 1px #999;

text-align: center;

height:30px;

}

//擷取清單中的原有内容

window.οnlοad=function(){

var contents=document.getElementById("list").innerHTML;

//每被調用一次,就将網頁原有内容添加一份,這個大家可以寫自己要加載的内容或指令

function appendcontent(){

document.getElementById("list").innerHTML+=contents;

}

document.getElementById("contents").οnscrοll=function(){

//content實際高度,

var contentscrollHeight=document.getElementById("contents").scrollHeight;

//contentclientHeight可視區高度,

var contentclientHeight=document.getElementById("contents").offsetHeight;

//滾動條距頂部高度

var contentscrollTop=document.getElementById("contents").scrollTop;

//通過判斷滾動條的距離底部位置判斷手否加載内容

var height=contentclientHeight+100;

if(contentscrollTop+height>=contentscrollHeight){

if(document.getElementById("list").childNodes.length>=150){

if(document.getElementById("nodata")){

}else{

var nodata=document.createElement("div");

nodata.id="nodata";

nodata.style.height="50px";

nodata.style.textAlign="center";

nodata.style.lineHeight="50px";

nodata.style.borderTop="1px solid #eee";

nodata.innerHTML="我是有底線的";

nodata.style.backgroundColor="#fff";

document.getElementById("list").appendChild(nodata);

}

console.log(document.getElementById("list").childNodes.length)

return;

}else{

appendcontent();

}

}

};

}

  • 張朋1
  • 張朋2
  • 張朋3
  • 張朋4
  • 張朋5
  • 張朋6
  • 張朋7
  • 張朋8
  • 張朋9
  • 張朋10