Take a look at this code snippet:
$(document).ready(function() {
$('.scrollable-area').on('wheel', function(e) {
var scrollLeft = $(this).scrollLeft();
var width = $(this).get(0).scrollWidth - $(this).width();
var deltaY = e.originalEvent.deltaY;
var deltaX = e.originalEvent.deltaX;
var newScrollLeft = scrollLeft + deltaY + deltaX;
if ((deltaY > 0 && newScrollLeft < width) ||
(deltaY < 0 && newScrollLeft > 0)) {
e.preventDefault();
}
if (newScrollLeft <= 0) {
$(this).scrollLeft(0);
} else if (newScrollLeft >= width) {
$(this).scrollLeft(width);
} else {
$(this).scrollLeft(newScrollLeft);
}
});
});
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.gallery-container {
position: relative;
width: 100%;
height: 90vh;
background-color: yellow;
overflow: hidden;
}
.scrollable-area {
width: 100%;
height: 100%;
overflow-x: auto;
}
.gallery-items {
display: flex;
min-width: 100%;
height: 100%;
}
.gallery-item {
flex: 0 0 auto;
height: 100%;
display: flex;
}
.gallery-item img {
max-width: 100%;
height: auto;
object-fit: contain;
}
.gallery-item iframe {
background-color: blue;
width: auto;
width: 800px;
}
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<div class="gallery-container">
<div class="scrollable-area">
<div class="gallery-items">
<div class="gallery-item">
<img src="https://upload.wikimedia.org/wikipedia/commons/1/16/Appearance_of_sky_for_weather_forecast%2C_Dhaka%2C_Bangladesh.JPG">
</div>
<div class="gallery-item">
<img src="https://upload.wikimedia.org/wikipedia/commons/d/da/Sky_landscape.jpg">
</div>
<div class="gallery-item">
<iframe src="https://player.vimeo.com/video/584985260" frameborder="0" allow="fullscreen"></iframe>
</div>
<div class="gallery-item">
<img src="https://upload.wikimedia.org/wikipedia/commons/1/16/Appearance_of_sky_for_weather_forecast%2C_Dhaka%2C_Bangladesh.JPG">
</div>
</div>
</div>
</div>
When scrolling through the media gallery, you might notice that it stops when reaching an iframe. This behavior is intentional but can be improved. I've attempted to handle pointer events during scrolling, but encountered issues with video playback buttons. If anyone has insight on resizing iframes dynamically or adjusting their height within the horizontal gallery, your input would be greatly appreciated!