Last active
February 26, 2019 08:27
-
-
Save okj579/a3ce778cfe3288e57b3d73be570b5c58 to your computer and use it in GitHub Desktop.
Initial Hover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function initialHover(window, elements, cssClass, firstEvent, finished) { | |
window.addEventListener('mousemove', update); | |
window.addEventListener('mouseover', update); | |
window.addEventListener('load', finish); | |
function update(e) { | |
console.log('update'); | |
if ( | |
(e.sourceCapabilities && e.sourceCapabilities.firesTouchEvents) || | |
(firstEvent && distance(firstEvent, e) > 50) | |
) { | |
return destroy(); | |
} | |
if (!firstEvent) { | |
firstEvent = e; | |
} | |
console.log(firstEvent, e, distance(firstEvent, e)); | |
if (finished) return; | |
while (elements.length) { | |
elements.pop().classList.remove(cssClass); | |
} | |
for (var current = e.target; current && current.type !== 'body'; current = current.parentElement) { | |
elements.push(current); | |
current.classList.add(cssClass); | |
} | |
} | |
function finish() { | |
if (finished) return; | |
if (!firstEvent) return setTimeout(finish, 100); | |
console.log('finish'); | |
finished = true; | |
window.removeEventListener('mouseover', update); | |
window.removeEventListener('load', finish); | |
window.addEventListener('mouseout', mouseout); | |
} | |
function mouseout(e){ | |
e.target.classList.remove(cssClass); | |
} | |
function destroy() { | |
console.log('destroy', elements); | |
window.removeEventListener('mousemove', update); | |
window.removeEventListener('mouseover', update); | |
window.removeEventListener('mouseout', mouseout); | |
window.removeEventListener('load', finish); | |
while (elements.length) { | |
elements.pop().classList.remove(cssClass); | |
} | |
} | |
function distance(e1, e2) { | |
return Math.sqrt(Math.pow(e1.clientX - e2.clientX, 2) + Math.pow(e1.clientY - e2.clientY, 2)); | |
} | |
})(this, [], 'initial-hover'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(e,t,n,o,i){function r(u){if(u.sourceCapabilities&&u.sourceCapabilities.firesTouchEvents||o&&a(o,u)>50)return function(){e.removeEventListener("mousemove",r),e.removeEventListener("mouseover",r),e.removeEventListener("mouseout",v),e.removeEventListener("load",s);for(;t.length;)t.pop().classList.remove(n)}();if(o||(o=u),!i){for(;t.length;)t.pop().classList.remove(n);for(var m=u.target;m&&"body"!==m.type;m=m.parentElement)t.push(m),m.classList.add(n)}}function s(){if(!i){if(!o)return setTimeout(s,100);i=!0,e.removeEventListener("mouseover",r),e.removeEventListener("load",s),e.addEventListener("mouseout",v)}}function v(e){e.target.classList.remove(n)}function a(e,t){return Math.sqrt(Math.pow(e.clientX-t.clientX,2)+Math.pow(e.clientY-t.clientY,2))}e.addEventListener("mousemove",r),e.addEventListener("mouseover",r),e.addEventListener("load",s)}(this,[],"initial-hover"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment