var images;
var scrollContainer;
var noScrollDown;
var noScrollUp;
var scrollLeft;
var scrollTop;
var latch;
function setUp() {
images = document.getElementsByClassName("image");
scrollContainer = document.getElementById("content");
noScrollDown = true;
noScrollUp = true;
scrollLeft = scrollContainer.scrollLeft;
scrollTop = scrollContainer.scrollTop;
latch = false;
}
function doScrollUp () {
noScrollUp = false;
noScrollDown = true;
scrollContainer.style.overflow = "auto";
latch = false;
}
function doScrollDown () {
noScrollDown = false;
noScrollUp = true;
scrollContainer.style.overflow = "auto";
latch = false;
}
function fadeIn(ndx) {
if ( ndx > 3 ) {
// Top image is showing so switch direction of allowable scrolling
doScrollDown();
} else {
// Fade in an image and queue the fade in of the next image
images[ndx].style.opacity = 1;
setTimeout(fadeIn, 2000, ndx+1); // The time here, in milliseconds, should be the same as the transition time above
}
}
function fadeOut(ndx) {
if ( ndx < 1 ) {
// Bottom image is showing so switch direction of allowable scrolling
doScrollUp();
} else {
// Fade out an image and queue the fade out of the next image
images[ndx].style.opacity = 0;
setTimeout(fadeOut, 2000, ndx-1); // The time here, in milliseconds, should be the same as the transition time above
}
}
function transOpacity() {
if ( noScrollDown && scrollContainer.scrollTop >= scrollTop ) {
// No scroll down allowed, and scroll event tried to go down
// Scroll back to where the scroll was
scrollContainer.scrollTo(scrollLeft, scrollTop); // This will trigger another scroll event
//
if ( ! latch ) {
latch = true; // Stop double processing due to second scroll event triggered above
scrollContainer.style.overflow = "hidden";
fadeIn(1);
}
} else if ( noScrollUp && scrollContainer.scrollTop <= scrollTop ) {
// No scroll up allowed, and scroll event tried to go up
// Scroll back to where the scroll was
scrollContainer.scrollTo(scrollLeft, scrollTop); // This will trigger another scroll event
if ( ! latch ) {
latch = true; // Stop double processing due to second scroll event triggered above
scrollContainer.style.overflow = "hidden";
fadeOut(3)
}
} else {
scrollLeft = scrollContainer.scrollLeft;
scrollTop = scrollContainer.scrollTop;
latch = false;
}
}
.images img {
width: 100%; /* Note that 100% width will cover the scoll bar of the content, when the flow is decoupled with 'display: position;' */
max-height: 25vh; /* Set a limit on the height of the images, for this example, so that the scroll bar of the content can be fully accessible */
display: block;
position: absolute;
top: 0vh; /* Set the position, for this example, so that the scroll bar of the content can be fully accessible */
}
/* Set up the required transition on the property relevant property */
.images img.image {
opacity: 0; /* The default initial opacity */
transition-property: opacity;
transition-duration: 2s; /* The time here, in seconds, needs to be the same as the setTimeout times below */
}
/* Initialise the first image as showing, ie, full opacity. */
.images img.image:nth-of-type(1) {
opacity: 1;
}
.content {
margin-top: 25vh; /* For this example, make sure the scroll container is below the images so that the scroll bar is fully accessible. */
}
<body onLoad="setUp()">
<div>
<div class="images">
<img src="https://dummyimage.com/1400x600/ff4400/fff.png&text=1" alt="1" class="image">
<img src="https://dummyimage.com/1400x600/3c00ff/fff.png&text=2" alt="2" class="image">
<img src="https://dummyimage.com/1400x600/44a10f/fff.png&text=3" alt="3" class="image">
<img src="https://dummyimage.com/1400x600/d99f2b/fff.png&text=4" alt="4" class="image">
</div>
<div id="content" class="content" onscroll="transOpacity()" style="height: 50vh; overflow:auto;">
<p>This should be scrolled down normally - line 1.</p>
<p>This should be scrolled down normally - line 2.</p>
<p>This should be scrolled down normally - line 3.</p>
<p>This should be scrolled down normally - line 4.</p>
<p>This should be scrolled down normally - line 5.</p>
<p>This should be scrolled down normally - line 6.</p>
<p>This should be scrolled down normally - line 7.</p>
<p>This should be scrolled down normally - line 8.</p>
<p>This should be scrolled down normally - line 9.</p>
<p>This should be scrolled down normally - line 10.</p>
<p>This should be scrolled down normally - line 11.</p>
<p>This should be scrolled down normally - line 12.</p>
<p>This should be scrolled down normally - line 13.</p>
<p>This should be scrolled down normally - line 14.</p>
<p>This should be scrolled down normally - line 15.</p>
<p>This should be scrolled down normally - line 16.</p>
<p>This should be scrolled down normally - line 17.</p>
<p>This should be scrolled down normally - line 18.</p>
<p>This should be scrolled down normally - line 19.</p>
<p>This should be scrolled down normally - line 20.</p>
<p>This should be scrolled down normally - line 21.</p>
<p>This should be scrolled down normally - line 22.</p>
<p>This should be scrolled down normally - line 23.</p>
<p>This should be scrolled down normally - line 24.</p>
<p>This should be scrolled down normally - line 25.</p>
<p>This should be scrolled down normally - line 26.</p>
<p>This should be scrolled down normally - line 27.</p>
<p>This should be scrolled down normally - line 28.</p>
<p>This should be scrolled down normally - line 29.</p>
<p>This should be scrolled down normally - line 30.</p>
</div>
</div>
</body>