Home > OS >  Why PageYOffset is being strikethrough in VS Studio? and its not working
Why PageYOffset is being strikethrough in VS Studio? and its not working

Time:06-10

I am trying to use "pageYOffset" property but its shown as strikethrough and not doing the job. Any better alternative or solution this problem.

JavaScript Code:

window.addEventListener("scroll", function({
        if(this.pageYOffset > 60){
            document.querySelector(".header").classList.add("sticky");
        }
        else {
            document.querySelector(".header").classList.remove("sticky");
        }
    });

CodePudding user response:

You didn't write correctly your event listener call back function. It should work as you can see here:

window.addEventListener("scroll", function() {
  if (this.pageYOffset > 60) {
    document.querySelector(".header").classList.add("sticky");
  } else {
    document.querySelector(".header").classList.remove("sticky");
  }
});
.header{
  background:crimson;
}
.sticky{
  background:blue;
  position:sticky;
  top:0;
}
<header >My header</header>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>

CodePudding user response:

The reason that you cannot use the pageYOffset is because it a Window property. In your code:

window.addEventListener("scroll", function({
        if(this.pageYOffset > 60){  /// <------
            document.querySelector(".header").classList.add("sticky");
        }
        else {
            document.querySelector(".header").classList.remove("sticky");
        }
    });

This is referring to the event listener function and not the window itself.

Documentation can be found here.

The alternative to pageYOffset is scrollY.

  • Related