Last active
June 27, 2016 13:53
-
-
Save safhac/ff429ab77b3eff27f2988356efb9e433 to your computer and use it in GitHub Desktop.
listen to mouse move in window object instead of mouseover in list elements to overcome mouseout events from firing, calculate element index when knowing height
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<style> | |
body { | |
margin: 0 auto; | |
text-align: center; | |
} | |
#display { | |
display: block; | |
position: absolute; | |
top: 0; | |
right: 0; | |
width: 400px; | |
height: 100px; | |
border: solid thin lightgray; | |
} | |
#display p { | |
display: block; | |
border: solid thin darkred; | |
color: darkred; | |
} | |
.container { | |
width: 100%; | |
height: 100%; | |
display: block; | |
} | |
#drag-if { | |
display: inline-block; | |
width:300px; | |
height: 275px; | |
border: solid thin lightgray; | |
border-top: solid 100px black; | |
} | |
ul { | |
list-style: none; | |
} | |
.box, #ph { | |
display: inline-flex; | |
justify-content: space-around; | |
height: 35px; | |
width: 250px; | |
border: solid thin lightgreen; | |
margin: 5px 0; | |
} | |
.box a { | |
display: -webkit-box; | |
} | |
#ph { | |
border: solid thin red!important; | |
width: 125px; | |
border-radius: 3px; | |
background-color: darkcyan; | |
margin-left: 62px; | |
} | |
</style> | |
<script src="script.js" type="text/javascript"></script> | |
</head> | |
<!--onload="init();"--> | |
<body onload="init();"> | |
<div class="container"> | |
<div id="drag-if"> | |
<ul id="list"> | |
<li id="ph"></li> | |
<li class="box"> | |
<a href="#">link <header>header </header></a> | |
<span>span <label>label </label></span> | |
</li> | |
<li class="box"> | |
<a href="#">link <header>header </header></a> | |
<span>span <label>label </label></span> | |
</li> | |
<li class="box"> | |
<a href="#">link <header>header </header></a> | |
<span>span <label>label </label></span> | |
</li> | |
<li class="box"> | |
<a href="#">link <header>header </header></a> | |
<span>span <label>label </label></span> | |
</li> | |
<li class="box"> | |
<a href="#">link <header>header </header></a> | |
<span>span <label>label </label></span> | |
</li> | |
</ul> | |
</div> | |
</div> | |
</body> | |
</html> |
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 init() { | |
var boxes = document.getElementsByClassName('box'); | |
var ul = document.getElementById('list'); | |
var phe = document.getElementById('ph'); | |
var drag = document.getElementById('drag-if'); | |
var bounds = ul.getBoundingClientRect(); | |
var ph = phe.parentNode.removeChild(phe); | |
var min, height; | |
min = boxes[0].getBoundingClientRect().top; | |
height = boxes[0].getBoundingClientRect().height; | |
function isInside(node, target) { | |
for (; node != null; node = node.parentNode) | |
if (node == target) { | |
return true; | |
} | |
} | |
function getIndexInRange(mousePosition) { | |
return Math.round((mousePosition - min) / height); | |
} | |
window.addEventListener('mousemove', function () { | |
if(isInside(event.target, list)) { | |
ul.insertBefore(ph, boxes[getIndexInRange(event.pageY) - 1]); | |
} | |
else { | |
setTimeout(function () { | |
if (ul.contains(ph)) { | |
ul.removeChild(ph); | |
} | |
}, 500); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment