0

This is my current code and feels like it's not very efficient and would perhaps be better if Timer/Timeout is used. However, I'm lost as to how to go about it.

Could someone help? Not very efficient with javascript. My JS guy is on leave.

app.directive('ScrollBar', function () {
    return {
        restrict: 'A',
        scope: {},
        link: function postLink(scope, elem, attrs) {
            jQuery(window).scroll(function(){
                var SBar = jQuery("#ScrollStop").offset();
                var screenPosition = jQuery(document).scrollTop() + window.innerHeight;
                if (screenPosition < SBar.top) {
                    jQuery(".ScrollClass").fadeIn();
                }
                if (screenPosition >= SBar.top) {
                    jQuery( ".ScrollClass" ).fadeOut();
                }
            });
        }
    };
})
2
  • 1
    Seems fine as far as efficiency goes, but you probably want to debounce (throttle) those conditions so it doesn't fade on every scroll movement. Commented Feb 5, 2017 at 19:51
  • How do I do that? Commented Feb 6, 2017 at 4:04

1 Answer 1

1

It would be good if you differentiate the scroll direction by binding scroll function ,I have a fiddle,hope it helps.

http://jsfiddle.net/kavinhuh/17hca7wa/

myApp.directive('scrolly', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
        var lastScrollTop = 0;
            var raw = element[0];
            console.log('loading directive');

            element.bind('scroll', function () {
                console.log('in scroll');
                if(raw.scrollTop < lastScrollTop)
              {
              alert("scroll up");
              lastScrollTop = raw.scrollTop;
              }
              else{
              lastScrollTop = raw.scrollTop;
              }

                if (raw.scrollTop + raw.offsetHeight > raw.scrollHeight) {
                    scope.$apply(attrs.scrolly);
                }
            });
        }
    };
});
Sign up to request clarification or add additional context in comments.

Comments

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.