Code Monkey home page Code Monkey logo

mouse-wheel.js's Introduction

mouse-wheel.js

This is a javascript class which handles native mouse wheel events and fires an custom output event.

#how to use by default

index.html

<html>
  <head>
    <title>Mouse Wheel event handler</title>
    <script src="./build/MouseWheel.min.js"></script>
  </head>
  <body>
    <div id="myElement"></div>
  </body>
</html>
//create instance of the class
var myElement = document.getElementById('myElement');
var mw = new MouseWheel(myElement);

//listen for the event
mw.on('roll', function($event) {
  //Do something with $event for both scroll directions
});
mw.on('roll.up', function($event) {
  //Do something with $event for up scroll direction only
});
mw.on('roll.down', function($event) {
  //Do something with $event for down scroll direction only
});

#using AngularJS index.html

<div mw-roll="onScroll" ng-repeat="item in items">
  {{item}} scroll
</div>
<div mw-roll="onScroll" mw-direction="roll.up" ng-repeat="item in items">
  {{item}} scroll up
</div>
<div mw-roll="onScroll" mw-direction="roll.down" ng-repeat="item in items">
  {{item}} scroll down
</div>

main.js

  /**
   * This method will be called automatically when you scroll over the $target element
   * @param {Object} $event - this is OutputMouseWheel event
   * @param {Object} $target - this is target element wraped by angular.element()
   * */
  $scope.onScroll = function($event, $target) {
    //Do something with $target and $event data
  };

using requireJS

//main.js
define(['./build/MouseWheel', function(MouseWheel) {
  var myElement = document.getElementById('myElement');
  var mw = new MouseWheel(myElement);
  
  //listen for the event as native example
}];

using browserify

  var MouseWheel = require('./build/MouseWheel');
  
  var myElement = document.getElementById('myElement');
  var mw = new MouseWheel(myElement);
  
  //listen for the event as native example

Explore the output event

var OutputWheelEvent = {
  direction : String,// This is the scroll direction ['up', 'down']
  time      : Number,// Current timestamp of the event creation
  isNew     : Boolean,//This is true when the event is fired for the first time or scroll direction is changed
  delta     : Number// This is delta time in miliseconds between current and last output event
};

MouseWheel events

  • roll - triggered when scrolling
  • roll.up - triggered when scrolling up
  • roll.down - triggered when scrolling down

mouse-wheel.js's People

Contributors

corsa1r avatar

Stargazers

Zmei Zelen avatar Apostol Marinov avatar Veneta Kochovska avatar  avatar

Watchers

James Cloos avatar  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.