CSS动画不会渐渐消失
P粉352408038
P粉352408038 2023-08-18 16:45:01
0
1
580
<p>我在尝试解决一个简单的动画问题,即在悬停时背景图像淡入淡出。尽管我已经定义了关键帧,但网页浏览器显示未定义:</p> <pre class="brush:php;toolbar:false;">.elementor-section.elementor-element.core-range > .elementor-container:hover .product-image::before { animation: coreRangeBackground 0.3s ease-in-out 0.3s; animation-fill-mode: forwards; } @keyframes coreRangeBackground { 100% { background-image: url("./images/core-range-bg.svg"); } } .elementor-section.elementor-element.phases-of-the-moon > .elementor-container:hover .product-image::before { animation-name: phasesMoonBackground; animation-duration: 0.3s; animation-timing-function: ease-in-out; animation-delay: 0.3s; animation-fill-mode: forwards; } @keyframes phasesMoonBackground { 100% { background-image: url("./images/phases-of-the-moon-bg.svg"); } }</pre> <p><strong>编辑:无法对背景图像进行动画处理,改为使用不透明度</strong></p> <p>图像以渐变的方式淡入,但在取消悬停后,它立即切换到没有图像,而不是渐出。</p> <pre class="brush:php;toolbar:false;">.elementor-section.elementor-element.product-card > .elementor-container .product-image::before { transition: all 0.3s ease-in-out; position: absolute; width: 100%; height: 100%; background-repeat: no-repeat; background-size: cover; top: 0; left: 0; display: block; content: ""; } .elementor-section.elementor-element.core-range > .elementor-container:hover .product-image::before { animation: productCardBackgroundHover 0.3s ease-in-out; animation-fill-mode: forwards; background-image: url("./images/core-range-bg.svg"); } .elementor-section.elementor-element.phases-of-the-moon > .elementor-container:hover .product-image::before { animation: productCardBackgroundHover 0.3s ease-in-out; animation-fill-mode: forwards; background-image: url("./images/phases-of-the-moon-bg.svg"); } @keyframes productCardBackgroundHover { 0% { opacity: 0; } 100% { opacity: 1; } }</pre> <p><br /></p>
P粉352408038
P粉352408038

全部回复(1)
P粉588660399

所以问题是不透明度没有过渡,这是因为我在悬停时设置了背景图像,所以当没有悬停时,不透明度会过渡消失,然后图像自然地立即消失:

.elementor-section.elementor-element.product-card
   > .elementor-container
   .product-image::before {
   transition: opacity 0.3s ease-in-out;
   position: absolute;
   width: 100%;
   height: 100%;
   background-repeat: no-repeat;
   background-size: cover;
   top: 0;
   left: 0;
   display: block;
   content: "";
   opacity: 0;
}

.elementor-section.elementor-element.core-range
   > .elementor-container
   .product-image::before {
   background-image: url("./images/core-range-bg.svg");
}

.elementor-section.elementor-element.core-range
   > .elementor-container:hover
   .product-image::before, 
.elementor-section.elementor-element.phases-of-the-moon
   > .elementor-container:hover
   .product-image::before {
   opacity: 1;
}

.elementor-section.elementor-element.phases-of-the-moon
   > .elementor-container
   .product-image::before {
   background-image: url("./images/phases-of-the-moon-bg.svg");
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板