简体   繁体   English

使用jQuery进行淡入/淡出的动画效果

[英]animated effect for fadein/fadeout using jQuery

To understand my code please visit this page: 要了解我的代码,请访问以下页面:

Please click on packaging filter first 请先点击包装过滤器

http://codepen.io/mariomez/pen/qNrzAr?editors=0010 http://codepen.io/mariomez/pen/qNrzAr?editors=0010

It's a simplified animated filtering method. 这是一种简化的动画过滤方法。

Each red box might have more than one classes as an identifier for the filter. 每个红色框可能有多个类作为过滤器的标识符。 My goal with this code is to achieve a nice animated way for fade-in and for fade-out. 我用此代码的目标是为淡入和淡出实现一种不错的动画方式。 For now I managed to do this only for fade-in. 现在,我仅对淡入淡出做到了这一点。

Even though I wrote the animation for fade-out I can't use it properly in the JS code. 即使我编写了淡出动画,也无法在JS代码中正确使用它。

Example for 1 filter: I want all classes except "packaging" to fade-out nicely and have the packaging class fade-in. 1个过滤器的示例:我希望除“ packaging”外的所有类都很好地淡出,并使包装类淡入。

jQuery CODE jQuery代码

    $(document).ready(function(){
    $(".filter-logo").click(function(){
        $(".all").fadeOut(normal,addClass('animated fadeOutEffect'));
        $(".logo").fadeIn(normal,addClass('animated fadeInEffect'));
    });
    $(".filter-website").click(function(){
        $(".all").fadeOut();
        $(".website").fadeIn().addClass('animated fadeInEffect');
    });

    $(".filter-packaging").click(function(){
        $(".all").fadeOut();
        $(".packaging").fadeIn().addClass('animated fadeInEffect');
    });

    $(".filter-forsale").click(function(){
        $(".all").fadeOut();
        $(".forsale").fadeIn().addClass('animated fadeInEffect');
    });

    $(".filter-all").click(function(){
        $(".all").fadeOut();
        $(".logo, .website, .packaging, .forsale, .all").fadeIn().addClass('animated fadeInEffect');
    });
});

Trying to use the fade-in animation: (FAILED) 尝试使用淡入动画:(FAILED)

$(".all").not('.packaging').fadeOut().addClass('animated  fadeOutEffect');
        $(".packaging").fadeIn().addClass('animated fadeInEffect');
    });

How can I improve this code? 如何改善此代码?

I have updated your example. 我已经更新了您的示例。 http://codepen.io/jammer99/pen/mEQabN Essentially you need to set fadeout to finish within 0 seconds forcefully, additionally since you have already used css to generate the animation, you should use hide() and show() instead of fadeOut() and fadeIn() http://codepen.io/jammer99/pen/mEQabN本质上,您需要将淡出设置为强制在0秒内完成,此外,由于您已经使用CSS来生成动画,因此应该使用hide()show()代替fadeOut()fadeIn()

here's the updated code 这是更新的代码

  $(document).ready(function() {
      $(".all").each(function() {
        $(this).addClass("animated")
      })
      $(".filter-logo").click(function(e) {
        e.preventDefault();
        $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect").hide(0);
        $(".logo").show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
      });

      $(".filter-website").click(function(e) {
        e.preventDefault();
        $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect").hide(0)
        $(".website").show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
      });

      // UPDATE CODE - START ////////////
      $(".filter-packaging").click(function(e) {
        e.preventDefault();
        $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect").hide(0);
        $(".packaging").show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');

      });
      // UPDATE CODE - END ////////////

      $(".filter-forsale").click(function(e) {
        e.preventDefault();
        $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect").hide(0);
        $(".forsale").show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
      });

      $(".filter-all").click(function(e) {
        e.preventDefault();
        $(".all").removeClass('fadeOutEffect').addClass("fadeInEffect").show(0)
      });
    });

EDIT : Here is updated code. 编辑 :这是更新的代码。 http://codepen.io/jammer99/pen/mEQabN?editors=0010 http://codepen.io/jammer99/pen/mEQabN?editors=0010

$(document).ready(function() {
  $(".all").each(function() {
    $(this).addClass("animated")
  })
  $(".filter-logo").click(function(e) {
    e.preventDefault();
    $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect")
    setTimeout(function() {
      $(".all").hide(0)
      $(".logo").stop().show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
    }, 500);
  });

  $(".filter-website").click(function(e) {
    e.preventDefault();
    $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect")
    setTimeout(function() {
      $(".all").hide(0)
      $(".website").stop().show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
    }, 500);
  });

  $(".filter-packaging").click(function(e) {
    e.preventDefault();
    $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect")
    setTimeout(function() {
      $(".all").hide(0)
      $(".packaging").stop().show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
    }, 500);
  });

  $(".filter-forsale").click(function(e) {
    e.preventDefault();
    $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect")
    setTimeout(function() {
      $(".all").hide(0)
      $(".forsale").stop().show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
    }, 500);
  });

  $(".filter-all").click(function(e) {
    e.preventDefault();

    $(".all").removeClass('fadeInEffect').addClass("fadeOutEffect")
    setTimeout(function() {
      $(".all").show(0).removeClass("fadeOutEffect").addClass('fadeInEffect');
    }, 500);
  });
});

For your Example for 1 filter, you're saying you want "packaging" to be the only one to not fade out, but then have "packaging" fade in even though it didn't fade out? 对于您的Example for 1过滤器,您要说“包装”是唯一不淡出的过滤器,但是即使“包装”没有淡出也要淡入吗?

I'm going to assume you want everything to fade out, then have "packaging" fade in. You can do so by calling the fadeIn for "packaging" inside a callback when you fadeOut .all . 我会假设你想要的一切淡出,则有“包装”淡入。您可以通过调用这样做fadeIn的“包装”的回调时,你的内心fadeOut .all

Edit: 编辑:

So the fadeOut function takes a completion callback, a function that will run after the fadeOut animation completes. 因此, fadeOut函数接受完成回调,该函数将在fadeOut动画完成后运行。 You just need to pass in an anonymous function to the fadeOut parameter, and inside that function do what you want to do after the animation completes: 您只需要将匿名函数传递给fadeOut参数,并在动画完成后在该函数内执行您想做的事情:

$(".filter-packaging").click(function(){
    $(".all").fadeOut(function() {
        $(".packaging").fadeIn().addClass('animated fadeInEffect');
    });
});

Basically your css and js animations are clashing. 基本上,您的CSS和JS动画会发生冲突。 You should probably stick to one or the other. 您可能应该坚持一个或另一个。 Also both js animations are running at the same time fadeIn and fadeOut . 同样,两个js动画都同时运行fadeInfadeOut

An option is to wait until the fadeOut animation ends before starting the fadeIn animation. 一种选择是等到fadeOut动画结束后再开始fadeIn动画。 You can do that with a timeout. 您可以超时进行操作。

In the example below the function hideThenShow waits until the of the fadeOut animation (500ms) then runs the fadeIn on the selected elements. 在下面的示例中,函数hideThenShow一直等到fadeOut动画的结束时间(500ms),然后在选定的元素上运行fadeIn

As a bonus you could loop through your filters and your list elements instead of repeating the hideThenShow function in the example. 另外,您可以遍历过滤器和列表元素,而hideThenShow在示例中重复hideThenShow函数。

Hope it helps 希望能帮助到你

 function hideThenShow($clickedElement, $elementsToShow){ var duration = 500; $clickedElement.click(function(){ $('.all').fadeOut(duration); setTimeout(function(){ $elementsToShow.fadeIn(); },duration); }); } $(document).ready(function(){ var filterLogo = $(".filter-logo"); var filterWebsite = $(".filter-website"); var filterPackaging = $('.filter-packaging'); var filterForsale = $('.filter-forsale'); var filterAll = $(".filter-all"); var websiteElemetns = $('.website'); var logoElements = $(".logo"); var packagingElements = $('.packaging'); var forsaleElements = $('.forsale'); var allElements = $(".all"); hideThenShow(filterLogo, logoElements); hideThenShow(filterWebsite, websiteElemetns); hideThenShow(filterPackaging, packagingElements); hideThenShow(filterForsale, forsaleElements); hideThenShow(filterAll, allElements); }); 
 .ullist li {width:100px;background:#f00; height:100px; display:block; float:left;clear:none; margin:25px;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="secmenu"> <ul> <a href="#" class="filter-all"><li>All</li></a> <a href="#" class="filter-logo"><li>Logo</li></a> <a href="#" class="filter-website"><li>Website</li></a> <a href="#" class="filter-packaging"><li>Packaging</li></a> <a href="#" class="filter-forsale"><li>For sale</li></a> </ul> </div> <ul class="ullist"> <li class="website all">text</li> <li class="website all">text</li> <li class="website all">text</li> <li class="packaging all">text</li> <li class="packaging all">text</li> <li class="logo all">text</li> <li class="logo all">text</li> <li class="logo all">text</li> <li class="logo all">text</li> <li class="logo all">text</li> <li class="forsale all">text</li> </ul> 

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

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