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

javascript - how to detect the change of y offset by scrolling without refresh - Stack Overflow

programmeradmin1浏览0评论

i am trying to make scroll to top button and i need the button only appears when the y offset = 400, it is working when the page is refreshed or recently opened but it cannot detect the change in the y offset so the button is not dynamic and only works on refresh and detect the position of y offset only on load how can i make the button detect the position of the y offset by scrolling and without refresh.

var goup = document.getElementById("top");

(window.pageYOffset.onchange = function() {
  if (window.pageYOffset >= 400) {
    goup.style.display = "block";
  } else {
    goup.style.display = "none";
  }
})();
#top {
  background-color: red;
  color: white;
  border-radius: 100%;
  padding: 10px;
  position: fixed;
  bottom: 10px;
  right: 10px;
  border: none;
  display: none;
}
<button id="top">top</button>

i am trying to make scroll to top button and i need the button only appears when the y offset = 400, it is working when the page is refreshed or recently opened but it cannot detect the change in the y offset so the button is not dynamic and only works on refresh and detect the position of y offset only on load how can i make the button detect the position of the y offset by scrolling and without refresh.

var goup = document.getElementById("top");

(window.pageYOffset.onchange = function() {
  if (window.pageYOffset >= 400) {
    goup.style.display = "block";
  } else {
    goup.style.display = "none";
  }
})();
#top {
  background-color: red;
  color: white;
  border-radius: 100%;
  padding: 10px;
  position: fixed;
  bottom: 10px;
  right: 10px;
  border: none;
  display: none;
}
<button id="top">top</button>

Share Improve this question edited Dec 26, 2019 at 13:05 vahdet 6,75910 gold badges62 silver badges116 bronze badges asked Dec 26, 2019 at 13:03 yasser Eltibiliyasser Eltibili 1343 silver badges8 bronze badges 1
  • You need to listen to the scroll event and then check [window.pageYOffset](window.pageYOffset) to see if it passed 400px. Ideally instead place the element you want as the threshold (rather than 400px) on the page and use an intersection observer to check when it bees visibile. – Benjamin Gruenbaum Commented Dec 26, 2019 at 13:06
Add a ment  | 

3 Answers 3

Reset to default 4

window.pageYOffset.onchange will not work. The pageYOffset is a plain number, so there is no onchange or any other event. The reason why it works on refresh is by accident, because the callback function is called once on load (the () at the end of the function). So even if the pageYOffset.onchange worked, it would not work with your code.

You have to listen for the scroll event on the window:

window.addEventListener('scroll', function () {
    // the page was scrolled (horizontally or vertically)
});

var goup = document.getElementById("top");

window.addEventListener('scroll', function() {
  if (window.pageYOffset >= 400) {
    goup.style.display = "block";
  } else {
    goup.style.display = "none";
  }
})
body {
  height: 1000px;
}

#top {
  background-color: red;
  color: white;
  border-radius: 100%;
  padding: 10px;
  position: fixed;
  bottom: 10px;
  right: 10px;
  border: none;
  display: none;
}
<button id="top">top</button>

You want this:

working demo: https://jsfiddle/vj6Lnzhd/

var goup = document.getElementById("top");
window.addEventListener('scroll', ()=> {
  if (window.pageYOffset >= 400) {
    goup.style.display = "block";
  } else {
    goup.style.display = "none";
  }
});

Same concept, slightly shorter code:

window.addEventListener("scroll", function(){
  document.getElementById("top").style.display = window.pageYOffset >= 400 ? "block" : "none";
});
发布评论

评论列表(0)

  1. 暂无评论