My window.scrolltop not working

1.1k views Asked by At

I am using window.scroll function, but window.scrollTop didn't return any value in lightning controller. Help me.

initialize : function(component, event, helper) {
            var isHidden = false
            var lastScrollTop = 0
            console.log('heyyyyyy');
            window.addEventListener('scroll', function() {
                console.log('heyasasyyyyy');
                var scrollTop =  document.documentElement.scrollTop;
                var offset = scrollTop-lastScrollTop ;
                lastScrollTop = scrollTop ;
                console.log(scrollTop+'++++'+offset+'++++++'+lastScrollTop);
            });

        },
1

There are 1 answers

0
elicohenator On

You have a typo: 3rd row - latScrollTop instead of lastScrollTop.

Fixed Code:

initialize : function(component, event, helper) {
            var isHidden = false
            var lastScrollTop = 0
            console.log('heyyyyyy');
            window.addEventListener('scroll', function() {
                console.log('heyasasyyyyy');
                var scrollTop =  document.documentElement.scrollTop;
                var offset = scrollTop-lastScrollTop ;
                lastScrollTop = scrollTop ;
                console.log(scrollTop+'++++'+offset+'++++++'+lastScrollTop);
            });

        },