CSS动画不会渐渐消失
P粉352408038
P粉352408038 2023-08-18 16:45:01
[CSS3讨论组]

我在尝试解决一个简单的动画问题,即在悬停时背景图像淡入淡出。尽管我已经定义了关键帧,但网页浏览器显示未定义:

.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"); }
  }

编辑:无法对背景图像进行动画处理,改为使用不透明度

图像以渐变的方式淡入,但在取消悬停后,它立即切换到没有图像,而不是渐出。

.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; }
  }


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");
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号