i've designed a program. With it i can move li tags on my page.
Code:
var click = 0;
var pos_x = 0;
var pos_y = 0;
var a = 0;
var out = [0, 0, 0, 0];
$(".movenode").mousedown(function() {
a = $(this).attr("id");
var b = $(this).position();
if(click == 0)
{
pos_x = b.left;
pox_y = b.top;
}
click = 1;
$(document).mousemove(function(e) {
if(click==1) {
$("#"+a).css({left:e.pageX-b.left-40, top:e.pageY-b.top-10});
}
});
});
$(document).mouseup(function(e) {
$("#"+a).css({left:pos_x, top:pos_y});
var d = 1;
while(d < 5) {
var pos = $("#"+d).position();
if(e.pageY > pos.top && e.pageY < pos.top + $("#"+d).height() && e.pageX > pos.left && e.pageX < pos.left + $("#"+d).width()) {
alert($("#"+d).attr("id"));
}
d++;
}
click = 0;
});
.movenode {
position:relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="movenode" id="1"/>1234
<li class="movenode" id="2"/>sadsdsadsad
<li class="movenode" id="3"/>sadsdsadsad
<li class="movenode" id="4"/>sadsdsadsad
This code works. But i have a little problem. When i add this code to my html file and set the elements in this order:
- HTML (just this 4 li)
- LOAD CSS TO html
- Load JS to html
This program works, but when I set this elements in this order:
- Load css to html
- Load js to html
- HTML (this 4 li)
this program doeasn't work. I cannot move this li. In fact, JS works because shows me alerts, but still i cannot move this li. I cannot us head, body etc. because its not working. What should i do to repair this bug?