html - 线性渐变过渡

标签 html css css-transitions linear-gradients

我想在悬停时添加线性渐变过渡。

我做了一些研究并发现了它是如何工作的,但不幸的是它们仅适用于按钮等,而我希望它在图像上。

我使用 css 属性 background-image 添加了图像。我希望当用户将鼠标悬停在图像上时,图像显示带有过渡的线性渐变。

这是代码。

.project-1 { 
    background-image: url("https://cdn.pixabay.com/photo/2018/03/11/20/42/mammals-3218028_960_720.jpg");
    width: 350px;
    height: 250px;
    background-position: center;
    background-size: cover;
    transition: transform 0.5s , opacity 0.5s; 
}

.project-1:hover {
    
    background-image: linear-gradient(rgba(0, 0, 0, 0.39) , rgba(0, 0, 0, 0.39)) , url("https://cdn.pixabay.com/photo/2018/03/11/20/42/mammals-3218028_960_720.jpg");
    background-position: center;
    background-size: cover;
    transform: scale(1.05);
    
}
<div class="project-1"></div>

我在 stackoverflow 上找到的主题有按钮或简单的背景,没有任何图像。

(我在代码中使用的图像仅用于片段)

最佳答案

您不能应用像这样的线性渐变的淡入淡出过渡。另一种方法是在应用不透明度过渡的位置使用伪元素:

.project-1 {
  background-image: url("https://cdn.pixabay.com/photo/2018/03/11/20/42/mammals-3218028_960_720.jpg");
  width: 350px;
  height: 250px;
  background-position: center;
  background-size: cover;
  transition: transform 0.5s, opacity 0.5s;
}

.project-1:before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.39);
  transition: 0.5s;
  opacity: 0;
}

.project-1:hover {
  transform: scale(1.05);
}

.project-1:hover::before {
  opacity: 1;
}
<div class="project-1"></div>

或者,您可以通过设置背景大小或背景位置来实现另一种渐变过渡。这是一个例子:

.project-1 { 
    background-image:
    linear-gradient(rgba(0, 0, 0, 0.39) , rgba(0, 0, 0, 0.39)) , url("https://cdn.pixabay.com/photo/2018/03/11/20/42/mammals-3218028_960_720.jpg");
    width: 350px;
    height: 250px;
    background-position:0 0,center;
    background-size:100% 0%,cover;
    background-repeat:no-repeat;
    transition: 0.5s; 
}

.project-1:hover { 
    background-image: 
    linear-gradient(rgba(0, 0, 0, 0.39) , rgba(0, 0, 0, 0.39)) , url("https://cdn.pixabay.com/photo/2018/03/11/20/42/mammals-3218028_960_720.jpg");
    background-size:100% 100% ,cover;
    transform: scale(1.05);
    
}
<div class="project-1"></div>

关于html - 线性渐变过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49519881/

相关文章:

javascript - transitionend 使用 if 语句触发 +1 事件

html - CSS 3 : Correct "transition-property" for translate operation

javascript - HTML 5 通知在 Chrome 中无法在本地运行?

html - 响应式网页设计中布局更改时 HTML 元素的切换顺序

jquery - 如何在谷歌地图 v3 图标上添加 css 边框

html - 导致父 ul 之间存在间隙的子列表元素

javascript - 如何随着进度条的增长改变颜色

css - HTML/CSS : How to have a container's scrollbar control the scroll of an inner-inner div?

ios - iFrame 在 iOS 上根本不加载

javascript - 滚动后带有过渡效果的导航栏显示