开发者

mojQuery append a div on mouse move

开发者 https://www.devze.com 2023-03-31 08:00 出处:网络
I tried to achieve this by something like this : $(\'div.check\').mousemove(function(e){ // Mouse click + moving logic here

I tried to achieve this by something like this :

$('div.check').mousemove(function(e){

    // Mouse click + moving logic here
    $('.movestatus').text('mouse moved');
    var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
    $(".chords").text(clientCoords);
    var ap = $("<div>OO</div>");
    ap.offset({ top: e.c开发者_StackOverflow中文版lientX, left: e.clientY });
    $("div.check").appendTo(ap);
});

What is happening when i move the mouse in the div the main div gets dissapeared or on jsfiddle nothing happens. Demo

Where have i been going wrong Question: How can i bind jQuery to .check such that when i move the mouse it adds a div at the moved position.

Thanks


You need to use append, not appendTo. You also need to position absolute and swap the clientX and clientY.

 ap.css({ position:"absolute", top: e.clientY, left: e.clientX });
 $("div.check").append(ap);

Working demo: http://jsfiddle.net/ubgNH/


Add this for your HTML:

<script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>  
0

精彩评论

暂无评论...
验证码 换一张
取 消