window.onscrollend 이벤트

2008/01/02 10:08

window에서 scroll의 끝에 다다랐을때의 이벤트가 필요해서 만들었다.
세로 스크롤을 끝까지 내리면 발생하는 Event.
(창, 스크롤의 크기를 확인하는 function은 lightbox.js 로 부터 가져왔다.)

예제보기

코드는 아래와 같다.

// creates "onscrollend" event
window.onscroll = function() {
	var scroll = getPageScroll(); // function from lightbox.js
	var size = getPageSize(); // function from lightbox.js

	if((size[3] + scroll[1])/size[1] == 1) {
		if(window.onscrollend) {
			return window.onscrollend();
		}
		return false;
	}
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

댓글을 달아 주세요

  1. 티눈제거 2008/01/02 13:40  댓글주소  수정/삭제  댓글쓰기

    어.... 잘했네
    예제는...익스플로러 버그인가? 알럿이 두번씩 뜨네?
    오페라에서는 무한 알럿!!! 파폭, 네비게텨, 사파리(윈용)만 정상동작

  2. 컴미ㆀ 2008/01/02 20:23  댓글주소  수정/삭제  댓글쓰기

    Good~!
    사진탭에서 상단에 페이지단위로 볼 수 있도록 추가해주면 좋겠다.(날짜별 검색등도??)

    • fguy 2008/01/17 17:11  댓글주소  수정/삭제

      Flickr 사이트(http://www.flickr.com/photos/fguy/)들어가서 보면 되삼.
      내 홈페이지에 있는 건 그냥 Preview