javascript – CSS动画宽度从右到左,高度从下到上

前端之家收集整理的这篇文章主要介绍了javascript – CSS动画宽度从右到左,高度从下到上前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图解决从右到左扩展div的宽度和从下到上的不同容器的高度的问题.我正在尝试创建将在正方形旋转的CSS动画,并且模仿这里的边框是我的CodePen https://codepen.io/Archezi/pen/xReqvq?editors=0100链接,如果有帮助的话.

这是我的HTML .container是一个主要包装.circle是一个动画line1-line4是正方形的边框,我正在尝试动画.

  1. <div class="container">
  2. <div class="circle "></div>
  3. <div class="line1 "></div>
  4. <div class="line2 "></div>
  5. <div class="line3 "></div>
  6. <div class="line4 "></div>
  7. </div>

这是我的CSS

  1. body {
  2. margin: 0 auto;
  3. }
  4. .container {
  5. position:relative;
  6. margin: 50px auto;
  7. width: 800px;
  8. height:800px;
  9. border:1px solid #000;
  10. }
  11. .circle {
  12. display:inline-block;
  13. width: 25px;
  14. height: 25px;
  15. border-radius:50%;
  16. position: absolute;
  17. top:100px;
  18. left:100px;
  19. background:#000;
  20. animation: myframes 4s ease-in-out 0s infinite;
  21.  
  22. /* animation-name: myanimation;
  23. animation-duration:5s;
  24. animation-timing-function:ease-in-out;
  25. animation-delay: 0s;
  26. animaiton-iteration-count: infinite;
  27. animation-direction: normal;
  28. animation-fill-mode: forwards;
  29. animation-play-state: running; */
  30. }
  31. .line1 {
  32. height:15px;
  33. width:15px;
  34. position:absolute;
  35. top:105px;
  36. left:105px;
  37. background:red;
  38. animation: squerframe 2s ease-in-out 0s infinite;
  39. }
  40. .line2 {
  41. height:15px;
  42. width:15px;
  43. position:absolute;
  44. top:105px;
  45. left:205px;
  46. background:green;
  47. animation: squerframe2 2s ease-in-out 1s infinite;
  48. }
  49. .line3 {
  50. height:15px;
  51. width:15px;
  52. position:absolute;
  53. top:205px;
  54. left:205px;
  55. background-color:red;
  56. animation: squerframe3 2s ease-in-out 2s infinite;
  57. }
  58.  
  59. .line4 {
  60. height:15px;
  61. width:15px;
  62. position:absolute;
  63. top:205px;
  64. left:105px;
  65. background:green;
  66. animation: squerframe4 2s ease-in-out 3s infinite;
  67. }
  68. @Keyframes squerframe
  69. {
  70. 0% { width:15px; opacity: 1; }
  71. 50% { width:115px; }
  72. 100%{ width:115px; opacity: 0; }
  73. }
  74. @Keyframes squerframe2
  75. {
  76. 0% { height:15px; opacity: 1; }
  77. 50% { height:115px; }
  78. 100%{ height:115px; opacity: 0; }
  79. }
  80. @Keyframes squerframe3
  81. {
  82. 0% { width:115px; opacity: 0;}
  83. 50% { width:115px; }
  84. 100%{ width:15px; opacity: 1; }
  85. }
  86. @Keyframes squerframe3
  87. {
  88. 0% { width:15px; opacity: 1;}
  89. 50% { width:115px; }
  90. 100%{ width:115px; opacity: 0; }
  91. }
  92. @Keyframes squerframe4
  93. {
  94. 0% { height:15px; opacity: 1;}
  95. 50% { height:115px; }
  96. 100%{ height:115px; opacity: 0; }
  97. }
  98.  
  99. @keyframes myframes
  100. {
  101. 0% { top:100px; left:100px; }
  102. 25% { top:100px; left:200px; }
  103. 50% { top:200px; left:200px; }
  104. 75% { top:200px; left:100px; }
  105. 100% { top:100px; left:100px; }
  106. }

请指导我在哪里找到解决方案或指出我对这个问题的不同方法.谢谢!

解决方法

这里的问题是你需要line3具有绝对权限,这样当宽度改变时,它将向左伸展.

另外,第4行应该有一个绝对的底部,所以它会伸展.

我建议您添加一个容器或更改当前div.container的维度(就像我在示例中所做的那样)为四行,并使用4行作为该容器的极值.

以下是您修改的示例,作为如何继续的参考点:

https://codepen.io/anon/pen/MbRvGM?editors=0100

  1. body {
  2. margin: 0 auto;
  3. }
  4. .container {
  5. position:relative;
  6. margin: 50px auto;
  7. width: 115px;
  8. height:115px;
  9. border:1px solid #000;
  10. }
  11. .circle {
  12. display:inline-block;
  13. width: 25px;
  14. height: 25px;
  15. border-radius:50%;
  16. position: absolute;
  17. top:0px;
  18. left:0px;
  19. background:#000;
  20. animation: myframes 4s ease-in-out 0s infinite;
  21.  
  22. /* animation-name: myanimation;
  23. animation-duration:5s;
  24. animation-timing-function:ease-in-out;
  25. animation-delay: 0s;
  26. animaiton-iteration-count: infinite;
  27. animation-direction: normal;
  28. animation-fill-mode: forwards;
  29. animation-play-state: running; */
  30.  
  31.  
  32.  
  33. }
  34. .line1 {
  35. height:15px;
  36. width:15px;
  37. position:absolute;
  38. top:0px;
  39. left:0px;
  40. background:red;
  41. animation: squerframe 2s ease-in-out 0s infinite;
  42. }
  43. .line2 {
  44. height:15px;
  45. width:15px;
  46. position:absolute;
  47. top:0px;
  48. left:100px;
  49. background:green;
  50. animation: squerframe2 2s ease-in-out 1s infinite;
  51. }
  52. .line3 {
  53. height:15px;
  54. width:15px;
  55. position:absolute;
  56. top:100px;
  57. right:0px;
  58. float: right;
  59. background-color:red;
  60. animation: squerframe3 2s ease-in-out 2s infinite;
  61. }
  62.  
  63. .line4 {
  64. height:15px;
  65. width:15px;
  66. position:absolute;
  67. left:0px;
  68. bottom: 0;
  69. background:green;
  70. animation: squerframe4 2s ease-in-out 3s infinite;
  71. }
  72. @Keyframes squerframe
  73. {
  74. 0% { width:15px; opacity: 1; }
  75. 50% { width:115px; }
  76. 100%{ width:115px; opacity: 0; }
  77. }
  78. @Keyframes squerframe2
  79. {
  80. 0% { height:15px; opacity: 1; }
  81. 50% { height:115px; }
  82. 100%{ height:115px; opacity: 0; }
  83. }
  84. @Keyframes squerframe3
  85. {
  86. 0% { width:115px; opacity: 0;}
  87. 50% { width:115px; }
  88. 100%{ width:15px; opacity: 1; }
  89. }
  90. @Keyframes squerframe3
  91. {
  92. 0% { width:15px; opacity: 1;}
  93. 50% { width:115px; }
  94. 100%{ width:115px; opacity: 0; }
  95. }
  96. @Keyframes squerframe4
  97. {
  98. 0% { height:15px; opacity: 1;}
  99. 50% { height:115px; }
  100. 100%{ height:115px; opacity: 0; }
  101. }
  102.  
  103. @keyframes myframes
  104. {
  105. 0% { top:0px; left:0px; }
  106. 25% { top:0px; left:100px; }
  107. 50% { top:100px; left:100px; }
  108. 75% { top:100px; left:0px; }
  109. 100% { top:0px; left:0px; }
  110. }

猜你在找的JavaScript相关文章