Skip to content

Commit

Permalink
add updateScrollbar method to update just the scrollbar positioning.
Browse files Browse the repository at this point in the history
useful for situations with contenteditable divs
  • Loading branch information
yads committed Sep 23, 2014
1 parent c0f0439 commit fe49f2f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 77 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngTinyScrollbar",
"version": "0.6.0",
"version": "0.6.1",
"homepage": "https://github.com/yads/ngTinyScrollbar",
"authors": [
"Vadim Kazakov <[email protected]>"
Expand Down
78 changes: 41 additions & 37 deletions dist/ng-tiny-scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ angular.module('ngTinyScrollbar', ['ngAnimate'])
document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
"DOMMouseScroll"), // let's assume that remaining browsers are older Firefox
sizeLabel = isHorizontal ? 'width' : 'height',
sizeLabelCap = sizeLabel.charAt(0).toUpperCase() + sizeLabel.slice(1).toLowerCase(),
posiLabel = isHorizontal ? 'left' : 'top',
moveEvent = document.createEvent('HTMLEvents'),
restoreVisibilityAfterWheel;
Expand All @@ -90,47 +91,50 @@ angular.module('ngTinyScrollbar', ['ngAnimate'])

this.update = function(scrollTo)
{
var sizeLabelCap = sizeLabel.charAt(0).toUpperCase() + sizeLabel.slice(1).toLowerCase();
this.viewportSize = $viewport[0]['offset'+ sizeLabelCap];
this.contentSize = $overview[0]['scroll'+ sizeLabelCap];
this.contentRatio = this.viewportSize / this.contentSize;
this.trackSize = this.options.trackSize || this.viewportSize;
this.thumbSize = Math.min(this.trackSize, Math.max(0, (this.options.thumbSize || (this.trackSize * this.contentRatio))));
this.trackRatio = this.options.thumbSize ? (this.contentSize - this.viewportSize) / (this.trackSize - this.thumbSize) : (this.contentSize / this.trackSize);
mousePosition = $scrollbar[0].offsetTop;

$scrollbar.toggleClass('disable', this.contentRatio >= 1);

if (this.contentRatio > 1) {
return this;
this.updateScrollbar(scrollTo);
if (this.contentRatio <= 1) {
$overview.css(posiLabel, -self.contentPosition + 'px');
}

if (!this.options.alwaysVisible && this.viewportSize > 0) {
//flash the scrollbar when update happens
$animate.addClass($scrollbar[0], 'visible').then(function() {
$animate.removeClass($scrollbar[0], 'visible');
$scope.$digest();
});
}
switch (scrollTo) {
case 'bottom':
this.contentPosition = this.contentSize - this.viewportSize;
break;
case 'relative':
this.contentPosition = Math.min(this.contentSize - this.viewportSize, Math.max(0, this.contentPosition));
break;
default:
this.contentPosition = parseInt(scrollTo, 10) || 0;
}
setSize();
return this;
};

function setSize() {
$thumb.css(posiLabel, self.contentPosition / self.trackRatio + 'px');
$overview.css(posiLabel, -self.contentPosition + 'px');
$scrollbar.css(sizeLabel, self.trackSize + 'px');
$thumb.css(sizeLabel, self.thumbSize + 'px');
this.updateScrollbar = function(scrollTo) {
this.viewportSize = $viewport[0]['offset'+ sizeLabelCap];
this.contentSize = $overview[0]['scroll'+ sizeLabelCap];
this.contentRatio = this.viewportSize / this.contentSize;
this.trackSize = this.options.trackSize || this.viewportSize;
this.thumbSize = Math.min(this.trackSize, Math.max(0, (this.options.thumbSize || (this.trackSize * this.contentRatio))));
this.trackRatio = this.options.thumbSize ? (this.contentSize - this.viewportSize) / (this.trackSize - this.thumbSize) : (this.contentSize / this.trackSize);
mousePosition = $scrollbar[0].offsetTop;

$scrollbar.toggleClass('disable', this.contentRatio >= 1);

if (this.contentRatio > 1) {
return this;
}

if (!this.options.alwaysVisible && this.viewportSize > 0) {
//flash the scrollbar when update happens
$animate.addClass($scrollbar[0], 'visible').then(function() {
$animate.removeClass($scrollbar[0], 'visible');
$scope.$digest();
});
}
switch (scrollTo) {
case 'bottom':
this.contentPosition = this.contentSize - this.viewportSize;
break;
case 'relative':
this.contentPosition = Math.min(this.contentSize - this.viewportSize, Math.max(0, this.contentPosition));
break;
default:
this.contentPosition = parseInt(scrollTo, 10) || 0;
}
$thumb.css(posiLabel, self.contentPosition / self.trackRatio + 'px');
$scrollbar.css(sizeLabel, self.trackSize + 'px');
$thumb.css(sizeLabel, self.thumbSize + 'px');

return this;
}

function setEvents() {
Expand Down
2 changes: 1 addition & 1 deletion dist/ng-tiny-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": "ngTinyScrollbar",
"version": "0.6.0",
"version": "0.6.1",
"description": "An angular directive port of the TinyScrollbar",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit fe49f2f

Please sign in to comment.