Working on Jquery
(Toggle Slider).
Here is my jQuery
code for reference.
$(".leftBtn").click(function(e) { goRight(); }); $(".rightBtn").click(function(e) { goLeft(); }); function goRight() { // inner stuff slides left var initalLeftMargin = $(".innerLiner").css('margin-left').replace("px", "") * 1; var newLeftMargin = (initalLeftMargin - 204); // extra 2 for border $(".innerLiner").animate({ marginLeft: newLeftMargin }, 500); } function goLeft() { // inner stuff slides right var initalLeftMargin = $(".innerLiner").css('margin-left').replace("px", "") * 1; var newLeftMargin = (initalLeftMargin + 204); // extra 2 for border if (newLeftMargin >= 0){ $(".leftBtn").css("display", "none"); } else { $(".leftBtn").css("display", "block"); } $(".innerLiner").animate({ marginLeft: newLeftMargin }, 500); }
* { Box-sizing: Border-box } .mycontainer { white-space: nowrap; overflow-x: hidden; width: 204px; } .box { display: inline-block; border: 2px black solid; padding: 20px; width: 200px; height: 100px; vertical-align: top; background-color: pink; } .box2 { background-color: yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input class="rightBtn" type="button" value="Left"> <div class="mycontainer"> <div class="innerLiner"> <span class="box"> This is box1 </span> <span class="box"> This is box2 </span> <span class="box box2"> This is box3 </span> </div> </div> <input class="leftBtn" type="button" value="Right">
You are moving to the correct direction. Here are some tips to fix the code:
In short,
function updateButtons( newLeftMargin ) {
$(".leftBtn,.rightBtn").show(); // Show both buttons by default
if ( newLeftMargin >= 0 )
$(".rightBtn").hide();
if ( newLeftMargin <= -408 )
$(".leftBtn").hide();
}
updateButtons(0)
Below is a complete snippet. Note that I took the liability to lightly optimise your other code.
function goRight() { // inner stuff slides left var initalLeftMargin = parseInt( $(".innerLiner").css('margin-left') ); var newLeftMargin = (initalLeftMargin - 204); // extra 2 for border updateButtons( newLeftMargin ); $(".innerLiner").animate({ marginLeft: newLeftMargin }, 500); } function goLeft() { // inner stuff slides right var initalLeftMargin = parseInt( $(".innerLiner").css('margin-left') ); var newLeftMargin = (initalLeftMargin + 204); // extra 2 for border updateButtons( newLeftMargin ); $(".innerLiner").animate({ marginLeft: newLeftMargin }, 500); } function updateButtons( newLeftMargin ) { $(".leftBtn,.rightBtn").show(); // Show both buttons by default if ( newLeftMargin >= 0 ) $(".rightBtn").hide(); if ( newLeftMargin <= -408 ) $(".leftBtn").hide(); } updateButtons(0) $(".leftBtn").click( goRight ); $(".rightBtn").click( goLeft );
* { Box-sizing: Border-box } .mycontainer { white-space: nowrap; overflow-x: hidden; width: 204px; } .box { display: inline-block; border: 2px black solid; padding: 20px; width: 200px; height: 100px; vertical-align: top; background-color: pink; } .box2 { background-color: yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input class="rightBtn" type="button" value="Left"> <div class="mycontainer"> <div class="innerLiner"> <span class="box"> This is box1 </span> <span class="box"> This is box2 </span> <span class="box box2"> This is box3 </span> </div> </div> <input class="leftBtn" type="button" value="Right">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.