JavaScript - mousemove event not triggered on iPad

2019-09-06 08:59发布

问题:

I worked for a while on E-learning project, where I had to script an HTML file that's loaded on a global framework. I used to use:

$('#myobject').on('mousedown' ... 'mousemove' ... 'mouseup' , function(){})

And it worked well everywhere (Chrome, IE, iOS ... etc)

Now I am working on a personal project, and in the browser everything is working well, but 'mousemove' does not seem to get triggered on iDevices (iPad, iPhone ... etc).

Here is simple code I wrote, that doesn't work on an iPad:

    $(window).load(function() {

        document.ontouchmove = function(e){
            e.preventDefault(); 
        }   

        $(document).on('mousemove',onmm);
        function onmm(e){
            var a = e.pageX;
            var b = e.pageY;
            $('#txt1').html(a);
            $('#txt2').html(b);
        }

    });

Any help ?

回答1:

Just use jquery.event.move. Move events provide an easy way to set up press-move-release interactions on mouse AND touch devices. So you don't have to worry which device your users have.

Source and examples: http://stephband.info/jquery.event.move/