2

So I have an object or div that is a square 10x10 pixels. I want to be able to click somewhere in the browser window that causes the div to gradually move towards the point I clicked.

Tesselate
  • 131
  • 1
  • 4
  • 8

3 Answers3

4

jQuery

$(document).click(function(event) {
    var x = event.pageX,
        y = event.pageY;

    $('div').animate({
        top: y,
        left: x
    }, 1000);
});

CSS

div {
    background: red;
    padding: 5px;
    position: absolute; 
}

HTML

<div>hello</div>

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
0

jQuery code

$("body").bind("click", function(e){
  var str = "( " + e.pageX + ", " + e.pageY + " )";
  $("span").text("Clicked  at " + str);
});

after getting this you need to update your div.style.left and div.style.top !

Sourav
  • 17,065
  • 35
  • 101
  • 159
0
$(document).click(function(event) {
    $('#divID').css({
       'position': 'absolute', 
       'left': event.clientX + document.body.scrollLeft, 
       'top': event.clientY + document.body.scrollTop });
});

Demo

Ketan Modi
  • 1,780
  • 1
  • 16
  • 28