开发者

jQuery positioning and appending a div on mouse move

开发者 https://www.devze.com 2023-03-30 15:56 出处:网络
Well i tried to do something like this : var old开发者_StackOverflow中文版X=0,oldY=0; $(\'body\').mousemove(function(e){

Well i tried to do something like this :

var old开发者_StackOverflow中文版X=0,oldY=0;
$('body').mousemove(function(e){
     $('.movestatus').text('mouse moved');
     var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
     $(".chords").text(clientCoords);
     var ap = $("<div class='k'></div>");
     ap.offset({ top: e.clientX, left: e.clientY });
         $("body").append(ap);
     oldX = e.clientX;
     oldY = e.clientY;
 });

Demo

Well this works BUT the added div are added a lot below where the mouse actually is and also not added always.

How can i fix this ?


One little thing you forgot:

.k {
    height:10px;
    width:10px;
    background:red;
    position:absolute; <-------
}


add position: absolute; to you class k

AND

ap.offset({ top: e.clientX, left: e.clientY });

should be:

ap.offset({ top: e.clientY, left: e.clientX });

example

0

精彩评论

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