Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 15843

滚动条触事件的底获取及处理

$
0
0
1、获取滚动条触底事件;
2、屏蔽对一秒之内的多次触底的响应;
3、标记滚动条位置,实现刷新后的位置恢复


var height = 0;
var index=1;
var cal=1;

//获取滚动条上下滑动的距离
function getScrollTop(){
	var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
	if(document.body){
		bodyScrollTop = document.body.scrollTop;
	}
	if(document.documentElement){
		documentScrollTop = document.documentElement.scrollTop;
	}
		scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
		return scrollTop;
	}


//页面的总高度
function getScrollHeight(){
		var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
		if(document.body){
			bodyScrollHeight = document.body.scrollHeight;
		}
		if(document.documentElement){
			documentScrollHeight = document.documentElement.scrollHeight;
		}
		scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
		return scrollHeight;
	}


//浏览器显示窗口的高度
function getWindowHeight(){
		var windowHeight = 0;
		if(document.compatMode == "CSS1Compat"){
			windowHeight = document.documentElement.clientHeight;
		}else{
			windowHeight = document.body.clientHeight;
		}
		return windowHeight;
	}

	var timeout = false; 
	var result;
	window.onscroll = function(){
		//阀门算法思想:setTimeout处理完成后开启阀门,clearTimeout清理后关闭阀门
		if (timeout){clearTimeout(result);timeout = false;}
		result = setTimeout(function(){ 
			if(getScrollTop() + getWindowHeight() == getScrollHeight()){
				//alert("you are in the bottom!");
				//保存触底时滚动条滑动的高度,作为重置滚动条位置的依据
				height = getScrollTop();
				//alert(getScrollHeight());
				//alert(getScrollTop());
				//alert(getWindowHeight());
				if (cal < document.getElementById("mainForm:dataList:countPage").value){
					cal++;
					//alert(document.getElementById("mainForm:dataList:countPage").value);
					getNewDatas(20*index,20);
					index++;
				}
			}
		},1000);
		timeout = true;
	};


//重置滚动条的位置到上次滚动的位置
function setOnscroll(){
	 window.scrollTo(0, height);
}




已有 0人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐




Viewing all articles
Browse latest Browse all 15843

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>