scrollTop()if / else switchClass()动画收缩导航栏

时间:2015-07-08 21:38:57

标签: jquery html css nav

我使用jQUery UI方法switchClass(),因为我想控制动画的持续时间。我感到困惑的一件事是switchClass()方法中的识别类。我使用'。'在一个班级之前,但不是为另一个班级?以下是我的代码。事实上,它不起作用。有更优雅的解决方案吗?

 $(document).ready(function(){
    $(window).on("scroll touchmove", function () {
        var x = $(document).scrollTop();
        if( x > 0 ) {
         $('.header').switchClass('header', '.header.tiny', 500);
         $('.logo').switchClass('logo', '.logo.tiny', 500);
        } else {
         $('.logo.tiny').switchClass('tiny', '.logo', 500);
         $('.header.tiny').switchClass('tiny', '.header', 500);
       };
      });
    });

https://jqueryui.com/switchClass/

1 个答案:

答案 0 :(得分:1)

你不需要'。'见:

 $(document).ready(function(){
$(window).on("scroll touchmove", function () {
    var x = $(document).scrollTop();
    if( x > 0 ) {
     $('div.header').switchClass('header', 'tiny', 500);
     $('div.logo').switchClass('logo', 'tiny', 500);
    } else {
     $('div.logo div.tiny').switchClass('tiny', 'logo', 500);
     $('div.header div.tiny').switchClass('tiny', 'header', 500);
   };
  });
});

尝试使用对象定义类,例如:div.header