JSFiddle - React, Tailwind, and code Playground
HTML
<!--- DISPLAY CONTAINER --->
<div id="h_container"><!-- OUTTER WRAPPER -->
<div id="h_wrapper"><!-- SLIDE 1 -->
<div id="slide1" class="h_slide">
<p>Part 1</p>
</div>
<!-- SLIDE 2 -->
<div id="slide2" class="h_slide">
<p>Part 2</p>
</div>
<!-- SLIDE 3 -->
<div id="slide3" class="h_slide">
<p>Part 3</p>
</div>
</div>
</div>
<!--- NAVIGATION BUTTONS -->
<table style="width:200px; padding: 0 10px 0 10px;">
<tbody>
<tr>
<td align="left"><a href="javascript:void(0);" id="PreviousButton" style="display:none">« Previous</a></td>
<td align="right"><a href="javascript:void(0);" id="NextButton">Next »</a></td>
</tr>
</tbody>
</table>
CSS
#h_container { width: 200px; overflow: hidden; }
#h_wrapper { width: 600px; margin-left:0px; }
.h_slide { width: 200px; height: 200px; overflow: hidden; float: left; }
JavaScript
var SlideSpeed = 500;
var SlideWidth = 200;
$("#NextButton").on("click", function(event) {
if ( $("#h_wrapper").is(':not(:animated)') && $("#NextButton").is(':not(:animated)') ) {
var newMargin = CurrentMargin() - SlideWidth;
$("#h_wrapper").animate({ marginLeft: newMargin }, SlideSpeed, function () { SetNavigationDisplay() });
}
});
$("#PreviousButton").on("click", function() {
if ( $("#h_wrapper").is(':not(:animated)') && $("#PreviousButton").is(':not(:animated)') ) {
var newMargin = CurrentMargin() + SlideWidth;
$("#h_wrapper").animate({ marginLeft: newMargin }, SlideSpeed, function () { SetNavigationDisplay() });
}
});
function CurrentMargin() {
// get current margin of slider
var currentMargin = $("#h_wrapper").css("margin-left");
// return the current margin to the function as an integer
return parseInt(currentMargin);
}
function SetNavigationDisplay() {
// get current margin
var currentMargin = CurrentMargin();
// if current margin is at 0, then we are at the beginning, hide previous
if (currentMargin == 0) {
$("#PreviousButton").fadeOut();
}
else {
$("#PreviousButton").fadeIn();
}
// get wrapper width
var wrapperWidth = $("#h_wrapper").width();
// turn current margin into postive number and calculate if we are at last slide, if so, hide next button
if ((currentMargin * -1) == (wrapperWidth - SlideWidth)) {
$("#NextButton").fadeOut();
}
else {
$("#NextButton").fadeIn();
}
}