I want to showcase a collection of movies on my website through a horizontal, scrollable div. Since I disabled the scrollbar, users can no longer scroll, leading me to incorporate two buttons - one for moving right and one for moving left.
However, the issue arises when users repeatedly press the buttons, causing the movies to get obstructed due to the additional margin applied through Javascript.
How can I tackle this problem? I aim to deactivate the left-button when the starting position is reached and deactivate the right-button when the end position is reached.
$('#right-button').click(function() {
event.preventDefault();
$('.movie-scroll').animate({
marginLeft: "-=200px"
}, "fast");
});
$('#left-button').click(function() {
event.preventDefault();
$('.movie-scroll').animate({
marginLeft: "+=200px"
}, "fast");
});
::-webkit-scrollbar {
display: none;
}
.sc_content {
overflow-y: scroll;
}
.title_home {
font-size: 18px;
margin: 30px 0px -10px 30px;
}
.movie-scroll {
height: 300px;
white-space: nowrap;
overflow-x: scroll;
margin-right: 30px;
}
.thumbnail-movie-box {
width: 130px;
margin: 30px 0px 0px 30px;
display: inline-block;
}
.thumbnail-movie-content {
display: block;
position: relative;
}
.thumbnail-movie-poster {
width: 130px;
height: 195px;
}
.thumbnail-movie-title {
font-size: 12px;
font-weight: bold;
margin: 5px 0px 10px 0px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.thumbnail-movie-year {
font-size: 12px;
margin: -5px 10px 10px 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div class="sc_content">
<h1 class="title_home">Movies</h1>
<a href="#" id="left-button">Left</a>
<div class="movie-scroll">
<div class="thumbnail-movie-box">
<a href="">
<div class="thumbnail-movie-content">
<img class="thumbnail-movie-poster" src="https://image.tmdb.org/t/p/w500/ijoPd2gdt8jW982fGemySKDOGtv.jpg">
</div>
</a>
<a href="">
<p class="thumbnail-movie-title">Split</p>
</a>
<p class="thumbnail-movie-year">2016</p>
</div>
<div class="thumbnail-movie-box">
<a href="">
<div class="thumbnail-movie-content">
<img class="thumbnail-movie-poster" src="https://image.tmdb.org/t/p/w500/ijoPd2gdt8jW982fGemySKDOGtv.jpg">
</div>
</a>
<a href="">
<p class="thumbnail-movie-title">Split</p>
</a>
<p class="thumbnail-movie-year">2016</p>
</div>
<div class="thumbnail-movie-box">
<a href="">
<div class="thumbnail-movie-content">
<img class="thumbnail-movie-poster" src="https://image.tmdb.org/t/p/w500/ijoPd2gdt8jW982fGemySKDOGtv.jpg">
</div>
</a>
<a href="">
<p class="thumbnail-movie-title">Split</p>
</a>
<p class="thumbnail-movie-year">2016</p>
</div>
<div class="thumbnail-movie-box">
// Repeat for other movie boxes
</div>
</div>
<a href="#" id="right-button">Right</a>