html - 覆盖动画的 CSS 类不起作用

标签 html css css-specificity

我有以下 HTML/CSS/JS。我想在移动设备或屏幕宽度低于 780 像素的情况下禁用 list-div li 子元素的关键帧动画。因为我不想通过阅读用户代理来弄乱 JavaScript 解决方案,所以我想到了覆盖添加的动画类。
然而那是行不通的。所以为什么我要重写类并禁用动画是因为我想将 li 对齐到列表 div 的 Angular ,例如:li:nth-child(1){top : 0; left:0;}绝对定位。
这是代码片段,我也将它粘贴在用于屏幕宽度测试的代码笔中:https://codepen.io/anon/pen/JaZgrX

     current = 1;
      $(".list-div ul li").each(function(){
        $(this).addClass("animate").css("animation-delay", current + 's');
        current++;
      });
body {
  margin:0;
}
.container {
  display:flex;
  flex-wrap:wrap;    
  flex-direction:row;    
  height:100vh;
  background-color: beige;
}
.container > div {
  min-height: 100vh;
  border:1px solid black;
  box-sizing:border-box;
  background-color: inherit;
}
.half-width {
  width:50%;
}
.half-width > .half-width-content{
  position: relative;
  margin-top: 0;
  height: 100%;
  width: 100%;
}
.list-div {
  display: flex;
  justify-content: center;
  align-items: center;
}

.list-div ul {
  padding: 0;
  margin-top: 15%;
  width: 75%;
}

.list-div li {
  position: relative;
  display: block;
  border: 1px solid red;
  margin-bottom: 5px;
  padding: 10px;
  text-align: center;
  text-transform: uppercase;
  visibility: hidden;
}

.list-div li.animate{
  visibility: visible;
  animation: fadeIn 1s linear;
  animation-fill-mode: both;
}

@-webkit-keyframes fadeIn {
  0% {
    opacity: 0;
    top: 220px;
  }
  25%{
    opacity: 0.3;
  }
  
  75% {
    opacity: 0.5;
    top: 0px;
  }
  100% {
    opacity: 1;
  }
}

@media max-width:768px{
  
  
  .list-div li.animate{
  visibility: visible;
}
  .list-div {
  display: flex;
  justify-content: center;
  align-items: center;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="half-width">
    <div class="half-width-content" id="list-cont">
      <div class="list-div">
        <ul>
          <li>Entry A</li>
          <li>Entry B</li>
          <li>Entry C</li>
          <li>Entry D</li>
        </ul>
      </div>
    </div>
  </div>
</div>

最佳答案

我认为这就是您所需要的。切换到完整页面 View 以查看动画。

current = 1;
$(".list-div ul li").each(function() {
  $(this).addClass("animate").css("animation-delay", current + 's');
  current++;
});
body {
  margin: 0;
}

.container {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  height: 100vh;
  background-color: beige;
}

.container>div {
  min-height: 100vh;
  border: 1px solid black;
  box-sizing: border-box;
  background-color: inherit;
}

.half-width {
  width: 50%;
}

.half-width>.half-width-content {
  position: relative;
  margin-top: 0;
  height: 100%;
  width: 100%;
}

.list-div {
  display: flex;
  justify-content: center;
  align-items: center;
}

.list-div ul {
  padding: 0;
  margin-top: 15%;
  width: 75%;
}

.list-div li {
  position: relative;
  display: block;
  border: 1px solid red;
  margin-bottom: 5px;
  padding: 10px;
  text-align: center;
  text-transform: uppercase;
  visibility: hidden;
}

.list-div li.animate {
  visibility: visible;
  animation: fadeIn 1s linear;
  animation-fill-mode: both;
}

@-webkit-keyframes fadeIn {
  0% {
    opacity: 0;
    top: 220px;
  }
  25% {
    opacity: 0.3;
  }
  75% {
    opacity: 0.5;
    top: 0px;
  }
  100% {
    opacity: 1;
  }
}

@media (max-width:768px) {
  .list-div ul {
    margin-top: 0;
  }
  
  .list-div li.animate {
    visibility: visible;
    animation: none;
  }
  .list-div {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="half-width">
    <div class="half-width-content" id="list-cont">
      <div class="list-div">
        <ul>
          <li>Entry A</li>
          <li>Entry B</li>
          <li>Entry C</li>
          <li>Entry D</li>
        </ul>
      </div>
    </div>
  </div>
</div>

关于html - 覆盖动画的 CSS 类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52326549/

相关文章:

html - 我认为链接 div id 在 html 位置不起作用

javascript - 处理焦点行的按键事件

html - 垂直对齐 : Middle

html - Bootstrap Accordion - 开始全部关闭

css - 嵌套表格可能的 CSS 特异性和继承错误

CSS 特异性 : ID overwritten by class + element

css - 所有样式都应该只在 .css 文件中吗?

php - 我怎样才能在保留回复的同时移动我的评论?

javascript - 这个 jQuery "on-mouse-over-scroll-the-image"插件是什么?

Css 特异性——样式表中链接样式的问题