Skip to content Skip to sidebar Skip to footer

Mouse Event Still Fired On Touch Event Even With Preventdefault Set

is there anything I misunderstood here? I often use code like the following in my site that I want to work either for desktop and iPad device: $('#someElement') .on( 'mousemove

Solution 1:

Check with user agent for ipad.simply use trenary operator for more simple code

var isIPad = navigator.userAgent.match(/iPad/i) != null;

$("#someElement").on(((isIPad)? "touchmove" : "mousemove" ), 
                         ((isIPad)? gotoIpad : gotoOthers ));

functiongotoIpad() {

      alert("I am ipad");
}

functiongotoOthers() {

      alert("I am not ipad");
}

Solution 2:

try this user agent

var isTouch = /iPad/i.test(navigator.userAgent);

            if(isTouch ){
            $("#someElement").on( "touchmove", function(e) {
                       // only desired code for ipad use here
                } );
            }
     else{
            $("#someElement")
                .on( "mousemove", function(e) {         
                    alert ( "I am still here" );
                    // undesired code for ipad
                });
            }

Post a Comment for "Mouse Event Still Fired On Touch Event Even With Preventdefault Set"