Last active
December 16, 2015 16:09
-
-
Save guillaumepiot/5460846 to your computer and use it in GitHub Desktop.
JQUERY - Trail navigator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HTML | |
<div id="mattress_slider"> | |
<div class="mattress_container"> | |
<div class="mattress_slider"> | |
<div class="product-list-item"></div> | |
<div class="product-list-item"></div> | |
<div class="product-list-item"></div> | |
... | |
</div> | |
</div> | |
</div> | |
// LESS | |
#mattress_slider{ | |
position:relative; | |
.mattress_container{ | |
overflow:hidden; | |
position:relative; | |
height: 292px; | |
} | |
.mattress_slider{ | |
position: absolute; | |
top:0; | |
left:0; | |
height: 252px; | |
} | |
.previous{ | |
position: absolute; | |
text-decoration: none; | |
background-image: url('/media/img/arrow-left.png'); | |
background-repeat: no-repeat; | |
display: block; | |
width:20px; | |
height:54px; | |
opacity:0.7; | |
&:hover{ | |
opacity:1; | |
} | |
} | |
.next{ | |
position: absolute; | |
text-decoration: none; | |
background-image: url('/media/img/arrow-right.png'); | |
background-repeat: no-repeat; | |
display: block; | |
width:20px; | |
height:54px; | |
opacity:0.7; | |
&:hover{ | |
opacity:1; | |
} | |
} | |
} | |
//JS | |
//Mattress slider | |
//Mattress slider | |
$('#mattress_slider').append('<a href="#" class="previous"></a>'); | |
$('#mattress_slider').append('<a href="#" class="next"></a>'); | |
// How manay mattresses | |
var mattress_count = $('#mattress_slider .product-list-item').length; | |
var mattress_width = $('#mattress_slider .product-list-item').width(); | |
var slider_width = $('#mattress_slider').width(); | |
// How manay fit in slider | |
var fits = slider_width / mattress_width; | |
// How manay are missing | |
var hidden = mattress_count - fits; | |
// Adjust slider | |
$('.mattress_slider').css({'width':mattress_count * mattress_width}); | |
// Position arrows | |
$('#mattress_slider .previous').hide().css({'top':100, 'left':-34}).click(function(e){ | |
e.preventDefault() | |
if(hidden < (mattress_count - fits)){ | |
var passed = mattress_count - fits - hidden; | |
// Shift slider by one | |
$('.mattress_slider').stop().animate({'left': ((1-passed) * mattress_width)}, 500); | |
// Decrement hidden | |
hidden += 1; | |
} | |
// Show or hide controls accordingly | |
show_hide_controls(); | |
}) | |
$('#mattress_slider .next').css({'top':100, 'right':-34}).click(function(e){ | |
e.preventDefault() | |
if(hidden > 0){ | |
var passed = mattress_count - fits - hidden; | |
// Shift slider by one | |
$('.mattress_slider').stop().animate({'left': -((1+passed) * mattress_width)}, 500); | |
// Decrement hidden | |
hidden -= 1; | |
} | |
// Show or hide controls accordingly | |
show_hide_controls(); | |
}) | |
function show_hide_controls(){ | |
if(hidden == 0) $('#mattress_slider .next').hide(); | |
else $('#mattress_slider .next').show(); | |
if(hidden == mattress_count - fits) $('#mattress_slider .previous').hide(); | |
else $('#mattress_slider .previous').show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment