$(function() { // js部分開始
var scrolling =false; //判斷到底了
function isBottom() {
var scrollTop =0;
if ( document.documentElement && document.documentElement.scrollTop ) {
scrollTop = document.documentElement.scrollTop;
}else if ( document.body ) {
scrollTop =document.body.scrollTop;//滾動(dòng)條高度
}
var clientHeight =document.documentElement.clientHeight; //瀏覽器高度
var scrollHeight = Math.max( document.body.scrollHeight , document.documentElement.scrollHeight ); //內(nèi)容區(qū)域的高度
return Math.abs(scrollTop + clientHeight - scrollHeight) <=50; //高度差在50內(nèi)可以判斷到底了
}
//判斷到頂部了
function isTop(){
var scrollTop =0;
if ( document.documentElement && document.documentElement.scrollTop) {
scrollTop =document.documentElement.scrollTop;
} else if ( document.body) {
scrollTop =document.body.scrollTop;
}
return scrollTop==0;
}
function onFinish() {
scrolling =false;
}
function onScroll() { //定義當(dāng)前滾動(dòng)的區(qū)域
var scrollTop =me._param.config.scroller
?jQuery(me._param.config.scroller).scrollTop() //自己設(shè)置的滾動(dòng)區(qū)域
: jQuery(document).scrollTop(); //瀏覽器的滾動(dòng)區(qū)域
if(scrollTop >0&& !scrolling &&isBottom()) {
scrolling =true;
try{
//找到當(dāng)前頁面的onScroll方法
var scrollEvent =me.control().onScroll; //當(dāng)前頁面的onScroll方法,me.control() 是當(dāng)前頁面
if( scrollEvent ) scrollEvent(onFinish,true); //這里的true和下面的false分別是用來表示滾到底部(true)還是頂部(false)了
else onFinish();
}catch(e) {
onFinish();
}
}
if(scrollTop ==0&& !scrolling &&isTop()) {
scrolling =true;
try{
//找到當(dāng)前頁面的onScroll方法
var scrollEvent = me.control().onScroll;
if( scrollEvent ) scrollEvent(onFinish,false);
else onFinish();
}catch(e) {
onFinish();
}
}
}
window.addEventListener("scroll",onScroll,false);
});
其中scrollHeight表示滾動(dòng)條需要滾動(dòng)的距離,即可滾動(dòng)的總距離