我正在尝试创建一个带有分页的轮播(演示)。当用户单击底部的块时,它应该循环到相应的图像。下面的代码是我目前拥有的。介意控制台mumdo jumbo。现在我正在确定用户是在可见块之后还是之前单击。
//grab the width and calculate left value
var item_width = $('#slides li').outerWidth();
var left_value = item_width * (-1);
//if user clicks on pagination block
$('#pagination ul li').click(function() {
var $this = $(this);
var temp = $(this).index() + 1;
var current = $('#pagination li a.active').parent().index() + 1; // console.log(current + " : " + temp );
if (current <= temp) {
// get difference of temp and current (temp - current)
var jump = Math.abs(temp - current);
//get the right position
var left_indent = parseInt($('#slides ul').css('left')) - item_width;
//slide the item
$('#slides ul').animate({'left' : left_indent}, 300, function () {
//active pagination
$('#pagination li a.active').removeClass('active');
$this.children().addClass('active');
//move the first item and put it as last item
$('#slides li:last').after($('#slides li:first'));
//set the default item to correct position
$('#slides ul').css({'left' : left_value});
//debugging nonsense
console.log( "left_indent: " + left_indent + "px" + "\n" + "left_value: " + left_value + "px" + "\n" + "current slide: " + current + "\n" + "future slide: " + temp + "\n" + "difference: " + jump);
});
} else if (current >= temp) {
// get difference of temp and current (temp - current)
var jump = Math.abs(temp - current);
//get the right position
var left_indent = parseInt($('#slides ul').css('left')) + item_width;
//slide the item
$('#slides ul').animate({'left' : left_indent}, 300,function(){
//active pagination
$('#pagination li a.active').removeClass('active');
$this.children().addClass('active');
//move the last item and put it as first item
$('#slides li:first').before($('#slides li:last'));
//set the default item to correct position
$('#slides ul').css({'left' : left_value});
//debugging nonsense
console.log( "left_indent: " + left_indent + "px" + "\n" + "left_value: " + left_value + "px" + "\n" + "current slide: " + current + "\n" + "future slide: " + temp + "\n" + "difference: " + jump);
});
}
});
我尝试过获取温度和电流的差异:
var jump = Math.abs(temp - current);
然后将两者相乘
$('#slides ul').animate({'left' : left_indent * jump}, 300, function () {
$('#slides ul').css({'left' : left_value * jump});
通过跳跃,但它不起作用。几次跳跃后我得到一个空白区域。
我似乎无法针对 left_indent、left_value 和分页之间的关系。任何帮助表示赞赏。
jsFiddle:http: //jsfiddle.net/hyTeq/