【问题标题】:On click fade out div then fade back in单击淡出 div 然后淡入
【发布时间】:2015-03-16 16:35:43
【问题描述】:

我已经实现了一个切换视图页面,您可以在其中在列表和网格之间切换,切换状态之间的动画有点难看(使用顶部的两个链接切换以了解我的意思):http://jsfiddle.net/qvLoLhy7/2/

为了解决这个问题,我尝试在切换按钮上应用 setTimeout 函数,以便我可以先淡出父 div #post-list,然后执行布局更改,最后淡入 #post-list再次显示更新的布局。当我实现这个时,虽然淡入/淡出工作但布局没有改变:http://jsfiddle.net/qvLoLhy7/

谁能帮忙找出为什么会发生这种情况?

这是 JS 的 sn-p:

function init() {
    optionSwitch.forEach( function( el, i ) {
        el.addEventListener( 'click', function( ev ) {

            $('#post-list').addClass('go'); // Fade out

            setTimeout(function() {
                _switch( this ); //Update layout. Switch refers to another function
            }, 500);


            setTimeout(function() {
                 $('#post-list').removeClass('go'); // Fade new layout back in
            }, 1000);

        }, false );
    } );
}

【问题讨论】:

    标签: javascript jquery css settimeout


    【解决方案1】:

    您正在使用 jquery,但您并没有真正使用 jquery。 Jquery 附带了一个可以使用的淡入淡出动画。

    var $pl = $('#post-list');
    $pl.fadeOut('slow', function(){
      //call back when animation is done
      //here you can apply layout changes
      $pl.fadeIn('slow', function(){
         //call back when the fade in animation is done
         //do other stuff here
      });
    });
    

    Here 是一个基本示例。

    您还应该将您的代码转换为等效的 jquery。

    如果这就是您要找的,请告诉我。

    【讨论】:

    • 对于像我这样的新手来说,这对我来说没有多大意义,尽管我确信对于经验丰富的专业人士来说确实如此!我看不出如何将我的代码与您的代码合并以实现我需要的解决方案。你能提供更多的细节给我吗?
    猜你喜欢
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2012-09-17
    • 1970-01-01
    相关资源
    最近更新 更多