if(!Array.from){ Array.from = function (el) { return Array.apply(this, el); } } var navBox = document.getElementById('navBox'), navBoxLIs = Array.from(navBox.getElementsByClassName('navboxs')), textBox = document.getElementById('textBox'), textBoxLIs = Array.from(textBox.getElementsByClassName('textBoxs')), box = document.getElementsByClassName('box')[0]; console.log(navBoxLIs);
var xyArr = [];
var A = {x: navBox.offsetWidth, y: 0}, B = {x: navBox.offsetWidth, y: navBox.offsetHeight}; navBoxLIs.forEach(function (item,index) { return item.index = index }) navBox.addEventListener('mouseover', moveGoing); navBox.addEventListener('mousemove', getXY); navBox.addEventListener('mouseout', clearTimer); function moveGoing(e) { if (e.target.nodeName.toUpperCase() === 'DIV') { var currentnavBox = e.target, flag; textBox.className = 'textBox show-block'; currentnavBox.timer = null; try { flag = inORout(xyArr[0], xyArr[2], A, B); } catch (err) {
} if (flag) { currentnavBox.timer = setTimeout(function () { toggle(textBoxLIs, currentnavBox.index); }, 300) } else { toggle(textBoxLIs, currentnavBox.index) } } }
function getXY(e) { if (e.target.nodeName.toUpperCase() === 'DIV') { var x = e.clientX - box.offsetLeft, y = e.clientY - box.offsetTop; xyArr.push({x:x, y:y}); if (xyArr.length > 3) { xyArr.shift(); } } }
function clearTimer(e) { if (e.target.nodeName.toUpperCase() === 'DIV') {
if (e.target.timer) { clearTimeout(e.target.timer); } } }
function toggle(eleArr, id) { eleArr.forEach(function (item,index) { navBoxLIs[index].className = 'navboxs'; item.className = 'textBoxs'; }) navBoxLIs[id].className = 'navboxs on'; eleArr[id].className = 'textBoxs show-block'; }
function inORout(moveStar, moveEnd, star, end) { var x = moveEnd.x, y = moveEnd.y, x1 = moveStar.x, y1 = moveStar.y, x2 = star.x, y2 = star.y, x3 = end.x, y3 = end.y, r1 = y - y1 - (y2 - y1) / (x2 - x1) * (x - x1), r2 = y - y2 - (y3 - y2) / (x3 - x2) * (x - x2), r3 = y - y3 - (y1 - y3) / (x1 - x3) * (x - x3); return (r1 * r2 * r3 < 0) && (r1 > 0); }
|