mousemove event updating only once after mousedown

自闭症网瘾萝莉.ら 提交于 2020-01-15 05:42:06

问题


I'm having problems with mousemove in jquery. I want to check the coordinates of the mouse pointer AFTER a mousedown event AND a mousemove event, but it updates only once and the result is only the coordinates at the moment of the mousedown event.

I really need some advices, thanks :)

I post only a short extract of my original code here:

P.S. I tried to use "$(document).ready(function(){", but without success.

<!DOCTYPE html>
<head>
<title>jQuery Test</title>
   <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
   <script type="text/javascript" language="javascript">
    function my_func(event){
        $("#log1").html("mouse is down");
        var x = $(this);
        //some code
        $(document).ready(function(){
            x.on("mousemove", function(){
                $("#log1").html("x:"+event.pageX+" y:"+event.pageY);
            });
        });
    }

    $(document).ready(function(){
        $("button").on("mousedown",my_func); //it needs to be a named function because I will call it multiple times
    });
   </script>
</head>
<body>
   <button >TEST</button> 
   <div id="log1"></div>
</body>
</html>

回答1:


Change

x.on("mousemove", function(){

to

x.on("mousemove", function(event){

JsFiddle -> http://jsfiddle.net/6y83H/




回答2:


Did you forget to pass event on your mouse movement function?

Try this fiddle fiddle

function my_func(event) {
    $("#first").html("mouse is moving</br>x:" + event.pageX + " y:" + event.pageY);
}

function my_func1(event) {
    $("#first").html("mouse is now down</br>x:" + event.pageX + " y:" + event.pageY);
}

$(document).ready(function () {
    $("#first").on("mousemove", my_func);
    $("#first").on("mousedown", my_func1);
});


来源:https://stackoverflow.com/questions/23384483/mousemove-event-updating-only-once-after-mousedown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!