最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $window.scrollY , window.scrollY and window.pageYOffset nothing is working in angularjs - Stack Overflow

programmeradmin2浏览0评论

I tried all the possibilities I know and which I found in Stackoverflow to get window.scrollY value it always gave me "0" even same with window.scrollX. Please, can somebody suggest me what I'm doing wrong?

I tried $window.scrollY, $document[0].documentElement.scrollTop ,$document[0].documentElement.scrollLeft, $window.pageYOffset , angular.element($window) nothing is working for me.

This is my code snippet (angular way)

Here in below code

  1. topOfNav working great
  2. fixNav is triggering on scroll event
  3. eventListener is working great
  4. window.scrollY not working and I'm always getting zero.

    $timeout(function () {
       var nav = angular.element($document[0].querySelector("#main"));
    
        // getting this offsetTop value
           var topOfNav = nav.prop('offsetTop');
    
           var fixNav = function () {
           console.log(topOfNav, $window.scrollY);
          };
    
      // this is working great, it is listening to scroll event 
         $window.addEventListener('scroll', fixNav, true); 
     });
    

To test I tried creating simple HTML file and added this snippet (javascript way) in <script></script> tag it's working great.

here is the code (works like a champ)

<script>
  const nav = document.querySelector('#main');
  const topOfNav = nav.offsetTop;
  function fixNav() {
     console.log(topOfNav, window.scrollY);
 }

 window.addEventListener('scroll', fixNav);
 </script>

Additional Information:

  • AngularJS Version: 1.6.2
  • Browser Type: chrome
  • Browser Version: Version 55.0.2883.87 (64-bit)
  • OS: Ubuntu 14.04 LTS

Thank you.

EDIT:

Angular Material Version: 1.x.x

After digging too much (Updating Packages, uninstalling and re-installing packages) one thing I found is Angular-Material is the Blocker. I tried with other projects of angular too, the only blocker I found was Angular Material

I tried all the possibilities I know and which I found in Stackoverflow to get window.scrollY value it always gave me "0" even same with window.scrollX. Please, can somebody suggest me what I'm doing wrong?

I tried $window.scrollY, $document[0].documentElement.scrollTop ,$document[0].documentElement.scrollLeft, $window.pageYOffset , angular.element($window) nothing is working for me.

This is my code snippet (angular way)

Here in below code

  1. topOfNav working great
  2. fixNav is triggering on scroll event
  3. eventListener is working great
  4. window.scrollY not working and I'm always getting zero.

    $timeout(function () {
       var nav = angular.element($document[0].querySelector("#main"));
    
        // getting this offsetTop value
           var topOfNav = nav.prop('offsetTop');
    
           var fixNav = function () {
           console.log(topOfNav, $window.scrollY);
          };
    
      // this is working great, it is listening to scroll event 
         $window.addEventListener('scroll', fixNav, true); 
     });
    

To test I tried creating simple HTML file and added this snippet (javascript way) in <script></script> tag it's working great.

here is the code (works like a champ)

<script>
  const nav = document.querySelector('#main');
  const topOfNav = nav.offsetTop;
  function fixNav() {
     console.log(topOfNav, window.scrollY);
 }

 window.addEventListener('scroll', fixNav);
 </script>

Additional Information:

  • AngularJS Version: 1.6.2
  • Browser Type: chrome
  • Browser Version: Version 55.0.2883.87 (64-bit)
  • OS: Ubuntu 14.04 LTS

Thank you.

EDIT:

Angular Material Version: 1.x.x

After digging too much (Updating Packages, uninstalling and re-installing packages) one thing I found is Angular-Material is the Blocker. I tried with other projects of angular too, the only blocker I found was Angular Material

Share Improve this question edited Jun 17, 2017 at 13:32 Rajath M S asked Jun 15, 2017 at 7:25 Rajath M SRajath M S 1,0922 gold badges18 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Somewhere removing the style height:100% from the body element, the above code worked for me

The following worked for me:

html, body { height: auto }

发布评论

评论列表(0)

  1. 暂无评论