css 过渡不适用于渐变?

时间:2021-05-04 22:58:05

标签: css

.body {
    background-color: #18181a;
    color: white;
    font-size: 2.5rem;
    margin-left: 0%;
    border-radius: 5px;
}

.body  .card{
    background:linear-gradient(90deg, rgba(0,0,0,1) 0%, rgba(0,212,255,0) 100%),url("./images/yuuki_mikan_bg.jpg");
    background-repeat: no-repeat;
    background-position: right;

    width: 90%;
    height: 80%;
    border-radius: 10px;
    margin: 10px;
    margin-left: auto;
    margin-right: auto;
    display: flex;
    justify-content: space-between;
    flex-direction: row-reverse;

    transition: background 0.5s linear;
}

.body .card:hover {
    background:linear-gradient(90deg, rgba(0,0,0,0.17690826330532217) 0%, rgba(0,212,255,0) 100%),url("./images/yuuki_mikan_bg.jpg");
    background-position: right;
}

.body  .card .card_img{
    max-width: 80%;
    max-height: 95%;
    border-radius: 5px;
    /* bottom: 9px; */
    /* left: -10px; */
    /* top: -10px; */
    transition: 0.5s;
    opacity: 1;
    display: flex;
    justify-content: flex-start;
    align-self: center;
}

.body  .card .card_name{
    margin-right: 2%;
    margin-top: auto;
    margin-bottom: auto;
    font-size: 2.5rem;
    text-shadow: 0px 0px 18px black;
    opacity: 1;
}

这是我使用的代码,每当我将鼠标悬停在 .card 上时,它都会更改背景颜色,但过渡不会延迟其过渡时间。如果您需要更多上下文,这是我的代码在 https://oniichann.tk/waifus

上运行的网站

1 个答案:

答案 0 :(得分:1)

我相信背景渐变上唯一的动画属性是 background-position

您可以通过执行以下操作来实现您的目标:

.card {
  position: relative;
  text-align: center;
  color: white;
  padding: 100px;
  background: url("https://oniichann.tk/waifus/images/bg.jpg");
  background-repeat: no-repeat;
  background-position: right;
  background-size: cover;
}

.card:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 0) 100%
  );
  background-size: 200% 200%;
  transition: background-position 0.5s linear;
}

.card:hover:before {
  background-position: 100% 50%;
}
<div class="card"></div>