2016-09-15 64 views
0

我在粘性菜單的動畫有問題。 CSS被改變了,但沒有轉換。任何人都有一個想法,爲什麼?在粘性菜單css過渡不起作用

$(window).scroll(function() { 
    if ($(this).scrollTop() > 50) { 
     $('.navbar-fixed-top').addClass("navbar-fixed-top-sticky"); 
    } else { 
     $('.navbar-fixed-top').removeClass("navbar-fixed-top-sticky"); 
    } 
}); 

和CSS:

.navbar-fixed-top { 
    transition: 0.3 all ease; 
    -webkit-transition: 0.3 all ease; 
} 
.navbar-fixed-top.navbar-fixed-top-sticky { 
    background: #601a36; 
    height: 80px; 
    transition: 0.3 all ease-in-out; 
    -webkit-transition: 0.3 all ease-in-out; 
} 
+0

哪個過渡不工作?您不能從'auto'或從未聲明的值轉換, –

+0

您至少應該添加註釋,以便您不接受答案! @Neuropeptidula – eisbehr

回答

-1

你有沒有轉換髮生的原因是因爲你沒有爲它定義過渡到初始屬性。嘗試這個。

.navbar-fixed-top { 
    transition: 0.3s all ease; 
    -webkit-transition: 0.3s all ease; 

    background-color: transparent; 
    height: 0; 
} 

.navbar-fixed-top.navbar-fixed-top-sticky { 
    background-color: #601a36; 
    height: 80px; 
} 

希望工程!

+0

我不認爲這會奏效。你在轉換期間忘記了。 –

+0

啊,我的壞,我做了一些從OP複製粘貼。編輯。 – AsLittleDesign

+0

但比你的答案還不完全正確。問題不僅在於沒有定義默認值。主要的問題是缺少的單元... – eisbehr

1

您需要在您的計時後添加unit0.3無效,它必須是0.3s300ms之類的東西。 background-color可以工作,但是對於height轉換,您還需要在您的css類中添加一個默認值。

$(window).scroll(function() { 
 
    if($(this).scrollTop() > 50) { 
 
     $('.navbar-fixed-top').addClass("navbar-fixed-top-sticky"); 
 
    } else { 
 
     $('.navbar-fixed-top').removeClass("navbar-fixed-top-sticky"); 
 
    } 
 
});
/* --- just for demo --- */ 
 
.navbar-fixed-top { 
 
    position: fixed; 
 
    width: 100%; 
 
    line-height: 20px; 
 
    background: #ccc; 
 
} 
 
.navbar-fixed-top-sticky { 
 
    line-height: 80px; 
 
} 
 
.content { 
 
    height: 1000px; 
 
} 
 
/* --- just for demo --- */ 
 

 
.navbar-fixed-top { 
 
    height: 20px;        /* default height */ 
 
    transition: 0.3s all ease;    /* added unit */ 
 
    -moz-transition: 0.3s all ease;   /* added unit */ 
 
    -webkit-transition: 0.3s all ease;  /* added unit */ 
 
} 
 
.navbar-fixed-top-sticky { 
 
    background: #601a36; 
 
    height: 80px; 
 
    transition: 0.3s all ease-in-out;   /* added unit */ 
 
    -moz-transition: 0.3s all ease-in-out; /* added unit */ 
 
    -webkit-transition: 0.3s all ease-in-out; /* added unit */ 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="navbar-fixed-top">.navbar-fixed-top</div> 
 
<div class="content"></div>

-3

這應該整理出來。保持CSS,只排除transition部分。

$(window).scroll(function() { 
      if ($(this).scrollTop() > 50) { 
       $('.navbar-fixed-top').addClass("navbar-fixed-top-sticky").fadeIn('slow'); 
      } else { 
     $('.navbar-fixed-top').removeClass("navbar-fixed-top-sticky").fadeOut('slow'); 

    } 
     }); 

,或者你可以嘗試添加時間單位,你應該給你的transitions

.navbar-fixed-top { 
     transition: 0.3s all ease; 
     -webkit-transition: 0.3s all ease; 
    } 

    .navbar-fixed-top.navbar-fixed-top-sticky { 
     background: #601a36; 
     height: 80px; 
     transition: 0.3s all ease-in-out; 
     -webkit-transition: 0.3s all ease-in-out; 
    } 
+0

向下選民,謹慎解釋? – stormec56

+0

可能是因爲你在做一個假設,即需要淡出。在這個問題中沒有任何跡象表明。 –

+0

夠公平的...... – stormec56