I created a basic slider with animation effect,
I'm using transform: translate3d
to move the slider left or right and have an issue and a bit lost on how to make it loop and slide either left or right infinitely.
I'm trying to make it so when you click left or right it keeps showing and rotating the images.
I also wanted to have a smooth transition with z-index, but it doesn't seem possible.
Here's a jsFiddle of what I have accomplished https://jsfiddle.net/wo67h4n9/
here's the HTML code
<div class="vs-slider">
<div class="vss-wrap">
<div class="item active"><img src="http://lorempixel.com/430/280/sports" alt="Slider Item" width="430" height="280"></div>
<div class="item"><img src="http://lorempixel.com/430/280/animals" alt="Slider Item" width="430" height="280"></div>
<div class="item"><img src="http://lorempixel.com/430/280/nature" alt="Slider Item" width="430" height="280"></div>
</div>
<ul class="vss-nav">
<li class="prev"><</li>
<li class="next">></li>
</ul>
</div>
jQuery
;( function($) {
$(document).ready(function() {
$('.vs-slider .item').each( function() {
$(this).css('z-index', $('.vs-slider .item').length - $('.vs-slider .item').index(this));
});
$('.vss-nav').on('click', '.prev, .next', function() {
var active = $(this).closest('.vs-slider').find('.item.active');
if ( $(this).hasClass('next') ) {
vss_moveleft($('.vs-slider'));
active.next().addClass('active');
} else {
vss_moveleft( $('.vs-slider'), 'right');
active.prev().addClass('active');
}
active.removeClass('active');
});
function vss_moveleft( slider, type = 'left' ) {
var itemWidth = slider.find('.item').outerWidth() - 299,
itemTotal = slider.find('.item').length,
currentOff = slider.find('.vss-wrap').position().left,
movemVal = type === 'left' ? currentOff - itemWidth : currentOff + itemWidth;
slider.find('.vss-wrap').css('transform', 'translate3d('+ movemVal +'px, 0px, 0px)');
}
});
})(jQuery);
CSS
body {
background: #222
}
.vs-slider {
position: relative;
overflow: hidden;
max-height: 290px;
max-width: 500px;
}
.vs-slider img {
margin: 0;
vertical-align: top;
}
.vs-slider .vss-wrap {
min-width: 90VW;
transform: translate3d(0px, 0px, 0px);
transition: all 0.5s ease 0s;
}
.vs-slider .vss-wrap::after {
clear: both;
width: 100%;
display: block;
content: "";
}
.vs-slider .item {
float: left;
border: 1px solid #fff;
transform: scale(.7);
position: relative;
z-index: 1;
transition: all 1s ease 0s;
margin-right: -299px;
}
.vs-slider .item.active {
transform: scale(1);
z-index: 20 !important;
}
.vs-slider .item:not(.active) {
z-index: 0;
cursor: pointer;
}
.vss-nav {
position: absolute;
margin: 0;
padding: 0;
right: 5px;
bottom: 0;
}
.vss-nav li {
display: inline-block;
color: #fff;
margin: 0 5px;
cursor: pointer;
}
Would appreciate any help.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…