Skip to content

Commit

Permalink
Version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
asafdav committed Jun 19, 2015
1 parent 2bc478c commit 9c4f39c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-scrollbar",
"version": "0.0.7",
"version": "0.0.8",
"dependencies": {
},
"main": [
Expand Down
49 changes: 34 additions & 15 deletions dist/ng-scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
var mainElm, transculdedContainer, tools, thumb, thumbLine, track;
var flags = { bottom: attrs.hasOwnProperty('bottom') };
var win = angular.element($window);
var hasAddEventListener = !!win[0].addEventListener;
var hasRemoveEventListener = !!win[0].removeEventListener;
// Elements
var dragger = { top: 0 }, page = { top: 0 };
// Styles
Expand Down Expand Up @@ -55,10 +57,18 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
event.stopPropagation();
};
var wheelHandler = function (event) {
var wheelDivider = 20;
// so it can be changed easily
var deltaY = event.wheelDeltaY !== undefined ? event.wheelDeltaY / wheelDivider : event.wheelDelta !== undefined ? event.wheelDelta / wheelDivider : -event.detail * (wheelDivider / 10);
dragger.top = Math.max(0, Math.min(parseInt(page.height, 10) - parseInt(dragger.height, 10), parseInt(dragger.top, 10) - deltaY));
var wheelSpeed = 40;
// Mousewheel speed normalization approach adopted from
// http://stackoverflow.com/a/13650579/1427418
var o = event, d = o.detail, w = o.wheelDelta, n = 225, n1 = n - 1;
// Normalize delta
d = d ? w && (f = w / d) ? d / f : -d / 1.35 : w / 120;
// Quadratic scale if |d| > 1
d = d < 1 ? d < -1 ? (-Math.pow(d, 2) - n1) / n : d : (Math.pow(d, 2) + n1) / n;
// Delta *should* not be greater than 2...
event.delta = Math.min(Math.max(d / 2, -1), 1);
event.delta = event.delta * wheelSpeed;
dragger.top = Math.max(0, Math.min(parseInt(page.height, 10) - parseInt(dragger.height, 10), parseInt(dragger.top, 10) - event.delta));
redraw();
if (!!event.preventDefault) {
event.preventDefault();
Expand Down Expand Up @@ -93,22 +103,31 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
win.off('touchend', _touchEnd);
event.stopPropagation();
};
var buildScrollbar = function (rollToBottom) {
// Getting top position of a parent element to place scroll correctly
var parentOffsetTop = element[0].parentElement.offsetTop;
var registerEvent = function (elm) {
var wheelEvent = win[0].onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll';
if (hasAddEventListener) {
elm.addEventListener(wheelEvent, wheelHandler, false);
} else {
elm.attachEvent('onmousewheel', wheelHandler);
}
};
var removeEvent = function (elm) {
var wheelEvent = win[0].onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll';
if (hasRemoveEventListener) {
elm.removeEventListener(wheelEvent, wheelHandler, false);
} else {
elm.detachEvent('onmousewheel', wheelHandler);
}
};
var buildScrollbar = function (rollToBottom) {
rollToBottom = flags.bottom || rollToBottom;
mainElm = angular.element(element.children()[0]);
transculdedContainer = angular.element(mainElm.children()[0]);
tools = angular.element(mainElm.children()[1]);
thumb = angular.element(angular.element(tools.children()[0]).children()[0]);
thumbLine = angular.element(thumb.children()[0]);
track = angular.element(angular.element(tools.children()[0]).children()[1]);
// Check if scroll bar is needed
page.height = element[0].offsetHeight - parentOffsetTop;
if (page.height < 0) {
page.height = element[0].offsetHeight;
}
page.height = element[0].offsetHeight;
page.scrollHeight = transculdedContainer[0].scrollHeight;
if (page.height < page.scrollHeight) {
scope.showYScrollbar = true;
Expand All @@ -125,8 +144,8 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
thumbLine.css(draggerLineStyle);
// Bind scroll bar events
track.bind('click', trackClick);
// Handl mousewheel
transculdedContainer[0].addEventListener(wheelEvent, wheelHandler, false);
// Handle mousewheel
registerEvent(transculdedContainer[0]);
// Drag the scroller with the mouse
thumb.on('mousedown', function (event) {
lastOffsetY = event.pageY - thumb[0].offsetTop;
Expand All @@ -152,7 +171,7 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
scope.showYScrollbar = false;
scope.$emit('scrollbar.hide');
thumb.off('mousedown');
transculdedContainer[0].removeEventListener(wheelEvent, wheelHandler, false);
removeEvent(transculdedContainer[0]);
transculdedContainer.attr('style', 'position:relative;top:0');
// little hack to remove other inline styles
mainElm.css({ height: '100%' });
Expand Down
4 changes: 2 additions & 2 deletions dist/ng-scrollbar.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-scrollbar",
"version": "0.0.7",
"version": "0.0.8",
"main": "dist/ng-scrollbar.min.js",
"description": "A custom scrollbar written in pure AngularJS",
"keywords": [
Expand Down

0 comments on commit 9c4f39c

Please sign in to comment.