简体   繁体   English

覆盖CSS类的动画不起作用

[英]Overriding CSS class for animation does not work

I have the following HTML/CSS/JS. 我有以下HTML / CSS / JS。 I want to disable the keyframe animation for the list-div li childs on mobile devices or screen width below 780px. 我想禁用的关键帧动画list-div li在移动设备上或下方780px屏幕宽度的孩子的。 Because I don't want to mess arround with a JavaScript solution by reading the user agent I thought of overriding the animation class that gets added. 因为我不想通过阅读用户代理来弄乱JavaScript解决方案,所以我想到了覆盖已添加的动画类。
Howevert that does not work. 但是,这不起作用。 So why I want to override the class and disable the animation is because I want to align the li to the corner of the list div, like: li:nth-child(1){top: 0; left:0;} 所以为什么要覆盖该类并禁用动画是因为我想将li对齐到列表div的角,例如: li:nth-child(1){top: 0; left:0;} li:nth-child(1){top: 0; left:0;} by absolute positioning. li:nth-child(1){top: 0; left:0;}通过absolute定位。
Here is the code snippet, I also pasted it in a codepen fo screen width testing: https://codepen.io/anon/pen/JaZgrX 这是代码段,我也将其粘贴到屏幕宽度测试的代码笔中: 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> 

I think this is what you need. 我认为这就是您所需要的。 Switch to full page view to see the animation. 切换到全页视图以查看动画。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM