日期:2014-05-16 浏览次数:20407 次
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <style> div { width:200px; height:200px; border:1px solid red; margin:100px; } </style> </head> <body> <div id="test"></div> <script> var $ = function(id){ return document.getElementById(id); }; var obj = $('test'); var h = obj.offsetHeight; var w = obj.offsetWidth; obj.onmouseover = function(e){ e = window.event || e; var x = e.offsetX || (getOffset(e).offsetX); var y = e.offsetY || (getOffset(e).offsetY); if( x <= w/2 ){ this.innerHTML = 'left' }else{ this.innerHTML = 'right' } if( y <= h/2 ){ this.innerHTML += ' top' }else{ this.innerHTML += ' bottom' } } function getOffset(e) { var target = e.target; if (target.offsetLeft == undefined) { target = target.parentNode; } var pageCoord = getPageCoord(target); var eventCoord = { x: window.pageXOffset + e.clientX, y: window.pageYOffset + e.clientY }; var offset = { offsetX: eventCoord.x - pageCoord.x, offsetY: eventCoord.y - pageCoord.y }; return offset; } function getPageCoord(element) { var coord = {x: 0, y: 0}; while (element) { coord.x += element.offsetLeft; coord.y += element.offsetTop; element = element.offsetParent; } return coord; } </script> </body> </html>
------解决方案--------------------
onmousemove 设置变量i 让move只执行2次 然后移除move事件
根据 执行的2次move事件的鼠标坐标位置 比对方向
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function addEventListener(control, eventName, fn) { if (window.attachEvent) { control.attachEvent('on' + eventName, function(e) {fn.call(control, e);}); } else if (window.addEventListener) { control.addEventListener(eventName, fn, false); } else { control['on' + eventName] = fn; } } function getPosition(target) { var left = 0, top = 0; do { left += target.offsetLeft || 0; top += target.offsetTop || 0; target = target.offsetParent; } while(target); return { left: left, top: top }; } function mousePosition(e) { var event = e || window.event; if (event.pageX || event.pageY) { return {x:event.pageX, y:event.pageY}; } return { x: event.clientX + document.body.scrollLeft - document.body.clientLeft, y: event.clientY + document.body.scrollTop - document.body.clientTop }; } var FocusType = { LEFT: "left", RIGHT: "r