Jquery幻灯片div更改css左边距

Jquery幻灯片div更改css左边距,jquery,Jquery,我知道,像这样的问题不多,但大多数都太复杂,代码太多 我想在div不断变化的利润率之间滑动。它可以在向右滑动时正常工作,但在向左滑动时不起作用。对于这两种情况,我都有完全相同的代码,但我遗漏了一些东西,不知道是什么 $('#next').click(function(){ var current = $('.selected'); var next = current.next(); if (next.length !=0) {

我知道,像这样的问题不多,但大多数都太复杂,代码太多

我想在div不断变化的利润率之间滑动。它可以在向右滑动时正常工作,但在向左滑动时不起作用。对于这两种情况,我都有完全相同的代码,但我遗漏了一些东西,不知道是什么

$('#next').click(function(){
        var current = $('.selected');
        var next = current.next();
        if (next.length !=0) {

            width = current.width() + 'px';
            next.show().css({'margin-left': width});
            current.animate({'margin-left': -width}, 900, function(){
                current.hide().removeClass('selected');
            });
            next.animate({'margin-left': 0}, 900, function(){
                $(this).addClass('selected');
            });
        }
    })


您可以在小提琴中看到-当您单击next时,NexDiv按预期滑入。但是如果你点击“上一页”,它不会滑动。(

当您更改边距边时,您正在切换推/拉哪边。您实际上只想处理相同的边,但值相反。因此,与其在css方法和动画方法中切换到使用
边距右
,不如让它使用相同的
边距左
,但使值与其他边相反单击方法

$('#prev').click(function(){
    var current = $('.selected');
    var next = current.prev();
    if (next.length !=0) {

        width = current.width() + 'px';
        next.show().css({'margin-left': -width});
        current.animate({'margin-left': width}, 900, function(){
            current.hide().removeClass('selected');
        });
        next.animate({'margin-left': 0}, 900, function(){
            $(this).addClass('selected');
        });
    }

})

当您更改边距边时,您正在切换推/拉哪边。您确实只想处理相同的边,但值相反。因此,与其在css方法和动画方法中切换到使用
边距右
,不如让它使用相同的
边距左
,但使值与其他值相反r单击方法

$('#prev').click(function(){
    var current = $('.selected');
    var next = current.prev();
    if (next.length !=0) {

        width = current.width() + 'px';
        next.show().css({'margin-left': -width});
        current.animate({'margin-left': width}, 900, function(){
            current.hide().removeClass('selected');
        });
        next.animate({'margin-left': 0}, 900, function(){
            $(this).addClass('selected');
        });
    }

})

当您更改边距边时,您正在切换推/拉哪边。您确实只想处理相同的边,但值相反。因此,与其在css方法和动画方法中切换到使用
边距右
,不如让它使用相同的
边距左
,但使值与其他值相反r单击方法

$('#prev').click(function(){
    var current = $('.selected');
    var next = current.prev();
    if (next.length !=0) {

        width = current.width() + 'px';
        next.show().css({'margin-left': -width});
        current.animate({'margin-left': width}, 900, function(){
            current.hide().removeClass('selected');
        });
        next.animate({'margin-left': 0}, 900, function(){
            $(this).addClass('selected');
        });
    }

})

当您更改边距边时,您正在切换推/拉哪边。您确实只想处理相同的边,但值相反。因此,与其在css方法和动画方法中切换到使用
边距右
,不如让它使用相同的
边距左
,但使值与其他值相反r单击方法

$('#prev').click(function(){
    var current = $('.selected');
    var next = current.prev();
    if (next.length !=0) {

        width = current.width() + 'px';
        next.show().css({'margin-left': -width});
        current.animate({'margin-left': width}, 900, function(){
            current.hide().removeClass('selected');
        });
        next.animate({'margin-left': 0}, 900, function(){
            $(this).addClass('selected');
        });
    }

})

没问题,有时只需要几双眼睛:)没问题,有时只需要几双眼睛:)没问题,有时只需要几双眼睛:)没问题,有时只需要几双眼睛:)没问题,有时只需要几双眼睛:)