Code Monkey home page Code Monkey logo

jquery.event.ue's Introduction

jquery.event.ue

Summary

Use this unified event plugin to applications that easily and seamlessly support mobile (touch) and desktop (mouse) environments.

The plugin recognizes mouse, keyboard, and touch motions that have the same intent and publishes unified events for them. For example, a long-press intent is expressed with a mouse by pressing-and-holding on the left mouse button for over half a second without substantially moving the mouse. The same intent on a touch device is expressed by placing a finger on the screen for over half a second without substantially moving the finger. This pugin recognizes both motions as having the same intent and publishes a single uheld event for either.

Here are the full list of intents supported by this unified event plugin:

  • utap - Mouse-click (mouse) / Finger tap (touch)
  • uheld - Long-mouse-press / Finger-press-and-hold
  • udrag - Mouse-click-and-drag / Finger Swipe
  • uhelddrag - Long-mouse-press-and-drag / Finger-press-and-hold-and-drag
  • zoom - Mouse-click-and-drag-Y + shift / Finger-pinch

This plugin is used in multiple commercial SPAs and is featured in the best-selling book Single Page Web Applications - JavaScript end-to-end, also available on Amazon.

Please see the ue-test.html file for a demonstration of the different intents. Expected behavior is expressed in the BR corner of the tiles. If you see any unexpected behavior while testing motions on your device please file a bug report so we can fix it!

Browser Support

This plugin works with jQuery 1.7.0+, and has been tested on most jQuery versions through 3.0.0. It works with the latest versions of popular browsers: Chrome 15+, Firefox 23+, Safari 5+, and IE 9+. IE9 requires edge settings:

  <head><meta http-equiv="X-UA-Compatible" content="IE=edge" />
  ....

Examples

    // bind to a mouse click or tap event
    $( '#utap' ) .on( 'utap',  onTap   );

    // bind to a long-press event
    $( '#uheld' ).on( 'uheld', onHeld  );

    // bind to zoom events
    $( '#uzoom' )
       .bind( 'uzoomstart', onZoomstart )
       .bind( 'uzoommove',  onZoommove  )
       .bind( 'uzoomend',   onZoomend   )
       ;

    // bind to drag events
    $( '#udrag' )
      .bind( 'udragstart', onDragstart )
      .bind( 'udragmove',  onDragmove  )
      .bind( 'udragend',   onDragend   )
      ;

    // bind to hold-drag events
    $('#uhelddrag')
      .bind('uheldstart', onDragstart )
      .bind('uheldmove',  onDragmove  )
      .bind('uheldend',   onDragend   )
      ;

Intents

See the ue-test.html file for example use of the event object.

The click intent

A click intent results in a utap event.

  • The Desktop motion is Left Mouse Button [LMB] down-up where the mouse-down time is less than the tap tolerance and the location movement is less than the drag tolerance.
  • The Touch motion is touchstart-move-end where the touch time is less than the tap tolerance and the location movement is less than the drag tolerance.

You may tweak the tap, drag, and long-press tolerances in the defaultOptMap at the top of the lib. The API will have a method to change these values in the future.

The long-press intent

A long-press intent results in a uheld event.

  • The Desktop motion is Left Mouse Button [LMB] down-up where the mouse-down time exceeds the long-press tolerance and the location movement is less than the drag tolerance.
  • The Touch motion is touchstart-move-end where the touch time exceeds the long-press tolerance and the location movement is less than the drag tolerance.

The drag intent

A drag intent results in a single udragstart event, one or more udragmove events, and a final udragend event.

  • The Desktop motion is LMB down - mouse-move - LMB up where the mouse-down time exceeds the tap tolerance and the location movement is greater than the drag tolerance.
  • The Touch is touchstart-move-end, where the touch time exceeds drag tolerance,

The event order is as follows:

  • udragstart - fires at the start of a drag
  • udragmove - fires each time the pointer moves
  • udragend - fires at the end of a drag

The held-drag intent

The held-drag intent results in a single uheldstart event, one or more uheldmove events, and a final uheldend event.

  • The Desktop motion is LMB down+hold - mouse-move - LMB up where the mouse-down time exceeds the held tolerance and the mouse has stayed within the drag tolerance, after which the location movement is greater than the drag tolerance.
  • The Touch is touchstart+hold-move-end, where the touch time exceeds the held tolerance and the touch as stayed within the drag tolerance, after which the touch location is greater than the the drag tolerance.

The event order is as follows:

  • uheldstart - fires at the start of a held-drag
  • uheldmove - fires each time the pointer moves
  • uheldend - fires at the end of a held-drag

The zoom intent

A zoom intent results in a single uzoomstart event, one or more uzoommove events, and a final uzoomend event.

  • The Desktop motion is shift+LMB down - move-mouse - LMB up where the mouse-down time exceeds the tap tolerance and the location movement is greater than the drag tolerance.
  • The Touch motion is two-point-touch - pinch - two-point-release.

The event order is as follows:

  • uzoomstart - fires at the start of zoom
  • uzoommove - fires as zoom amount changes
  • uzoomend - fires at end of zoom motion

Event Attributes

The event object for all ue events include these attributes in addition to those already provided by jQuery:

var attr_list = [
  event.px_start_x,    // X-position at start of motion
  event.px_start_y,    // Y-ibid
  event.px_current_x,  // X-position, current
  event.px_current_y,  // Y-ibid
  event.px_end_x,      // X-position for x at end of motion
  event.px_end_y,      // Y-ibid
  event.px_delta_zoom, // Zoom delta since last motion event
  event.px_delta_x,    // X-ibid
  event.px_delta_y,    // Y-ibid
  event.px_tdelta_x,   // X-delta since beginning of motion
  event.px_tdelta_y,   // Y-ibid
  event.orig_target,   // Element under cursor at start of motion
  event.elem_bound,    // Element on which event was bound
  event.elem_target,   // Element under the cursor for *this* event
  event.timeStamp,     // Timestamp of this event
  event.ms_elapsed,    // Elapsed time since start of motion
  event.ms_timestart,  // Start time of motion
  event.ms_timestop,   // top time of motion
];

Error handling

This code ignores bad input, and does not throw exceptions. As JS implementations are (finally) seeing improved exception handling, expect this to change in 2.x.

Namespacing

jQuery event namespacing is fully supported, as shown in this example:

$( '#msg' )
  .on( 'utap.mytap',  onTapMsg  )
  .on( 'uheld.mytap', onHeldMsg );

$( '#msg' ).unbind( '.mytap' );

Use libraries, not frameworks

This is a library that strives to be best-in-class. If you are considering using an SPA framework, please read Do you really want an SPA framework? first.

Release Notes

Copyright (c) 2013-2016

Michael S. Mikowski (mike[dot]mikowski[at]gmail[dotcom])

License

Dual licensed under the MIT or GPL Version 2 http://jquery.org/license

Version 1.3.2

Updated to ignore most input elements (input, textarea, select) by default. See the ignore_select setting.

Version 1.3.0

  • Removed all references to console object
  • Changed deprecated bind and unbind to on and off

Version 1.2.0-5

  • Changed default option key ignore_class to ignore_select
  • The value is now "" instead of ":input"
  • Updated README and published to npm (1.2.5)

Version 1.1.9

Updated ue-test.html to handle zooming correctly.

Version 1.1.8

Stopped preventDefault() from firing on events not controlled by the plugin.

Versions 1.1.0-7

No code changes. Updated npm keywords. Fixed typos. Bumped version number to represent maturity and stability. Redirected library discussion to blog post.

Versions 0.6.1

The default px_radius parameter was doubled from 5px to 10px. This made utap and uheld events work much better on smaller screens. Confirmed compatible with with jQuery 2.1.4, thus 1.7.0 - 2.1.4 appear safe.

Versions 1.1.x

No code changes. Updated npm keywords. Fixed typos. Bumped version number to represent maturity and stability. Redirected library discussion to blog post.

Versions 0.6.0

Fixed held logic, added px_tdelta_x and px_tdelta_y to attributes. Updated test page to include expected results.

Versions 0.5.0-0.5.2

Updated docs, jslint compatibility, removed cruft, updated test page to include zoom support (shift + mb1 hold + mouse up/down ).

Versions 0.4.1-3

Updated documentation and npm release.

Version 0.3.2

Confirmed compatible with jQuery 1.7.0 through 1.9.1.

Version 0.3.1

First release registered with jQuery plugins.

Testing

Tested with Android 3.2 Chrome, iOS5+ Safari, Chrome 15+, Firefox 23, and IE 9+.

See also

The Hammer touch library, jQuery mobile.

TODO

Support a wider range of intents

Contribute!

If you want to help out, like all jQuery plugins this is hosted at GitHub. Any improvements or suggestions are welcome! You can reach me at mike[dot]mikowski[at]gmail[dotcom].

End

jquery.event.ue's People

Contributors

aisera-mikowski avatar mmikowski avatar snapadmin avatar tomjoro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.event.ue's Issues

iOS simulator and preventDefault() [follow-up]

I wanted to follow up on the scroll area issues in the iOS simulator. Sorry so late with that.

The reason I originally found your plugin was to have a "touch hold" event. I ended up implementing my own, but wanted to mention how I handled the same problem in my code. Basically event.preventDefault() wasn't called right away. Instead, it is only the criteria is met for detecting touch hold (or some other behaviors we wanted to detect a special case) that the default event is blocked. Seems to work well that way in our use case, which is admittedly more specific than yours.

If you haven't found a solution for this issue yet, I'm happy to help out.

Pinch Zoom Location

Hi,
When pinch zooming on a touch device I'd like to know the coordinate of the point midway between the two touch points so I can hold that point fixed during the zoom. Is there a way to get this information with your plugin?

Thanks,
Wayne

site becomes unscrollable and unzoomable on a mobile device if I use a utap event

Greetings Mike,

Thanks for the awesome book and the good work.

I need to use the utap event for a div on a spa that I am building. I have the following code:

         var entryDiv = $("#entry"+statusUpdateMap.entryId);         
         entryDiv.unbind('utap.utap').bind('utap.utap', function () {
             spa.model.bdv.askForLogDetails(statusUpdateMap.entryId);
         });

If I remove this bit of code, the site is zoomable and scrollable. How do I specify that I want the page to be zoomable and scrollable on a mobile device when using this library?

touchcancel event is not managed

touchcancel event is not managed
On IOS7 on iPad, when I

  • start a drag
  • go on the bottom of the screen (without release the finger)
  • go on the top of the screen
    => the AirPlay panel opens
    => a touchcancel event fires
  • release finger
    => it's not possible to manage another drag

I've update your delivery to prevent this case : I add the same management for touchcancel than for touchend.

line 163:
$(document).bind( 'mouseup.__ue' , onMouse );
$(document).bind( 'touchend.__ue' , onTouch );
$(document).bind( 'touchcancel.__ue' , onTouch );

line 363:
$(document).unbind( 'mouseup.__ue');
$(document).unbind( 'touchend.__ue');
$(document).unbind( 'touchcancel.__ue');

line 670:
case 'touchstart' : handler_fn = fnMotionStart; break;
case 'touchmove' :
handler_fn = fnMotionMove;
event.preventDefault();
break;
case 'touchend' :
case 'touchcancel':handler_fn = fnMotionEnd; break;
default : handler_fn = null;

On touch, event.elem_target always = event.orig_target

For mouse events, the elem_target set, when dragging over another element, seems to be the element over which you have dragged.

On touch events, the elem_target seems to always be set as the element being dragged.

This makes it difficult to implement a drop. On mouse, when dragging and dropping, the elm_target is set the element onto which the item was dropped. But for touch, it always remains the original item.

Delegated events don't work

The following works:
jQueryMap.$contentPane.on ('click', '.spa-shell-menu-toggle', onTapToggle);
But the following doesn't:
jQueryMap.$contentPane.on ('utap', '.spa-shell-menu-toggle', onTapToggle);
after removing the toggle from the contentPane, appending some new html to the content pane and prepending again the toggle.
Please, let me know how can I fix this directly in the source unless (you plan to fix this yourself, which would be great).

Is it possible to overwrite the defaultOptMap?

Hi

Thanks you for the handy lib to get unified events. We have some behavior on Windows touch pads, that a short tap (not click) is always recorded around 200ms (motion_map.ms_elapsed). Because of this, I would like to change the max_tap_ms and min_held_ms values.

Is it possible to overwrite the values of the defaultOptMap and what's the right way to do that?

Thanks,
Thomas

Broken in IE8

None of the events fire on IE8, best I can tell.

What would be involved with fixing this? I might be able to help if you could point me in the right direction.

It would be great to have this fixed, seeing that IE8 market share has hovered around 20% for the last year, and will likely stick around a while longer.

The documentation also needs to be clarified as to which IE versions are supported.

it can not perform banding event when use utap to bind a button element

hi, I found somthing wrong with the utap event when I tried to bind an event to a button element, it can not trigger the callback,just like
$('.ok').bind('utap.utap',onTapLogin);
onTapLogin function won't be triggered; and if $(".ok") is a submit button , my login-page will exit automaticly.
i am confused and i don't know if i did something wrong
and last forgive my poor English

failing in iOS simulator

Was using this on an app, but the library is returning this error when in the iOS simulator (iOS 8.1): "TypeError: undefined is not an object (evaluating 'o_event.changedTouches')". The error is on Line 656, after a reference to "event.originalEvent" (this property obviously doesn't exist).

I'd help fix this but not sure yet what that code is trying to do.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.