Questions about Scroll Avascript

Asked

Viewed 50 times

0

all right?

I would like to know how to capture the scroll of the page and appear the amount of scroll rolled on the console.

2 answers

0

The javascript has the object window who owns the property scrollY where it shows how much of scroll was made in the page...

try to use console.log(window.scrollY)

div{
  height: 2000px;
}
<div>

<div>


<script type="text/javascript">
window.onscroll = scroll;

function scroll () {
  console.log(window.scrollY , 'aa')
}
</script>

  • Good afternoon Otávio blz, the code you gave me really appears the value on the console but I wanted from the moment I scroll the console appear the amount in numbers, thanks.

  • Hi edited the answer see if it helps you...

0


Oops! If you want to render the screen value on the page, you can create a function activated by the scroll event... and insert the screen value into the HTML of an element of your page. For example:

<body>
  <div>
    <h1>Valor do Scroll da pagina</h1>
    <p id="valorScroll">x</p>
  </div>  
</body>

Apply this style so you can see the effect:

body{
  height:200vh;
}

div{
  position:fixed;
}

Ai you make a function:

window.addEventListener('scroll', function() {
    document.getElementById("valorScroll").innerHTML=window.window.scrollY
}

Result pen: https://codepen.io/anon/pen/XQKjwr

Browser other questions tagged

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