How can you increment a number on scroll without having the scrollbar(actually scrolling)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How can you increment a number on scroll without having the scrollbar(actually scrolling)?



So, imagine 2 sections/divs (both with width: 100% and height: 100%). I only see the first section, the second section is to the right side of the screen, unable to be seen(overflow: hidden; on the body).


width: 100%


height: 100%



Now, when I scroll, I want that second section to appear gradually, with each pixel I'm "scrolling".



But, the problem here is, I can't actually scroll, thus properties such as this: window.pageYOffset, element.getBoundingClientRect() do not work. Basically, I want to increment a number each time I do a scrolling gesture, so I can assign that number to the second section( to modify its left property so it can come in the viewport ). And I don't know how to increment the number.


window.pageYOffset


element.getBoundingClientRect()



This is a recreation of what I'm trying to accomplish:




var secondSection = document.getElementsByClassName("second-section")[0];



function leftTransition(){
/*secondSection.style.left = number to be incremented + "px";*/
}

document.addEventListener("scroll", leftTransition);


html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}

.first-section, .second-section {
width: 100%;
height: 100%;
}

.second-section {
position: absolute;
top: 0;
left: 100%;
background-color: blue;
}


<div class="first-section">
content
content
</div>

<div class="second-section">
content
content
</div>





this recreation does nothing - so - hard to tell, because I'm not good at imagining :p
– Jaromanda X
53 mins ago





Why not continue using percentages? + or - 1% each scroll?
– Shilly
53 mins ago





Because technically, the scroll event does not fire, because there's nothing to scroll. I'm really confused to what I should do now.
– MWR
52 mins ago





Either resize the page so there's always something to scroll or choose a different event. Maybe you can do something with the wheel event which will fire when a mousewheel is rotated. And since the basic functionality on desktop is to scroll the page, it's a good alternative. Just be sure to provide some kind of fallback. It's generally not a good idea to use scrolling events if there's nothing to scroll.
– Shilly
41 mins ago




wheel





I think @Shilly's right about the wheel method, I would recommend looking at the documentation for it
– Matthew Schlachter
37 mins ago




wheel




1 Answer
1



If you go for the wheel event, something like this might resemble what you're trying to do.
You can then remove the event handler once the page is as much in view as you want.


wheel




var secondSection = document.getElementsByClassName("second-section")[0];
function leftTransition( event ){
var offset = event.deltaY;
var left = offset < 0
? ( parseInt( secondSection.style.left ) + 1 ) + '%'
: ( parseInt( secondSection.style.left ) - 1 ) + '%';
secondSection.style.left = left;
}
window.addEventListener("wheel", leftTransition);


html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.first-section, .second-section {
width: 100%;
height: 100%;
background-color: steelblue;
}
.second-section {
position: absolute;
top: 0;
background-color: blue;
}


<div class="first-section">content content</div>
<div class="second-section" style="left: 100%">content content</div>





Yes, that's what I wanted, but I have a question before I'll accept the answer how can I, if I scroll "up" for example, make the second section go back into its original state?
– MWR
24 mins ago







The event.deltaY property will be positive when scrolling down and negative when scrolling up. Maybe there's different properties or methods you can use as well, but have a look at the wheel event reference Matthew posted.
– Shilly
16 mins ago


event.deltaY





That's great! Thank you.
– MWR
2 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name