1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| var wrap = document.getElementById(‘wrap’); var hoverDir = function(e) { var w = wrap.offsetWidth; var h = wrap.offsetHeight; var x = (e.clientX– wrap.offsetLeft–(w / 2)) * (w > h ? (h / w) : 1); var y = (e.clientY– wrap.offsetTop–(h / 2)) * (h > w ? (w / h) : 1); var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4; var eventType = e.type; var dirName = new Array(‘上方’, ’右侧’, ’下方’, ’左侧’); if (e.type == ‘mouseover’ || e.type == ‘mouseenter’) { wrap.innerHTML = dirName[direction] + ’进入 '; } else { wrap.innerHTML = dirName[direction] + ’离开 '; } } if (window.addEventListener) { wrap.addEventListener(‘mouseover’, hoverDir, false); wrap.addEventListener(‘mouseout’, hoverDir, false); } else if (window.attachEvent) { wrap.attachEvent(‘onmouseenter’, hoverDir); wrap.attachEvent(‘onmouseleave’, hoverDir); }
|