【问题标题】:jQuery animate step transformjQuery动画步骤变换
【发布时间】:2018-12-30 02:50:58
【问题描述】:

我正面临 jQuery animate now 属性的问题。 但它总是增加当前值。 我希望它从 0 开始,每次循环都以 20 结束。这是我的代码

var container = $('#top_slides');

    var items = container.children('.top_slide_sp');
    
    var lengths = items.length;
    var index = 0;
    var transition_speed = 5000;
    var transSP = function(){
        items.eq(index).animate({
            'opacity': '100',
             now: '+=20'
        }, {
            step: function (now, fx) {
                $(this).css('transform', "translate3d(" + now + "px, 0, 0)");
            },
            duration: 6000,
            easing: 'linear',
            queue: false
        }, 'linear');
    };
    
    var show = function(){
        items.eq(index).fadeOut(800, function(){
            index += 1;
            if (index === lengths) {
                index = 0;
            }
               transSP();

            items.eq(index).fadeIn(800, function(){
                items.removeClass('active');
                items.eq(index).addClass('active');
                
                setTimeout(show, transition_speed);
            });
        });
    };
    
    // Show first item
    items.eq(index).addClass('active').fadeIn(400, function () {
      
           transSP();
        
        setTimeout(show, transition_speed);
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="top_slides">

<div class="top_slide_sp">1</div>
<div class="top_slide_sp">2</div>
<div class="top_slide_sp">3</div>

</div>

【问题讨论】:

    标签: javascript jquery css jquery-animate transform


    【解决方案1】:

    你当前的代码加号(+)是问题

    {
                'opacity': '100',
                 now: '+=20' <--------
    }
    

    删除并让我们像这样尝试

    {
                'opacity': '100',
                 now: '=20'
            }
    

    【讨论】:

    • 德克萨斯州。我试过像你的代码一样。但你可以看到它一直递增到 100 像素而不是 20 像素
    • 在所有 .top_slide_sp 类 div 上创建一个 20px 的 div 并将其设为 100% 而不是 100px
    • 我自己解决了。 tx 为您提供时间和建议。我现在使用默认值,并通过划分它使其成为 20。 $(this).css('transform', "translate3d(" + (now/2-30) + "px, 0, 0)");
    • var transSP = function(){ items.eq(index).animate({ 'opacity': '100', now: '=20' }, { step: function (now, fx) { var upto20 = (now / 100) * 20 $(this).css('transform', "translate3d(" + upto20 + "px, 0, 0)"); }, duration: 6000, easing: 'linear' , 队列: false }, '线性'); };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多