Using mousedown event on mobile without jQuery mobile?

前端 未结 3 1154
南方客
南方客 2020-11-30 05:40

I\'ve built a webapp, and for a little bit of polish, I wanted to add mousedown and mouseup handlers to swap out images (in this case, to make a button look like it\'s being

相关标签:
3条回答
  • 2020-11-30 06:13

    Have you considered styling your buttons using CSS instead? the :active state will be triggered when a user is clicking/touching the element. Here is an example:

    /* Default state */
    #button_img {
        background-image: url('button_off.png');
    }
    
    /* Clicked/touched state */
    #button_img:active { 
        background-image: url('button_on.png');
    }
    

    CSS will be much more performant and you will also be able to better separate concerns (display vs logic, etc).

    JSBin: http://jsbin.com/beyin/1/

    0 讨论(0)
  • 2020-11-30 06:23

    There is a way to get the vmouseup, vmousedown, vmousemove, vclick, etc. functionality of jQueryMobile without getting all the rest (and especially the side effects) of jquerymobile (i.e. enhancement, extra css, and the like)

    • Go to http://jquerymobile.com/download-builder/ (a tool for downloading a custom build of jquerymobile with only the components you need)
    • select ONLY "Virtual Mouse (vmouse) Bindings"
    • download it.

    The download will contain only a single .js files (in both minimized and uncompressed version). No css.

    Link this script in the head of your html after plain jquery, and use it like this:

    <head>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
      <script src="whatever/path/jquery.mobile.custom.min.js"></script>
      <script>
        $(function(){ // or replace this with window.onload for that matter
          // Your code here, e.g.
          $("#button_img").on("vmousedown", function() { 
            /*whatever*/
          });
          // CAUTION: this won't work (see note below):
          // $("#button_img").vmousedown(function(){/*whatever*/}); // WON'T WORK
        });
      </script>
    </head>
    

    NOTE: the methods .vmousedown(), .vmouseup(), etc. won't work. You have to bind the event listener with .on("vmousedown", ...). Not sure why: I guess this is because the part of jquerymobile that creates shortcut methods with the same name as the events is in some other module. Maybe it is possible to figure out which module it is and include it in the download, but I think it would force you to include other undesired dependencies.

    0 讨论(0)
  • 2020-11-30 06:28

    You're looking for touchstart and touchend. They are the events that vmousedown and vmouseup attempt to mimic.

    Here's an example:

    window.onload = function() {
        //preload mouse down image here via Image()
        $("#button_img").bind('touchstart', function(){
            $("#button_img").attr("src","button_on.png");
        }).bind('touchend', function(){
            $("#button_img").attr("src","button_off.png");
        });
    }
    

    This will work without any framework on any device that supports touch events. You could use something like Modernizr to do this test and if the device does not support touch events, bind to the regular desktop events.

    When you use touchstart/touchend/touchmove you get some interesting information, for instance how many touches are occurring at once, so you can detect if the user is scrolling or attempting to zoom.

    UPDATE

    Since the event object inside an event handler differs for touch events and mouse events, if you want to know the coordinates of the event either way, you can do something like this (the example below assumes Modernizr has been loaded):

    //determine which events to use
    var startEventType = 'mousedown',
        endEventType   = 'mouseup';
    
    if (Modernizr.touch === true) {
        startEventType = 'touchstart';
        endEventType   = 'touchend';
    }
    
    //bind to determined event(s)
    $("#button_img").bind(startEventType, function(event) {
    
        //determine where to look for pageX by the event type
        var pageX = (startEventType === 'mousedown')
                    ? event.pageX
                    : event.originalEvent.touches[0].pageX;
    
        ...
    })...
    

    UPDATE

    I was looking this over and it seems like you don't need to detect the event type before binding the event handler:

    //bind to determined event(s)
    $("#button_img").bind('mousedown touchstart', function(event) {
    
        //determine where to look for pageX by the event type
        var pageX = (event.type.toLowerCase() === 'mousedown')
                    ? event.pageX
                    : event.originalEvent.touches[0].pageX;
    
        ...
    })...
    

    If you are worried about receiving both events in quick succession you could use a timeout to throttle the event handler:

    //create timer
    var timer = null;
    
    //bind to determined event(s)
    $("#button_img").bind('mousedown touchstart', function(event) {
    
        //clear timer
        clearTimeout(timer);
    
        //set timer
        timer = setTimeout(function () {
    
            //determine where to look for pageX by the event type
            var pageX = (event.type.toLowerCase() === 'mousedown')
                        ? event.pageX
                        : event.originalEvent.touches[0].pageX;
    
            ...
    
        }, 50);
    })...
    

    Note: You can force mousedown and touchstart events in quick succession with developer tools but I'm not sure about the real world use case here.

    0 讨论(0)
提交回复
热议问题