window.pageYOffset всегда возвращает 0. (Но только на машине windows возвращает правильный номер на Ma c) - PullRequest
0 голосов
/ 10 июля 2020

Итак, я работаю над функцией закрепления заголовка, благодаря которой мой заголовок и список будних дней будут оставаться в верхней части страницы. Практически то же самое, что найдено здесь . Следующий код отлично работает на Ma c, однако становится проблематичным c на Windows компьютере, когда window.pageYoffset всегда возвращает 0. Любые предложения о том, почему это так, будут полезны.

Примечание. мой тег body не имеет свойства высоты и не настроен на переполнение: скрыто.

 window.onscroll = function() {respiteStickyFunction()};

              // Get the header
              var header = document.getElementById("myHeader");
              var weekdayHeader = document.getElementsByClassName("fc-head")[0];
                
              // Get the offset position of the navbar
              var sticky = header.offsetTop;
              
              // Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
              function respiteStickyFunction() {
                
                console.log(sticky, window.pageYOffset )

                if (window.pageYOffset > sticky) {
                  header.classList.add("sticky");
                  weekdayHeader.classList.add("sticky-weekdays");
                } else {
                  header.classList.remove("sticky");
                  weekdayHeader.classList.remove("sticky-weekdays");
                }
              }

1 Ответ

0 голосов
/ 10 июля 2020

Я сделал фрагмент, и он у меня работает в Chrome / Windows 10. "Работает" = ваш код регистрирует ненулевые значения для pageYOffset.

window.onscroll = function() {respiteStickyFunction()};

              // Get the header
              var header = document.getElementById("myHeader");
              var weekdayHeader = document.getElementsByClassName("fc-head")[0];
                
              // Get the offset position of the navbar
              var sticky = header.offsetTop;
              
              // Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
              function respiteStickyFunction() {
                
                console.log(sticky, window.pageYOffset )

                if (window.pageYOffset > sticky) {
                  header.classList.add("sticky");
                  weekdayHeader.classList.add("sticky-weekdays");
                } else {
                  header.classList.remove("sticky");
                  weekdayHeader.classList.remove("sticky-weekdays");
                }
              }
<h id="myHeader"></h>
<p>sample text</p>
<p class="fc-head">sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text x</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
<p>sample text</p>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...