如何實現錐形漸層動畫的顏色平滑過渡?
P粉285587590
P粉285587590 2023-09-01 16:37:22
0
1
501
<p>我正在製作一個錐形漸變的動畫,但顏色變化不夠平滑 我看過各種文章,但無法正確複製它們 你可以在這裡找到我的程式碼:</p> <pre class="brush:php;toolbar:false;"><style> body{ min-height: 100vh; display: flex; align-items: center; justify-content: center; } .pyramid { width: 500px; height: 500px; background-image: conic-gradient(red 135deg, green 135deg,yellow 165deg, yellow 165deg); clip-path: polygon(50% 50%, 0% 100%, 100% 100%); animation: change 1s ease infinite; } .pyramid:hover{ background-image: conic-gradient(red 135deg, #cadfca 135deg,#dddd3f 165deg, #aeae30 165deg); } @keyframes change{ 0%{ background-image: conic-gradient(red 135deg, yellow 135deg,yellow 165deg, lightpink 165deg); /*background: red;*/ } 50%{ background-image: conic-gradient(red 135deg, #cadfca 135deg,#dddd3f 165deg, #aeae30 165deg); } 100%{ background-image: conic-gradient(red 135deg, #565644 135deg,#838314 165deg, #a68f03 165deg) } } </style> </head> <body> <div class="pyramid"> </div> </body></pre> <p>如何使其變化更平滑</p>
P粉285587590
P粉285587590

全部回覆(1)
P粉605233764

背景圖像在您想要的方式中無法平滑地進行動畫處理。

然而,您可以平滑地動畫化不透明度,因此一種獲得效果的方法是將兩個背景圖像放在彼此上方,並改變它們的不透明度,使它們逐漸從一個變為另一個。

在您的情況下,您可以將背景圖像放在偽元素的before和after上,而不是放在元素本身上。

它們具有相同的動畫效果,但一個從中間開始(當不透明度為1時)。

當總持續時間只有1秒鐘時,很難意識到這種混合效果,因此此程式碼片段增加了持續時間,以便您可以看到效果。

當然,您可以根據需要更改時序。

更改還是線性的,以便覆蓋始終提供“整體”圖像而不是半透明圖像。

<style>
  body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .pyramid {
    width: 500px;
    height: 500px;
    clip-path: polygon(50% 50%, 0% 100%, 100% 100%);
    position: relative;
  }
  
  .pyramid::before,
  .pyramid::after {
    position: absolute;
    content: '';
    width: 100%;
    height: 100%;
    --duration: 10s; /* set the duration to whatever you want */
    animation: change var(--duration) linear infinite forwards;
    opacity: 0;
  }
  
  .pyramid::after {
    animation-delay: calc(var(--duration) / -6);
  }
  
  @keyframes change {
    0% {
      background-image: conic-gradient(red 135deg, yellow 135deg, yellow 165deg, lightpink 165deg);
      opacity: 0;
    }
    16.666% {
      opacity: 1;
      background-image: conic-gradient(red 135deg, yellow 135deg, yellow 165deg, lightpink 165deg);
    }
    32% {
      opacity: 0;
      background-image: conic-gradient(red 135deg, yellow 135deg, yellow 165deg, lightpink 165deg);
    }
    33.333% {
      background-image: conic-gradient(red 135deg, #cadfca 135deg, #dddd3f 165deg, #aeae30 165deg);
      opacity: 0;
    }
    50% {
      opacity: 1;
      background-image: conic-gradient(red 135deg, #cadfca 135deg, #dddd3f 165deg, #aeae30 165deg);
    }
    65% {
      opacity: 0;
      background-image: conic-gradient(red 135deg, #cadfca 135deg, #dddd3f 165deg, #aeae30 165deg);
    }
    66.666% {
      opacity: 0;
      background-image: conic-gradient(red 135deg, #565644 135deg, #838314 165deg, #a68f03 165deg)
    }
    83.333% {
      opacity: 1;
      background-image: conic-gradient(red 135deg, #565644 135deg, #838314 165deg, #a68f03 165deg)
    }
    99%,
    100% {
      opacity: 0;
      background-image: conic-gradient(red 135deg, #565644 135deg, #838314 165deg, #a68f03 165deg)
    }
  }
  
  @keyframes change2 {
    0% {
      background-image: conic-gradient(red 135deg, yellow 135deg, yellow 165deg, lightpink 165deg);
      /*background: red;*/
    }
    0% {
      background-image: conic-gradient(red 135deg, #cadfca 135deg, #dddd3f 165deg, #aeae30 165deg);
    }
    50% {
      background-image: conic-gradient(red 135deg, #565644 135deg, #838314 165deg, #a68f03 165deg)
    }
  }
</style>
</head>

<body>
  <div class="pyramid">
  </div>
</body>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板