Reputation: 404
I'm trying to make my slider move in the opposite direction, but I can't figure out how to make it move right to left, instead it's moving left to right. I can get it to go right to left, but the problem I'm encountering is with the right to left movement, it doesn't stay a full grid, instead their is a mysterious gap between the two. With the left to right method, the grid moves as one huge block without any interruptions.
How can I make it so that my slider goes right to left and stops at the last li, and only go back with left arrow instead of doing a full wrap around by just pressing right/button?
http://jsfiddle.net/YDSWA/2/ (use arrow keys right and left to move slider)
Below is the jS for my slider and the jsFiddle has the CSS and HTML.
jQuery(document).ready(function ($) {
var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider').css({
width: slideWidth,
height: slideHeight
});
$('#slider ul').css({
width: sliderUlWidth,
marginLeft: -slideWidth
});
$('#slider ul li:last-child').prependTo('#slider ul');
function moveLeft() {
$('#slider ul').animate({
right: -slideWidth
}, 700, function () {
$('#slider ul li').prependTo('#slider ul');
$('#slider ul').css('right', '');
})
}
function moveRight() {
$('#slider ul').animate({
right: -slideWidth
}, 700, function () {
$('#slider ul li:first-child').appendTo('#slider ul');
$('#slider ul').css('right', '');
})
}
$('#back').click(function () {
moveLeft();
})
$('#next').click(function () {
moveRight();
})
$(document).keydown(function (e) {
if (e.keyCode == 39) {
moveRight();
} else {
}
})
$(document).keydown(function (e) {
if (e.keyCode == 37) {
moveLeft();
} else {
}
})
});
Thanks for the help!
Upvotes: 1
Views: 851
Reputation: 206515
jQ:
jQuery(function($) {
var $sl = $('#slider'),
$ul = $('ul', $sl),
$li = $('li', $ul),
slideCount = $li.length,
slideWidth = $li.width(),
slideHeight = $li.height(),
sliderUlWidth = slideCount * slideWidth;
$sl.css({width: slideWidth, height: slideHeight});
$ul.css({width: sliderUlWidth});
function moveLeft() {
$ul.not(':animated').prepend( $('li:last-child', $ul) )
.css({left:-slideWidth})
.animate({left:0}, 700);
}
function moveRight() {
$ul.not(':animated').animate({left: -slideWidth}, 700, function() {
$(this).css({left: 0}).append( $('li:first-child', this) );
});
}
$('#back, #next').click(function() {
return this.id=='next' ? moveRight() : moveLeft();
});
$(document).keydown(function(e) {
var k = e.which;
if( k==39 || k==37 ){
e.preventDefault();
return k==39? moveRight() : moveLeft();
}
});
});
CSS:
*{margin:0;padding:0;}
.uni_con {
margin: 0 auto;
width: 1200px;
}
#slider {
position: relative;
overflow: hidden;
margin: 0 auto;
background:#cf5;
}
#slider ul {
position: relative;
left:0;
list-style: none;
background:#f00;
}
#slider ul li {
float: left;
background: none;
width: 1200px;
background:#555;
display: inline;
position: relative;
}
#slider img {
cursor: pointer;
float:left;
height: 400px;
width: 400px;
}
Upvotes: 1