Skip to content

Commit

Permalink
Natural scroll emulation is now optional (1.3.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Asselin committed Jul 7, 2015
1 parent d0d2c61 commit 4d6c43c
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store

/.idea
/bower_components
/node_modules
/node_modules
46 changes: 23 additions & 23 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module.exports = function(grunt) {
"use strict";
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
uglify: {
options: {
preserveComments: false,
banner: "/*! <%= pkg.main %> v<%= pkg.version %>\n" +
" * (c) 2014, Benoit Asselin contact(at)ab-d.fr\n" +
" * MIT License\n" +
" */\n"
},
build: {
src: "mousewheelStopPropagation.js",
dest: "mousewheelStopPropagation.min.js"
}
}
});
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.registerTask("default", ["uglify"]);
};
"use strict";

grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
uglify: {
options: {
preserveComments: false,
banner: "/*! <%= pkg.main %> v<%= pkg.version %>\n" +
" * (c) 2014, Benoit Asselin contact(at)ab-d.fr\n" +
" * MIT License\n" +
" */\n"
},
build: {
src: "mousewheelStopPropagation.js",
dest: "mousewheelStopPropagation.min.js"
}
}
});

grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.registerTask("default", ["uglify"]);

};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Options
| Name | Description | Type | Default |
|------|-------------|------|---------|
| `wheelstop` | The scroll was stopped. | Function | `null` |
| `emulateNaturalScrolling` | Emulate natural scrolling for IE | Boolean | `false` |
| `emulateNaturalScrolling` | Emulate natural scrolling for IE | Boolean | `true` |

4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jquery-mousewheel-stop-propagation",
"description": "This jQuery plugin can prevent the scrolling of parent elements, or stop the propagation with the mousewheel event listener.",
"version": "1.2.0",
"version": "1.3.0",
"license": "MIT",
"main": "mousewheelStopPropagation.js",
"author": {
Expand All @@ -21,4 +21,4 @@
"javascript",
"mousewheel"
]
}
}
16 changes: 8 additions & 8 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>$.mousewheelStopPropagation();</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=800, maximum-scale=1.2" />
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.0/normalize.min.css" />
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css" />
<style type="text/css">
#container {
width: 80%;
Expand Down Expand Up @@ -42,25 +42,25 @@
-webkit-overflow-scrolling: touch;
}
</style>

<!--[if lt IE 9]>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<!--<![endif]-->
<script src="mousewheelStopPropagation.js"></script>
<script>
$(function(){
$('#div-stop').mousewheelStopPropagation();
});
</script>

</head>
<body>
<div id="container">
<a href="https://github.com/basselin"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_left_gray_6d6d6d.png" alt="Fork me on GitHub" /></a>

<div id="div-normal">
<h2>Classic scrolling</h2>
<p>
Expand All @@ -73,7 +73,7 @@ <h2>Classic scrolling</h2>
Fugiat do laborum veniam dolore quis quis laboris irure excepteur dolor. Laborum consectetur consequat magna sint consequat et nostrud ad nisi ad tempor et velit et. Esse labore aute laboris sunt aliquip esse veniam consectetur excepteur ipsum. Consectetur cupidatat ut ex magna irure. Fugiat labore laborum ea incididunt tempor nisi occaecat ea do proident. Ipsum officia ad laboris eu ex consectetur commodo labore incididunt sunt. Officia pariatur amet consectetur proident deserunt velit mollit ad. Laborum aliquip fugiat adipisicing cupidatat deserunt duis ipsum et consequat non consectetur. Incididunt ad nisi consectetur incididunt dolor reprehenderit exercitation incididunt cupidatat. Ad eiusmod adipisicing reprehenderit consequat ex cillum ullamco. Et aute magna proident qui ea pariatur. Officia tempor cupidatat amet dolor sit. Tempor mollit dolore cillum fugiat. Esse cupidatat exercitation aliqua sint incididunt do laborum minim in nostrud cupidatat pariatur. Commodo duis commodo non duis ipsum excepteur enim culpa ea exercitation anim et est occaecat. Ea cillum consequat esse Lorem ullamco velit occaecat nostrud fugiat incididunt mollit. Et magna in incididunt irure culpa id laboris non. Ullamco dolore nostrud nostrud laboris fugiat dolor sunt dolor duis proident dolor exercitation ea eu. Mollit tempor nostrud ullamco duis pariatur. Pariatur velit id commodo est officia in nisi sint eiusmod dolore mollit. Aute eu sunt nisi in. Dolor enim ut ut elit consequat veniam deserunt exercitation labore. Cillum veniam esse enim occaecat veniam laborum officia voluptate. Esse est consectetur ex labore ea officia do. Nostrud sit eu sit ea Lorem cillum dolore veniam duis elit nulla. Est qui dolore do consequat nisi pariatur laborum. Nulla laboris elit magna est anim nulla. Eu ea id nisi magna nulla pariatur consectetur ad proident laboris anim pariatur pariatur nostrud. Est voluptate qui non commodo anim et incididunt occaecat officia cillum consequat aliquip proident. Ut nulla enim eu fugiat labore non ex nostrud irure nulla aliquip ullamco.
</p>
</div>

<div id="div-stop">
<h2>Prevent scrolling of parent element</h2>
<p>
Expand All @@ -86,7 +86,7 @@ <h2>Prevent scrolling of parent element</h2>
Fugiat do laborum veniam dolore quis quis laboris irure excepteur dolor. Laborum consectetur consequat magna sint consequat et nostrud ad nisi ad tempor et velit et. Esse labore aute laboris sunt aliquip esse veniam consectetur excepteur ipsum. Consectetur cupidatat ut ex magna irure. Fugiat labore laborum ea incididunt tempor nisi occaecat ea do proident. Ipsum officia ad laboris eu ex consectetur commodo labore incididunt sunt. Officia pariatur amet consectetur proident deserunt velit mollit ad. Laborum aliquip fugiat adipisicing cupidatat deserunt duis ipsum et consequat non consectetur. Incididunt ad nisi consectetur incididunt dolor reprehenderit exercitation incididunt cupidatat. Ad eiusmod adipisicing reprehenderit consequat ex cillum ullamco. Et aute magna proident qui ea pariatur. Officia tempor cupidatat amet dolor sit. Tempor mollit dolore cillum fugiat. Esse cupidatat exercitation aliqua sint incididunt do laborum minim in nostrud cupidatat pariatur. Commodo duis commodo non duis ipsum excepteur enim culpa ea exercitation anim et est occaecat. Ea cillum consequat esse Lorem ullamco velit occaecat nostrud fugiat incididunt mollit. Et magna in incididunt irure culpa id laboris non. Ullamco dolore nostrud nostrud laboris fugiat dolor sunt dolor duis proident dolor exercitation ea eu. Mollit tempor nostrud ullamco duis pariatur. Pariatur velit id commodo est officia in nisi sint eiusmod dolore mollit. Aute eu sunt nisi in. Dolor enim ut ut elit consequat veniam deserunt exercitation labore. Cillum veniam esse enim occaecat veniam laborum officia voluptate. Esse est consectetur ex labore ea officia do. Nostrud sit eu sit ea Lorem cillum dolore veniam duis elit nulla. Est qui dolore do consequat nisi pariatur laborum. Nulla laboris elit magna est anim nulla. Eu ea id nisi magna nulla pariatur consectetur ad proident laboris anim pariatur pariatur nostrud. Est voluptate qui non commodo anim et incididunt occaecat officia cillum consequat aliquip proident. Ut nulla enim eu fugiat labore non ex nostrud irure nulla aliquip ullamco.
</p>
</div>

<div class="content">
<h1>$.mousewheelStopPropagation()</h1>
<p>
Expand Down
132 changes: 66 additions & 66 deletions mousewheelStopPropagation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,76 @@
* (c) 2014, Benoit Asselin contact(at)ab-d.fr
* MIT License
*/

;(function($, window, undefined) {
'use strict';
'use strict';

var animOpt = { duration:200, easing:'linear' };

$.fn.mousewheelStopPropagation = function(options) {
options = $.extend({
// defaults
wheelstop: null, // Function
emulateNaturalScrolling: true // Boolean
}, options);

// Compatibilities
var ua = navigator.userAgent.toLowerCase(),
isMsIE = /(trident|msie)/.test(ua);
var docElt = document.documentElement,
mousewheelEventName = 'mousewheel';
if('onmousewheel' in docElt) {
mousewheelEventName = 'mousewheel';
} else if('onwheel' in docElt) {
mousewheelEventName = 'wheel';
} else if('DOMMouseScroll' in docElt) {
mousewheelEventName = 'DOMMouseScroll';
}
if(!mousewheelEventName) { return this; }

function mousewheelPrevent(event) {
event.preventDefault();
event.stopPropagation();
if('function' === typeof options.wheelstop) {
options.wheelstop(event);
}
}

var animOpt = { duration:200, easing:'linear' };
function emulateNaturalScrollIfNecessary($container, scrollTop) {
if (options.emulateNaturalScrolling) {
$container.stop(true).animate({scrollTop: scrollTop}, animOpt);
} else {
$container.get(0).scrollTop = scrollTop;
}
}

$.fn.mousewheelStopPropagation = function(options) {
options = $.extend({
// defaults
wheelstop: null, // Function
emulateNaturalScrolling: true
}, options);

// Compatibilities
var ua = navigator.userAgent.toLowerCase(),
isMsIE = /(trident|msie)/.test(ua);
var docElt = document.documentElement,
mousewheelEventName = 'mousewheel';
if('onmousewheel' in docElt) {
mousewheelEventName = 'mousewheel';
} else if('onwheel' in docElt) {
mousewheelEventName = 'wheel';
} else if('DOMMouseScroll' in docElt) {
mousewheelEventName = 'DOMMouseScroll';
}
if(!mousewheelEventName) { return this; }

function mousewheelPrevent(event) {
event.preventDefault();
event.stopPropagation();
if('function' === typeof options.wheelstop) {
options.wheelstop(event);
}
}
return this.each(function() {
var _this = this,
$this = $(_this);
$this.on(mousewheelEventName, function(event) {
var origiEvent = event.originalEvent;
var scrollTop = _this.scrollTop,
scrollMax = _this.scrollHeight - $this.outerHeight(),
delta = -origiEvent.wheelDelta;
if(isNaN(delta)) {
delta = origiEvent.deltaY;
}
var scrollUp = delta < 0;
if((scrollUp && scrollTop <= 0) || (!scrollUp && scrollTop >= scrollMax)) {
mousewheelPrevent(event);
} else if(isMsIE) {
if(scrollUp && -delta > scrollTop) {
emulateNaturalScrollIfNecessary($this, 0);
mousewheelPrevent(event);
} else if(!scrollUp && delta > scrollMax - scrollTop) {
emulateNaturalScrollIfNecessary($this, scrollMax);
mousewheelPrevent(event);
}
}
});
});
};

function emulateNaturalScrollIfNecessary($container, scrollTop) {
if (options.emulateNaturalScrolling) {
$container.stop(true).animate({scrollTop: scrollTop}, animOpt);
} else {
$container.get(0).scrollTop = scrollTop;
}
}

return this.each(function() {
var _this = this,
$this = $(_this);
$this.on(mousewheelEventName, function(event) {
var origiEvent = event.originalEvent;
var scrollTop = _this.scrollTop,
scrollMax = _this.scrollHeight - $this.outerHeight(),
delta = -origiEvent.wheelDelta;
if(isNaN(delta)) {
delta = origiEvent.deltaY;
}
var scrollUp = delta < 0;
if((scrollUp && scrollTop <= 0) || (!scrollUp && scrollTop >= scrollMax)) {
mousewheelPrevent(event);
} else if(isMsIE) {
if(scrollUp && -delta > scrollTop) {
emulateNaturalScrollIfNecessary($this, 0);
mousewheelPrevent(event);
} else if(!scrollUp && delta > scrollMax - scrollTop) {
emulateNaturalScrollIfNecessary($this, scrollMax);
mousewheelPrevent(event);
}
}
});
});
};

})(jQuery, window);


2 changes: 1 addition & 1 deletion mousewheelStopPropagation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jquery-mousewheel-stop-propagation",
"title": "$.mousewheelStopPropagation()",
"description": "This jQuery plugin can prevent the scrolling of parent elements, or stop the propagation with the mousewheel event listener.",
"version": "1.2.0",
"version": "1.3.0",
"license": "MIT",
"main": "mousewheelStopPropagation.js",
"author": {
Expand All @@ -13,8 +13,8 @@
"jquery": ">=1.11.0"
},
"devDependencies": {
"grunt": "^0.4.4",
"grunt-contrib-uglify": "^0.4.0"
"grunt": "~0.4.5",
"grunt-contrib-uglify": "~0.9.1"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 4d6c43c

Please sign in to comment.