1

How to prevent page scrolling when scrolling a DIV element in AngularJS?

After reading this JQuery answer I want to apply the same in AngularJS directive but I don't know how... Any suggestions?

Example in JQuery: http://jsfiddle.net/XNwbt/458/

$( '.scrollable' ).bind( 'mousewheel DOMMouseScroll', function ( e ) {
    var e0 = e.originalEvent,
    delta = e0.wheelDelta || -e0.detail;

    this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
    e.preventDefault();
});

Thank you!


Edited:

I tried to do the same in AngularJS but the DIV element never is scrolling...

'use strict';

angular
    .module('core')
    .directive('preventScrollBody', preventScrollBody);

function preventScrollBody() {
    return{
        link: link,
        restrict: 'A'
    };

    function link(scope, element, attrs) {
        element.bind( 'mousewheel DOMMouseScroll', function ( e ) {
            var e0 = e.originalEvent,
                delta = e0.wheelDelta || -e0.detail;

            this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
            e.preventDefault();
        });
    }
}
2
  • So juyst make directive customscroll and put this code to it link function. (replace $( '.scrollable' ) with element from args) should work imho Commented Sep 22, 2015 at 11:27
  • Just edited the question. I tried to do the same in Angular but doesn't work. (added the code in the question) Commented Sep 22, 2015 at 11:39

3 Answers 3

4

At least work in my chrome:

function link(scope, element, attrs) {
    element.bind( 'mousewheel DOMMouseScroll', function ( e ) {
      console.log(e);
        var e0 = e;//.originalEvent,
        var delta = e0.wheelDelta || -e0.detail;

        this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
        e.preventDefault();
    });
}

http://plnkr.co/edit/sZFvgIyt9l9SZjQtTkH2?p=preview

Sign up to request clarification or add additional context in comments.

3 Comments

Amazing! This works for me in plnkr, but I copied & pasted in my project and doesn't work. Maybe is for the Angular version? you are using Angular 1.4 and I'm using an old version of Angular 1.2... mmm I don't understand! hah! Thank you for all!
It's actually working but can you please explain Why you commented //.originalEvent?
Great, works perfectly! Just call prevent-scroll-body on the html element you want to apply it to :)
1

Using Petr's great answer, I've replaced line

    var delta = e0.wheelDelta || -e0.detail;

with

    var delta = e0.originalEvent.wheelDelta || -e0.detail;

and it works perfectly. So modified function is

    function link(scope, element, attrs) {
      element.bind( 'mousewheel DOMMouseScroll', function ( e ) {

        var e0 = e;
        var delta = e0.originalEvent.wheelDelta || -e0.detail;

        this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
        e.preventDefault();
      });

}

Comments

0

event argument "e" - which holds the scroll event object lets you prevent the page scroll, when it is scrolling on div ('.scrollable') as below

event.preventDefault();

1 Comment

I understand how works in JQuery. My question is how to do the same in angularJS directive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.