简体   繁体   English

我将如何使这个小jQuery滑块正确滑动?

[英]How would I make this small jQuery slider slide right?

I'm trying to get a slider to slide right, in addition to left. 我正在尝试使滑块向左滑动。 Two buttons named "Next" and "Previous" control the sliding. 两个名为“下一个”和“上一个”的按钮控制滑动。

Currently the slider slides left no problem. 目前,滑块向左滑动没有问题。 The variable direction holds whether the clicked button is "previous" (slide right) or "next" (slide left). 可变direction保持所单击的按钮是“上一个”(向右滑动)还是“下一个”(向左滑动)。 I'm having trouble implementing the logic to make it so when direction == 'previous' the slider slides right. 我在实现逻辑时遇到麻烦,因此当direction == 'previous' ,滑块会向右滑动。 I would think it'd be simply negating some numbers but not so! 我认为这只是简单地否定一些数字,但事实并非如此!

Here is the code prepared in jsFiddle: http://jsfiddle.net/LWJQm/ 这是jsFiddle中准备的代码: http : //jsfiddle.net/LWJQm/

For demo purposes this code is dumb-downed and only slides the same DIV over and over. 出于演示目的,该代码已精简,并且只能一遍又一遍地滑动相同的DIV。

JavaScript here: JavaScript在这里:

var $slider= $('#slider'),
    sliderHtml = $slider.html(),
    $next = $('#next'),
    $previous = $('#previous'),
    sliderWidth= parseInt($slider.css('width')),
    locked = false;

$('.button').on('click', function() {
    if (locked) {
        return false;   
    }

    locked = true;

    var direction = $(this).attr('id');
    console.log(direction);

    var $transfer = $('<div></div>').css({'width': (2 * sliderWidth) + 'px'});
    var $current = $('<div></div>').css({'width': sliderWidth+ 'px', 'left': '0', 'float': 'left'}).html($slider.html());
    var $next = $('<div></div>').css({'width': sliderWidth+ 'px', 'left': sliderWidth + 'px', 'float': 'left'}).html(sliderHtml);

    $transfer.append($current).append($next);
    $slider.html('').append($transfer);

    $transfer.animate({'margin-left': '-' + sliderWidth+ 'px'}, 300, function() {
        locked = false;
    });
});

i`m not sure i understand the problem. 我不确定我是否理解问题。 i moddified your fiddle with a simple if else statement. 我用一个简单的if else语句修改了你的小提琴。 is this what your are looking for? 这是您要找的东西吗? http://jsfiddle.net/flyingsausage/NvxKj/ http://jsfiddle.net/flyingsausage/NvxKj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM