Skip to content

Commit

Permalink
Throttle scroll events. Optimize scroll performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannu Pelkonen committed Jun 16, 2015
1 parent cf2e72d commit c1f674a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"oclazyload": "0.3.8",
"ngprogress": "~1.0.7",
"angular-debounce": "~1.0.0",
"angular-scroll": "0.7.0"
"angular-scroll": "0.7.0",
"lodash": "~3.9.3"
},
"devDependencies": {},
"resolutions": {
Expand Down
17 changes: 16 additions & 1 deletion lib/app/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,26 @@ angular.module('sgApp', [
modules: window.filesConfig
});
})
.run(function($rootScope) {
.factory('lodash', ['$window',
function($window) {
return $window._ || _;
}
])
.run(function($rootScope, $window, lodash) {
$rootScope.currentReference = {
section: {
}
};

// Create global throttled scorll
function broadcastScrollEvent(event) {
console.log("SCROLL!");
$rootScope.$broadcast('scroll', event);
}
// Some tests replace $window with a mock, make sure that we have real window
if (typeof $window.addEventListener === 'function') {
angular.element($window).bind('scroll', lodash.throttle(broadcastScrollEvent, 350));
}
})
.filter('addWrapper', ['Styleguide', function(Styleguide) {
return function(html) {
Expand Down
2 changes: 1 addition & 1 deletion lib/app/js/directives/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ angular.module('sgApp')
scope.section.showCSS = false;

// Listen to scroll events and update currentReference if this section is currently focused
angular.element($window).bind('scroll', function() {
scope.$on('scroll', function() {
updateCurrentReference();
});

Expand Down
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(config) {
'lib/app/js/components/ngprogress/build/ngProgress.js',
'lib/app/js/components/angular-debounce/dist/angular-debounce.js',
'lib/app/js/components/angular-scroll/angular-scroll.js',
'lib/app/js/components/lodash/lodash.js',
// application code
'lib/app/js/*.js',
'lib/app/js/controllers/*.js',
Expand Down

0 comments on commit c1f674a

Please sign in to comment.