Fade in Box Shadow using CSS 3?

2019-04-13 14:37发布

I have a script that as soon as the user starts to scroll a box shadow is added that looks very nice. However, this box shadow is added instantly. I would prefer that it fade in using CSS 3. I have tried creating keyframes that change the opacity from 0 - 1 over 1 second but that doesn't work.

Here is the script I am using:

$(function() {
 $(window).scroll(function() {
    var top_offset = $(window).scrollTop();
    if (top_offset) {
      $('.top_head_separator').addClass('fixed-top fade-in');
  }
});

CSS:

.fixed-top {
  background:#FFFFFF;
  box-shadow: 0 7px 15px 4px rgba(0,0,0,0.38);
  height: 90px;
  overflow: hidden;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 9999;

}
@keyframes fadeIn {
  0% {opacity: 0;}  
  100% {opacity: 1;}
}

.fadeIn {
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-name: fadeIn;
}

How do I have the box shadow fade in?

Note: I omitted vendor prefixes in this question but they are in my code.

1条回答
我只想做你的唯一
2楼-- · 2019-04-13 14:57

I think you just have a spelling mistake and a syntax error or two, otherwise you're fine. Two things:

  • Close both functions in your jQuery.
  • Your CSS mentions fadeIn, but jQuery had fade-in

Here's the new, fixed jQuery code:

$(function() {
 $(window).scroll(function() {
    var top_offset = $(window).scrollTop();
    if (top_offset) {
      $('.top_head_separator').addClass('fixed-top fadeIn'); // <<<< "fadeIn"
    }
 }); // <<<< ADDED
});

See this -webkit- demo for a working example.

查看更多
登录 后发表回答