Skip to content

add the ability to turn off stopPropagation and preventDefault #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ Specify the active class name to apply to a link when it is active, default is `
angular.module('myApp', ['duScroll']).value('duScrollActiveClass', 'custom-class');
```

### stopPropagation and preventDefault
By default `duSmoothScroll` will call `stopPropagation` and `preventDefault` on the click event. To prevent this set these values to `false`.

```js
angular.module('myApp', ['duScroll'])
.value('duStopPropagation', false)
.value('duPreventDefault', false);
```


Events
------

Expand Down
28 changes: 21 additions & 7 deletions angular-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var duScroll = angular.module('duScroll', [
.value('duScrollDuration', 350)
//Scrollspy debounce interval, set to 0 to disable
.value('duScrollSpyWait', 100)
//Scrollspy forced refresh interval, use if your content changes or reflows without scrolling.
//0 to disable
.value('duScrollSpyRefreshInterval', 0)
//Wether or not multiple scrollspies can be active at once
.value('duScrollGreedy', false)
//Default offset for smoothScroll directive
Expand All @@ -32,7 +35,12 @@ var duScroll = angular.module('duScroll', [
//Whether or not to activate the last scrollspy, when page/container bottom is reached
.value('duScrollBottomSpy', false)
//Active class name
.value('duScrollActiveClass', 'active');
.value('duScrollActiveClass', 'active')
//Whether or not to stopPropagation on the click event
.value('duStopPropagation', true)
//Whether or not to preventDefault on the click event
.value('duPreventDefault', true);


if (typeof module !== 'undefined' && module && module.exports) {
module.exports = duScroll;
Expand Down Expand Up @@ -254,7 +262,7 @@ angular.module('duScroll.requestAnimation', ['duScroll.polyfill'])


angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
.factory('spyAPI', ["$rootScope", "$timeout", "$window", "$document", "scrollContainerAPI", "duScrollGreedy", "duScrollSpyWait", "duScrollBottomSpy", "duScrollActiveClass", function($rootScope, $timeout, $window, $document, scrollContainerAPI, duScrollGreedy, duScrollSpyWait, duScrollBottomSpy, duScrollActiveClass) {
.factory('spyAPI', ["$rootScope", "$timeout", "$interval", "$window", "$document", "scrollContainerAPI", "duScrollGreedy", "duScrollSpyWait", "duScrollSpyRefreshInterval", "duScrollBottomSpy", "duScrollActiveClass", function($rootScope, $timeout, $interval, $window, $document, scrollContainerAPI, duScrollGreedy, duScrollSpyWait, duScrollSpyRefreshInterval, duScrollBottomSpy, duScrollActiveClass) {
'use strict';

var createScrollHandler = function(context) {
Expand Down Expand Up @@ -283,7 +291,7 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
for(i = 0; i < spies.length; i++) {
spy = spies[i];
pos = spy.getTargetPosition();
if (!pos) continue;
if (!pos || !spy.$element) continue;

if((duScrollBottomSpy && bottomReached) || (pos.top + spy.offset - containerOffset < 20 && (duScrollGreedy || pos.top*-1 + containerOffset) < pos.height)) {
//Find the one closest the viewport top or the page bottom if it's reached
Expand All @@ -300,7 +308,7 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
toBeActive = toBeActive.spy;
}
if(currentlyActive === toBeActive || (duScrollGreedy && !toBeActive)) return;
if(currentlyActive) {
if(currentlyActive && currentlyActive.$element) {
currentlyActive.$element.removeClass(duScrollActiveClass);
$rootScope.$broadcast(
'duScrollspy:becameInactive',
Expand Down Expand Up @@ -360,6 +368,9 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
var destroyContext = function($scope) {
var id = $scope.$id;
var context = contexts[id], container = context.container;
if(context.intervalPromise) {
$interval.cancel(context.intervalPromise);
}
if(container) {
container.off('scroll', context.handler);
}
Expand Down Expand Up @@ -411,6 +422,9 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
context.container.off('scroll', context.handler);
}
context.container = scrollContainerAPI.getContainer(spy.$scope);
if (duScrollSpyRefreshInterval && !context.intervalPromise) {
context.intervalPromise = $interval(context.handler, duScrollSpyRefreshInterval, 0, false);
}
context.container.on('scroll', context.handler).triggerHandler('scroll');
}
};
Expand Down Expand Up @@ -482,7 +496,7 @@ angular.module('duScroll.scrollContainerAPI', [])


angular.module('duScroll.smoothScroll', ['duScroll.scrollHelpers', 'duScroll.scrollContainerAPI'])
.directive('duSmoothScroll', ["duScrollDuration", "duScrollOffset", "scrollContainerAPI", function(duScrollDuration, duScrollOffset, scrollContainerAPI) {
.directive('duSmoothScroll', ["duScrollDuration", "duScrollOffset", "duStopPropagation", "duPreventDefault", "scrollContainerAPI", function(duScrollDuration, duScrollOffset, duStopPropagation, duPreventDefault, scrollContainerAPI) {
'use strict';

return {
Expand All @@ -495,8 +509,8 @@ angular.module('duScroll.smoothScroll', ['duScroll.scrollHelpers', 'duScroll.scr
var target = document.getElementById(id) || document.getElementsByName(id)[0];
if(!target || !target.getBoundingClientRect) return;

if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
if (duStopPropagation && e.stopPropagation) e.stopPropagation();
if (duPreventDefault && e.preventDefault) e.preventDefault();

var offset = $attr.offset ? parseInt($attr.offset, 10) : duScrollOffset;
var duration = $attr.duration ? parseInt($attr.duration, 10) : duScrollDuration;
Expand Down
Loading