Skip to content

Commit e13e398

Browse files
committed
Retrigger onScroll until position stabilizes on init. Fixes #6.
1 parent bf40b62 commit e13e398

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

angular-parallax.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ factory('parallaxHelper',
2121

2222
angular.module('duParallax.directive', ['duScroll']).
2323
directive('duParallax',
24-
function($rootScope, scrollPosition){
24+
function($rootScope, scrollPosition, $window){
2525
//Never mind touch devices
2626
if('ontouchstart' in window) {
2727
return;
@@ -75,9 +75,28 @@ directive('duParallax',
7575
link: function($scope, $element, $attr){
7676
var element = $element[0];
7777
var currentProperties;
78+
var inited = false;
7879

7980
var onScroll = function($event, scrollY){
8081
var rect = element.getBoundingClientRect();
82+
if(!inited) {
83+
inited = true;
84+
angular.element($window).on('load', function initParallax() {
85+
//Trigger the onScroll until position stabilizes. Don't know why this is needed.
86+
//TODO: Think of more elegant solution.
87+
var i = 0;
88+
var maxIterations = 10;
89+
var currentY = rect.top;
90+
var lastY;
91+
do {
92+
lastY = currentY;
93+
onScroll($event, scrollY);
94+
currentY = element.getBoundingClientRect().top;
95+
i++;
96+
} while(i < maxIterations && lastY !== currentY);
97+
});
98+
}
99+
81100
var param = {
82101
scrollY : scrollY,
83102
elemX: rect.left,

angular-parallax.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

angular-parallax.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/parallax.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
angular.module('duParallax.directive', ['duScroll']).
22
directive('duParallax',
3-
function($rootScope, scrollPosition){
3+
function($rootScope, scrollPosition, $window){
44
//Never mind touch devices
55
if('ontouchstart' in window) {
66
return;
@@ -54,9 +54,28 @@ directive('duParallax',
5454
link: function($scope, $element, $attr){
5555
var element = $element[0];
5656
var currentProperties;
57+
var inited = false;
5758

5859
var onScroll = function($event, scrollY){
5960
var rect = element.getBoundingClientRect();
61+
if(!inited) {
62+
inited = true;
63+
angular.element($window).on('load', function init() {
64+
//Trigger the onScroll until position stabilizes. Don't know why this is needed.
65+
//TODO: Think of more elegant solution.
66+
var i = 0;
67+
var maxIterations = 10;
68+
var currentY = rect.top;
69+
var lastY;
70+
do {
71+
lastY = currentY;
72+
onScroll($event, scrollY);
73+
currentY = element.getBoundingClientRect().top;
74+
i++;
75+
} while(i < maxIterations && lastY !== currentY);
76+
});
77+
}
78+
6079
var param = {
6180
scrollY : scrollY,
6281
elemX: rect.left,

0 commit comments

Comments
 (0)