Skip to content

Commit

Permalink
Added scroller.scrollToElement and released v0.2.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador committed Jan 8, 2014
1 parent 905ef7c commit ce09311
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ angular.module('myApp', ['duScroll']).
var chunk = 200;
//Scroll 200px down from current scroll position
scroller.scrollDelta(x, chunk, duration);

var offset = 30; //pixels; adjust for floating menu, context etc
//Scroll to #some-id with 30 px "padding"
//Note: Use this in a directive, not with document.getElementById
scroller.scrollToElement(document.getElementById('some-id'), offset, duration);
}
);
```
Expand Down
13 changes: 11 additions & 2 deletions angular-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ factory('scroller',
scrollTo(scrollPosition.x() + (x || 0), scrollPosition.y() + (y || 0), duration);
}

function scrollToElement(element, offset, duration){
if(!element || !element.getBoundingClientRect) return;

var pos = element.getBoundingClientRect();

scrollDelta(0, pos.top + (!offset || isNaN(offset) ? 0 : -offset), duration);
}

return {
scrollTo: scrollTo,
scrollDelta: scrollDelta
scrollTo: scrollTo,
scrollToElement: scrollToElement,
scrollDelta: scrollDelta
};
}
);
Expand Down
2 changes: 1 addition & 1 deletion angular-scroll.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 angular-scroll.min.js.map

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

7 changes: 4 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-scroll",
"version": "0.2.2",
"version": "0.2.3",
"main": "angular-scroll.min.js",
"ignore": [
"**/.*",
Expand All @@ -9,9 +9,10 @@
"test",
"tests",
"package.json",
"src"
"src",
"Gruntfile.js"
],
"dependencies": {
"angular": "~1.2.1"
"angular": "~1.2.7"
}
}
9 changes: 6 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,20 @@ <h2>Section 4</h2>
<p>Shoulder cow tenderloin chuck, pork chop jerky doner leberkas. Chuck sausage hamburger, kevin beef pork chop pork shoulder ground round ball tip turducken flank. Bresaola tri-tip meatloaf, salami venison tail pig shank shankle jowl sausage brisket cow biltong turducken. Swine turducken hamburger ball tip short loin prosciutto kevin jowl tri-tip. Doner meatloaf pork brisket.</p>
</section>
<footer>
<button ng-click="toTheTop()">Take me back!</button>
<button ng-click="toTheTop()">Take me back!</button> or <button ng-click="toSection2()">To section 2</button>
</footer>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script src="http://durated.github.io/angular-scroll/angular-scroll.min.js"></script>
<script>
angular.module('myApp', ['duScroll']).
controller('MyCtrl', function($scope, scroller){
$scope.toTheTop = function() {
scroller.scrollTo(0,0, 1500);
scroller.scrollTo(0,0, 5000);
}
$scope.toSection2 = function() {
scroller.scrollToElement(document.getElementById('section-2'), 30, 1500);
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-scroll",
"version": "0.2.2",
"version": "0.2.3",
"description": "Scrollspy, animated scrollTo and scroll events",
"keywords": ["angular", "smooth-scroll", "scrollspy", "scrollTo", "scrolling"],
"main": "angular-scroll.min.js",
Expand Down
13 changes: 11 additions & 2 deletions src/services/scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ factory('scroller',
scrollTo(scrollPosition.x() + (x || 0), scrollPosition.y() + (y || 0), duration);
}

function scrollToElement(element, offset, duration){
if(!element || !element.getBoundingClientRect) return;

var pos = element.getBoundingClientRect();

scrollDelta(0, pos.top + (!offset || isNaN(offset) ? 0 : -offset), duration);
}

return {
scrollTo: scrollTo,
scrollDelta: scrollDelta
scrollTo: scrollTo,
scrollToElement: scrollToElement,
scrollDelta: scrollDelta
};
}
);

0 comments on commit ce09311

Please sign in to comment.