整理了关于javascript中mouse坐标几段函数

function ___getPageScroll() {
	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;
	}
	return {xScroll: xScroll, yScroll: yScroll};
}

function ___getPageSize() {
	var xScroll, yScroll, pageWidth, pageHeight, windowWidth, windowHeight;
	
	xScroll = ___getPageScroll().xScroll;
	yScroll = ___getPageScroll().yScroll;
	
	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;
	}
	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) {
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize;
}

//获取鼠标当前所在的坐标
function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

//扩展原型对象的属性
Number.prototype.NaN0 = function () { return isNaN(this) ? 0 : this; }

function getPosition(e){
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth)).NaN0() : 0);
		top  += e.offsetTop  + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth)).NaN0() : 0);
		e     = e.offsetParent;
	}


	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	return {x: left, y: top};
}

//获取当前鼠标相对目标元素的偏移量
function getMouseOffset(target, ev){
	ev = ev || window.event;
	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
			console.log('mousePos.x:' + mousePos.x + ' , mousePos.y:' + mousePos.y);
			console.log('docPos.x:' + docPos.x + ' , docPos.y:' + docPos.y);
	return {x: mousePos.x - docPos.x, y: mousePos.y - docPos.y};
}


console.log(___getPageSize());

console.log('document.width:' + $(document).width() + ',' + $(window).width());
posted @ 2011-09-01 16:54  leamiko  阅读(295)  评论(0)    收藏  举报
document.getElementById('MySignature') && document.getElementById('MySignature').style.display = "none"; document.getElementById('MySignature') && document.getElementById('blog_post_info').style.display = "none";