博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
整理了关于javascript中mouse坐标几段函数
阅读量:4946 次
发布时间:2019-06-11

本文共 3122 字,大约阅读时间需要 10 分钟。

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());

转载于:https://www.cnblogs.com/leamiko/archive/2011/09/01/2162216.html

你可能感兴趣的文章
简单的nios II 流水灯 硬件部分
查看>>
hdu 1533 Going Home 最小费用最大流
查看>>
wordpress the_date 方法 偶尔为空的问题
查看>>
分页中,计算总页数
查看>>
值得收藏的8个Web端组件库
查看>>
Windows&Word 常用快捷键
查看>>
JSON--JavaScript Object Notation
查看>>
使用VSCode 断点调试 js项目,html页面
查看>>
【转】Jmeter(三)-简单的HTTP请求(非录制)
查看>>
随波逐流
查看>>
20165329 实现mypwd
查看>>
探寻代码民工的根本原因
查看>>
日期转化为周次
查看>>
mysql设置指定ip远程访问连接实例
查看>>
iframe与父页面传值
查看>>
让windows cmd以及vim更漂亮
查看>>
iPhone各种机型尺寸、屏幕分辨率
查看>>
TCP三次握手和四次挥手协议
查看>>
monkeyrunner总结
查看>>
eshop7-mysql
查看>>