Git Product home page Git Product logo

multievent's Introduction

MultiEvent

MultiEvent enables you to use the right event types in touch, mouse, pointer or hybrid environments (Windows 8 devices).

It determines which events are supported in the browser's environment and flags the input used (touch or mouse), including if the input matches the event type. This check for a match is needed due to some mouse events incorrectly firing with a touch input.

Bonus: MultiEvent uses the UMD pattern to support AMD, as well as global implementations.

Give the demo a try.

Download MultiEvent

Behaviors

I've broken the events down onto behaviors: on, off, over, out. This covers the most basic interactions.

I'm not doing any checking for generic pointer events since IE is the only browser that supports them currently. That will need to change as more browsers implement pointer events.

Here's what events are supported in each bucket:

var behaviors = {
    on: {
        pointer:    'MSPointerDown',
        touch:      'touchstart',
        mouse:      'click'
    },
    off: {
        pointer:    'MSPointerUp',
        touch:      'touchend',
        mouse:      'mouseup'
    },
    over: {
        pointer:    'MSPointerOver',
        mouse:      'mouseover'
    },
    out: {
        pointer:    'MSPointerOut',
        mouse:      'mouseout'
    }
}

Usage

Instantiate a behavior type:

// returns and array of events  
var onMe = multiEvent( 'on' );

Setup the listener (jQuery is assumed):

$( '#some-element' ).on( onMe.events.join( ' ' ), function( e ) {  
    
    // Make sure in hybrid enviroments that  
    // only the first event hanlder is called.  
    e.preventDefault();  
  
    // Tell our instance to generate the needed info about the event.  
    onMe.resolve( e );  
  
    // Flags for input sources  
    if ( onMe.isTouch ) {  
        alert( 'Input source was a touch!' );  
    }  
    else if ( onMe.isMouse ) {  
        alert( 'Input source was a mouse!' );  
    }  
  
    // Did the input source match the event that was fired?  
    // Certain mouse events will incorrectly fire when the   
    // input was a touch.  
    if ( onMe.isMatch ) {  
        alert( 'All is good!' );  
    }  
    else {  
        alert( 'I feel wonky!' )  
    }  
});

Sources

Roadmap

multievent's People

Contributors

ryanfitzer avatar

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.