Skip to content

Commit

Permalink
Added prevent event method #5
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rmat1k committed May 3, 2015
1 parent 996c20b commit ba6fe21
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 57 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
var indicator = new WheelIndicator(document.querySelector('.element'));

indicator.on(function(e){
console.log(e.direction); // "up" or "down"
e.prevent(); // если необходимо
console.log(e.direction); // "up" или "down"
});
```

**jquery**
```javascript
$('.jquery').on('wheel-indicator', function(e){
e.prevent(); // если необходимо
console.log(e.direction); // "up" or "down"
});
```
7 changes: 3 additions & 4 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="../lib/wheel-indicator.js"></script>
<script src="../dist/jquery.wheel-indicator.js"></script>
<script src="../dist/jquery/jquery.wheel-indicator.js"></script>

<style>
.customZone, .jquery {
Expand All @@ -20,19 +20,18 @@

<br /><br />
jquery
<code>

</code>
<div class="jquery"></div>

<script>
var indicator = new WheelIndicator(document.querySelector('.customZone'));

indicator.on(function(e){
e.prevent();
document.querySelector('.customZone').innerHTML = document.querySelector('.customZone').innerHTML + e.direction + '<br />';
});

$('.jquery').on('wheel-indicator', function(e){
e.prevent();
$(this).append(e.direction + '<br />');
});
</script>
Expand Down
35 changes: 25 additions & 10 deletions dist/amd/wheel-indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ define('wheel-indicator', function () {
this.direction = '';
this.delta = '';
this.timer = '';
this.prevent = false;

var self = this;
addEvent(elem, eventWheel, function(event) {
processDelta(getDeltaY(event), self);
processDelta(event, self);

if (self.prevent) {
event.preventDefault();
}
});
}

Expand All @@ -22,13 +27,17 @@ define('wheel-indicator', function () {

on: function(cb){
this.callback = cb || function(){};

return this;
}
};

function triggerEvent(self){
var event = {};
function triggerEvent(self, event){
event.prevent = function(){
preventDefault.call(self);
};
event.direction = self.direction;
self.callback(event);
self.callback.call(this, event);
}

var getDeltaY = function(event){
Expand All @@ -44,8 +53,14 @@ define('wheel-indicator', function () {
getDeltaY(event);
};

function processDelta(deltaY, self) {
var stepAcceleration = 0, i;
function preventDefault(){
this.prevent = true;
}

function processDelta(event, self) {
var stepAcceleration = 0,
deltaY = getDeltaY(event),
i;

self.direction = deltaY > 0 ? self.direction = 'down' : self.direction = 'up';
self.delta = Math.abs(deltaY);
Expand All @@ -60,14 +75,15 @@ define('wheel-indicator', function () {
}, 200);

if(self.isStopped) {
triggerEvent(self);
triggerEvent(self, event);
self.isStopped = false;
self.isAcceleration = true;
stepAcceleration = 1;

self.memoryAcceleration.shift();
self.memoryAcceleration.push(stepAcceleration);
} else {

self.last5values.shift();
self.last5values.push(self.delta);

Expand All @@ -91,7 +107,7 @@ define('wheel-indicator', function () {
//Произошло ускорение
if(!self.isAcceleration) {
self.isAcceleration = true;
triggerEvent(self);
triggerEvent(self, event);
}
}
}
Expand All @@ -100,8 +116,7 @@ define('wheel-indicator', function () {
function addEvent(elem, type, handler){
if(window.addEventListener) {
elem.addEventListener(type, handler, false);
}
if(window.attachEvent) {
} else if (window.attachEvent) {
elem.attachEvent('on' + type, handler);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/amd/wheel-indicator.min.js

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

35 changes: 25 additions & 10 deletions dist/commonjs/wheel-indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ var WheelIndicator = (function() {
this.direction = '';
this.delta = '';
this.timer = '';
this.prevent = false;

var self = this;
addEvent(elem, eventWheel, function(event) {
processDelta(getDeltaY(event), self);
processDelta(event, self);

if (self.prevent) {
event.preventDefault();
}
});
}

Expand All @@ -21,13 +26,17 @@ var WheelIndicator = (function() {

on: function(cb){
this.callback = cb || function(){};

return this;
}
};

function triggerEvent(self){
var event = {};
function triggerEvent(self, event){
event.prevent = function(){
preventDefault.call(self);
};
event.direction = self.direction;
self.callback(event);
self.callback.call(this, event);
}

var getDeltaY = function(event){
Expand All @@ -43,8 +52,14 @@ var WheelIndicator = (function() {
getDeltaY(event);
};

function processDelta(deltaY, self) {
var stepAcceleration = 0, i;
function preventDefault(){
this.prevent = true;
}

function processDelta(event, self) {
var stepAcceleration = 0,
deltaY = getDeltaY(event),
i;

self.direction = deltaY > 0 ? self.direction = 'down' : self.direction = 'up';
self.delta = Math.abs(deltaY);
Expand All @@ -59,14 +74,15 @@ var WheelIndicator = (function() {
}, 200);

if(self.isStopped) {
triggerEvent(self);
triggerEvent(self, event);
self.isStopped = false;
self.isAcceleration = true;
stepAcceleration = 1;

self.memoryAcceleration.shift();
self.memoryAcceleration.push(stepAcceleration);
} else {

self.last5values.shift();
self.last5values.push(self.delta);

Expand All @@ -90,7 +106,7 @@ var WheelIndicator = (function() {
//Произошло ускорение
if(!self.isAcceleration) {
self.isAcceleration = true;
triggerEvent(self);
triggerEvent(self, event);
}
}
}
Expand All @@ -99,8 +115,7 @@ var WheelIndicator = (function() {
function addEvent(elem, type, handler){
if(window.addEventListener) {
elem.addEventListener(type, handler, false);
}
if(window.attachEvent) {
} else if (window.attachEvent) {
elem.attachEvent('on' + type, handler);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/commonjs/wheel-indicator.min.js

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

35 changes: 25 additions & 10 deletions dist/es6/wheel-indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ var WheelIndicator = (function() {
this.direction = '';
this.delta = '';
this.timer = '';
this.prevent = false;

var self = this;
addEvent(elem, eventWheel, function(event) {
processDelta(getDeltaY(event), self);
processDelta(event, self);

if (self.prevent) {
event.preventDefault();
}
});
}

Expand All @@ -21,13 +26,17 @@ var WheelIndicator = (function() {

on: function(cb){
this.callback = cb || function(){};

return this;
}
};

function triggerEvent(self){
var event = {};
function triggerEvent(self, event){
event.prevent = function(){
preventDefault.call(self);
};
event.direction = self.direction;
self.callback(event);
self.callback.call(this, event);
}

var getDeltaY = function(event){
Expand All @@ -43,8 +52,14 @@ var WheelIndicator = (function() {
getDeltaY(event);
};

function processDelta(deltaY, self) {
var stepAcceleration = 0, i;
function preventDefault(){
this.prevent = true;
}

function processDelta(event, self) {
var stepAcceleration = 0,
deltaY = getDeltaY(event),
i;

self.direction = deltaY > 0 ? self.direction = 'down' : self.direction = 'up';
self.delta = Math.abs(deltaY);
Expand All @@ -59,14 +74,15 @@ var WheelIndicator = (function() {
}, 200);

if(self.isStopped) {
triggerEvent(self);
triggerEvent(self, event);
self.isStopped = false;
self.isAcceleration = true;
stepAcceleration = 1;

self.memoryAcceleration.shift();
self.memoryAcceleration.push(stepAcceleration);
} else {

self.last5values.shift();
self.last5values.push(self.delta);

Expand All @@ -90,7 +106,7 @@ var WheelIndicator = (function() {
//Произошло ускорение
if(!self.isAcceleration) {
self.isAcceleration = true;
triggerEvent(self);
triggerEvent(self, event);
}
}
}
Expand All @@ -99,8 +115,7 @@ var WheelIndicator = (function() {
function addEvent(elem, type, handler){
if(window.addEventListener) {
elem.addEventListener(type, handler, false);
}
if(window.attachEvent) {
} else if (window.attachEvent) {
elem.attachEvent('on' + type, handler);
}
}
Expand Down
Loading

0 comments on commit ba6fe21

Please sign in to comment.