1,注冊頁面滾動事件,window.onscroll = function(){ };
2,相關獲取頁面高度、滾動條位置、文檔高度的函數:
復制代碼 代碼如下:
//獲取滾動條當前的位置
function getScrollTop() {
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
}
else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
}
//獲取當前可是范圍的高度
function getClientHeight() {
var clientHeight = 0;
if (document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
}
else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
}
//獲取文檔完整的高度
function getScrollHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}
復制代碼 代碼如下:
<script>
window.onscroll = function () {
if (getScrollTop() + getClientHeight() == getScrollHeight()) {
alert("到達底部");
}
}
</script>
復制代碼 代碼如下:
var newnode = document.createElement("a");
newnode.setAttribute("href", "#");
newnode.innerHTML = "new item";
document.body.appendChild(newnode);
var newline = document.createElement("br");
document.body.appendChild(newline);
復制代碼 代碼如下:
<script>
window.onscroll = function () {
if (getScrollTop() + getClientHeight() == getScrollHeight()) {
var xmlHttpReq = null;
//IE瀏覽器使用ActiveX
if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
//其它瀏覽器使用window的子對象XMLHttpRequest
else if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
if (xmlHttpReq != null) {
//設置請求(沒有真正打開),true:表示異步
xmlHttpReq.open("GET", "/ajaxtest", true);
//設置回調,當請求的狀態發生變化時,就會被調用,傳遞參數xmlHttpReq
xmlHttpReq.onreadystatechange = function () { onajaxtest(xmlHttpReq); };
//提交請求
xmlHttpReq.send(null);
}
}
}
function onajaxtest(req) {
var newnode = document.createElement("a");
newnode.setAttribute("href", "#");
newnode.innerHTML = req.readyState + " " + req.status + " " + req.responseText;
document.body.appendChild(newnode);
var newline = document.createElement("br");
document.body.appendChild(newline);
}
</script>
新聞熱點
疑難解答
圖片精選