Javascript variable scope - DOM attribute value is not passed by assignment

Asked

Viewed 35 times

0

I was experimenting with pure Javascript and came across the following curiosity:

When I pass the reference values relative to the top of the page to variables, the code does not work, break.

That’s how it works:

window.onscroll = function(){
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        backToTop.style.display = "block";
    } else {
        backToTop.style.display = "none";
    }
};

So it doesn’t work:

var distanciaTopoBody = document.body.scrollTop;
var distanciaTopoHtml = document.documentElement.scrollTop;

window.onscroll = function(){
    if (distanciaTopoBody > 20 || distanciaTopoHtml > 20) {
        backToTop.style.display = "block";
    } else {
        backToTop.style.display = "none";
    }
};

Notice that the code doesn’t change much, it’s a noticeably small change, but the result is completely different.

I just assign document.body.scrollTop to a variable (distanceTopoBody) and document.documentElement.scrollTop to the other (distaTopoHtml);

JS Fiddle for those who want to test

1 answer

0

window.onscroll performs a function every time the scroll rolled.

So in order for it to work, the variables must be within the function!

var backToTop = document.getElementById("back-to-top");
window.onscroll = function(){
  var distanciaTopoBody = document.body.scrollTop;
  var distanciaTopoHtml = document.documentElement.scrollTop;
  if (distanciaTopoBody > 20 || distanciaTopoHtml > 20) {
    backToTop.style.display = "block";
  } else {
    backToTop.style.display = "none";
  }
};
#back-to-top {
  text-decoration: none;
  font-size: 24pt;
  font-weight: bold;
  padding: 15px 20px 7px 20px;
  background-color: blue;
  color: white;
  position: fixed;
  bottom: 10px;
  right: 10px;
  border-radius: 15px;
  opacity: .7;
  transition: opacity .7s ease-out;
  display: none;
}
<!-- Back to top button using pure JavaScript -->
<a href="#" id="back-to-top">^</a>
<div style="height: 900px;"></div>

The same goes for the event scroll.

var backToTop = document.getElementById("back-to-top");
window.addEventListener('scroll', function(e) {
  var distanciaTopoBody = document.body.scrollTop;
  var distanciaTopoHtml = document.documentElement.scrollTop;
  if (distanciaTopoBody > 20 || distanciaTopoHtml > 20) {
    backToTop.style.display = "block";
  } else {
    backToTop.style.display = "none";
  }
});
#back-to-top {
  text-decoration: none;
  font-size: 24pt;
  font-weight: bold;
  padding: 15px 20px 7px 20px;
  background-color: blue;
  color: white;
  position: fixed;
  bottom: 10px;
  right: 10px;
  border-radius: 15px;
  opacity: .7;
  transition: opacity .7s ease-out;
  display: none;
}
<!-- Back to top button using pure JavaScript -->
<a href="#" id="back-to-top">^</a>
<div style="height: 900px;"></div>

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.