用更少的循环优化css?

前端之家收集整理的这篇文章主要介绍了用更少的循环优化css?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过在每次迭代中延迟动画几秒来找出应用级联样式效果的人:
  1. .stashCard {
  2. background-color:white;
  3. }
  4.  
  5. .in(@delay) {
  6. -webkit-animation: swing-in-left-bck .6s cubic-bezier(.175,.885,.32,1.275) @delay both;
  7. animation: swing-in-left-bck .6s cubic-bezier(.175,1.275) @delay both
  8. }
  9.  
  10. .out(@delay) {
  11. -webkit-animation: fade-out .2s ease-out @delay both;
  12. animation: fade-out .2s ease-out @delay both
  13. }
  14.  
  15. .baseKid {
  16. width: 50px;
  17. height: 50px;
  18. display: inline-block;
  19. }
  20.  
  21.  
  22. .selected
  23. {
  24. .kid();
  25. color:yellow;
  26. }
  27.  
  28. .kid {
  29. .out(0s);
  30. .baseKid();
  31. }
  32. .stashCard:hover .kid {
  33. .in(0s);
  34. .baseKid();
  35. }
  36.  
  37. .stashCard:hover .kid.selected {
  38. .in(0s);
  39. .baseKid();
  40. }
  41. .stashCard:hover .kid2.selected {
  42. .in(0.05s);
  43. .baseKid();
  44. }
  45.  
  46. .stashCard:hover .kid2 {
  47. .in(0.05s);
  48. .baseKid();
  49. }
  50.  
  51. .stashCard:hover .kid3.selected {
  52. .in(0.1s);
  53. .baseKid();
  54. }
  55.  
  56. .stashCard:hover .kid3 {
  57. .in(0.1s);
  58. .baseKid();
  59. }
  60.  
  61. .hover {
  62. -webkit-animation: text-shadow-drop-center .6s both;
  63. animation: text-shadow-drop-center .6s both
  64. }
  65. .unhover {
  66. -webkit-animation: untext-shadow-drop-center .6s both;
  67. animation: untext-shadow-drop-center .6s both
  68. }

这就是我应用它的方式:

  1. export const PopupMenu = (props: InputProps) => {
  2. return <div className="menu" style={props.style}>
  3. <VoteOption count="actors" className={props.selectedCounts.indexOf("actors") >= 0 ? "selected kid" : "kid"} onClick={props.onClick} icon="sentiment_very_satisfied" tip="Real actors" />
  4. <VoteOption count="audio" className={props.selectedCounts.indexOf("audio") >= 0 ? "selected kid2" : "kid2"} onClick={props.onClick} icon="music_video" tip="Great audio quality" />
  5. <VoteOption count="picture" className={props.selectedCounts.indexOf("picture") >= 0 ? "selected kid3" : "kid3"} onClick={props.onClick} icon="photo_camera" tip="Great picture quality" />
  6. </div>;
  7. }

显然这是低效的,需要大量的复制粘贴,有没有一种方法可以让我可以添加尽可能多的投票选项,少可以写出css,它将遍历所有子组件并应用正确的偏移量开始时间?

解决方法

您可以使用循环来实现它:
  1. .in(@delay) {
  2. -webkit-animation: swing-in-left-bck .6s cubic-bezier(.175,1.275) @delay both
  3. }
  4.  
  5. .out(@delay) {
  6. -webkit-animation: fade-out .2s ease-out @delay both;
  7. animation: fade-out .2s ease-out @delay both
  8. }
  9.  
  10. .baseKid {
  11. width: 50px;
  12. height: 50px;
  13. display: inline-block;
  14. }
  15.  
  16.  
  17. .loop(@counter) when (@counter > 0) {
  18. .loop((@counter - 1)); // next iteration
  19. .kid@{counter} {
  20. .in(0.05s * (@counter - 1));
  21. .baseKid();
  22. }
  23. .kid@{counter}.seleted {
  24. width: (10px * @counter); // code for each iteration
  25. }
  26. }
  27.  
  28. .stashCard:hover {
  29. .loop(5); // launch the loop
  30. }

猜你在找的CSS相关文章