Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跨浏览器事件处理 #3

Open
HecateDK opened this issue Apr 17, 2017 · 0 comments
Open

跨浏览器事件处理 #3

HecateDK opened this issue Apr 17, 2017 · 0 comments

Comments

@HecateDK
Copy link
Owner

HecateDK commented Apr 17, 2017

跨浏览器获取目标对象

IE获取对象为window.event.srcElement

function getTarget(e){
    if( e.target ){                      // chrome/firefox....
        return e.target;
    }else if( window.event.srcElement ){ // IE
        return window.event.srcElement;
    }
}

跨浏览器获取滚动条位置

IE:document.body.***

function getScrollP(){
	return {
		top: document.documentElement.scrollTop || document.body.scrollTop,
		left: document.documentElement.scrollLeft || document.body.scrollLeft;
	}
}

跨浏览器获取可是窗口大小

function getWindow(){
	if( typeof window.innerWidth != 'undefined' ){
		return {
			width: window.innerWidth,
			height: window.innerHeight
		}
	}else{
		return {
			width: document.documentElement.clientWidth,
			height:document.documentElement.clientHeight
		}
	}
}

跨浏览器获取屏幕坐标

function mousePosition(e){
	if( e.pageX || e.pageY ){
		return { x: e.pageX,y: e.pageY };
	} 
	return{
		x: e.clientX + document.body.scrollLeft - document.body.clientLeft,
		y: e.clientY + document.body.scrollTop - document.body.clientTop
	};
}
function mouseMove(e){
	e = e || window.event;
	document.getElementById('xxx').value = mousePosition(e).x;
	document.getElementById('yyy').value = mousePosition(e).y;
}
document.onmousemove = mouseMove;

demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant