javascript - 在滚动条上淡入/淡出背景

标签 javascript jquery css css-selectors

最终元素将是background-image,这个测试是在background-color上进行的。

https://jsfiddle.net/broj3064/17/当您向下滚动时,红色 block 会淡入,但如果您向上滚动,它不会淡出,只是消失了。

HTML

<header>
<nav>
  <ul>
    <li class="up"></li>
  </ul>
</nav>
</header>

<div class="content">

</div>

CSS

header {
  display: block;
  width: 100%;
  position: fixed;
}
nav ul li {
  list-style: none;
  display: block;
  background-color: red;
  width: 50px;
  height: 50px;
}
nav ul li.up {
}
nav ul li.down {
}
.content {
  min-height: 1000px;
}

/* animation */
nav ul li.down {
    -webkit-animation: bummer 0.5s;
    animation: bummer 0.5s;
    -webkit-transform: scale(0,0); 
    transform: scale(0,0);
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
}
@-webkit-keyframes bummer {
    100% {
        -webkit-transform: scale(1); 
    }
}
@keyframes bummer{
    100% {
        transform: scale(1); 
    }
}
nav ul li.up {
    -webkit-animation: bummer2 0.5s;
    animation: bummer2 0.5s;
    -webkit-transform: scale(1,0); 
    transform: scale(1,0);
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
}
@-webkit-keyframes bummer2 {
    100% {
        -webkit-transform: scale(0); 
    }
}
@keyframes bummer2{
    100% {
        transform: scale(0); 
    }
}

jQuery

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();
    if (scroll >= 50) {
        $("li").addClass("down").removeClass("up");
    }
    else {
        $("li").removeClass("down").addClass("up");
    }
});

最佳答案

这是你需要的吗?

$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  
  $("li").toggleClass('show',(scroll >= 50)); /* toggleClass add or remove a class in a div if you pass a boolean to it, in this case, when scroll is greater than 50 wich its true adds the show class to the selector, in this case a $('li'), otherwise it remove the class */
});
header {
  display: block;
  width: 100%;
  position: fixed;
}

nav ul li {
  list-style: none;
  display: block;
  background-color: red;
  width: 50px;
  height: 50px;
  transition:all 400ms; /* This line create an animated transition if any property is overwritten */
    transform:scale(0);
}

nav ul li.show {
  /*Due to css specificity, adding a second class to an item, it overwrite its value, triggering the transition property to animate between old and new value*/
  transform:scale(1);
}

.content {
  min-height: 1000px;
}

/* animation */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <nav>
    <ul>
      <li></li>
    </ul>
  </nav>
</header>

<div class="content">

</div>

如果是,希望对你有帮助

关于javascript - 在滚动条上淡入/淡出背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085005/

相关文章:

jquery - 使用 Jquery 访问特定行

html - 在 CSS/HTML 中是否有语法正确的方法用图标替换文本

IE、Firefox 中的 CSS 定位 "Error"

html - 我怎样才能将内联图像降低 1px 而不是该行的其余部分?

javascript - Jquery:读取具有名称作为关联数组的表单输入文本

javascript - 如何从 vb.net 代码调用 javascript 函数?

jquery - Zepto 更好的条件负载测试

javascript - jquery获取上面div中select的值

javascript - jQuery 中仅允许所有语言的字母数字

javascript - 弹出对话框偏移了太多像素