Set Div position to Mouse position with jQuery

前端 未结 3 872
南方客
南方客 2021-01-04 10:06

I am trying to position my Div wherever the user clicks on my Image.

test is my Div, and myimg is my image.

Here is my JS:<

3条回答
  •  悲&欢浪女
    2021-01-04 10:47

    This works well enough for me, so your problem is likely elsewhere.

    HTML

    
    This is a test
    

    CSS

    #test {
        display: none;
        color: white;
    }
    

    JavaScript

    $(function() {
        $("#myimg").click(function(e) {
            var o = {
                left: e.pageX,
                top: e.pageY
            };
            $("#test").show(2000).offset(o);
        });
    });
    

    http://jsfiddle.net/mattball/haFMn/

提交回复
热议问题