zoukankan      html  css  js  c++  java
  • 整理了关于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.' + $(document).width() + ',' + $(window).width());
  • 相关阅读:
    【杭电】[1874]畅通工程续
    【杭电】[2544]最短路
    【杭电】[1087]Super Jumping! Jumping! Jumping!
    【HPU】[1689]MZY寻宝
    【杭电】[1495]非常可乐
    【杭电】[1242]Rescue
    【杭电】[1787]GCD Again
    【算法】欧拉函数——小于n的数中与n互质数的数目
    【HPU】[1738]Stack ? Queue ?
    【HPU】[1737]老王特警队
  • 原文地址:https://www.cnblogs.com/leamiko/p/2162216.html
Copyright © 2011-2022 走看看