简体   繁体   English

appendTo表现不理想

[英]appendTo not acting as expected

I've got a simple slideshow, that when you click .left , moves and prepends the content. 我有一个简单的幻灯片显示,当您单击.left ,将移动并添加内容。 This seems to only affect the last slide: 这似乎只影响last一张幻灯片:

http://jsfiddle.net/tmyie/4CuLE/4/ http://jsfiddle.net/tmyie/4CuLE/4/

$('.left').click(function(){
        $('.box').animate({left: '+=100'}, 100, function(){
        var $last = $('.box').last();
        if ($last.css('left') == '100px') {
            $last.prependTo('.container').before('\n\r');
            $('.box').css('left','0px');
        }
    });
});

However, I've tried to reverse this process with appendTo . 但是,我尝试使用appendTo逆转此过程。 This appends to ALL content: 这将附加到所有内容:

$('.right').click(function(){
        $('.box').animate({left: '-=100'}, 100, function(){
        var $last = $('.box').first();
        if ($last.css('left') == '-100px') {
            $last.appendTo('.container').before('\n\r');
            $('.box').css('left','0px');
        }
    });
});

Would anyone know why appendTo is appending to every item, whereas prependTo is prepending to just one? 有谁知道为什么appendTo追加到每个项目,而prependTo追加到每个项目?

I've updated your jsfiddle and now it works. 我已经更新了您的jsfiddle ,现在可以使用了。 Basically, I've changed your checking method, based on ifs that theoretically should always return true (because you're animating the five objects to the 100px, when you check if the animated property is 100px, it should return true). 基本上,我已经更改了您的检查方法,基于理论上应该始终返回true的ifs (因为您要对五个对象设置100px的动画效果,当检查animation属性是否为100px时,它应该返回true)。 I don't exactly know why it fails in the first case, but in the second case, when you go right to left, you basically are moving the five elements... but leaving the '\\n\\r' before them, that's why the elements move to the right with each animation. 我不完全知道为什么在第一种情况下它会失败,但是在第二种情况下,当您从右向左移动时,您基本上是在移动这五个元素...但是在它们之前保留了'\\ n \\ r',这就是为什么每个动画元素都向右移动。

var $boxes = $('.box');
var max = $boxes.length;
$('.left').click(function(){
    var count = 0;
    $boxes.animate({left: '+=100'}, 100, function(){
        if (++count != max) return; //check whether is the last call to the handler
        var $last = $('.box').last();
        $last.prependTo('.container');
        $('.box').css('left','0px');
    });
});

$('.right').click(function(){
    var count = 0;
    $('.box').animate({left: '-=100'}, 100, function(){
        if (++count != max) return; //check whether is the last call to the handler
        var $last = $('.box').first();
        $last.appendTo('.container');
        $('.box').css('left','0px');
    });
});

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

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