【问题标题】:CSS transition on background image change of DIVDIV背景图像变化的CSS过渡
【发布时间】:2017-07-09 14:32:25
【问题描述】:

我尝试应用过渡属性以在悬停时图像更改时产生效果,但它似乎不起作用。请看看并帮助我。

.ow-outer {
    background-color: #fff;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 1px solid #fff;
    text-align: center;
    padding: 20px;
    background-image: url(../images/team-canada-light.png);
    background-size: 120px;
    background-repeat: no-repeat;
    background-position: center;
    transition: all 0.3s ease-in-out;
}
.ow-outer:hover {
    background-image: url(../images/team-canada.png);
}

【问题讨论】:

  • 代码中没有过渡。向我们展示你的尝试,不要指望我们为你写东西。
  • 正如@junkfoodjunkie 所说,没有代码 = 没有答案
  • 哦,是的!我的坏..更新了...
  • 它不起作用,过渡只适用于具有整数的属性(高度、宽度、边距、颜色[rgba])
  • 我不知道您在此处发布问题之前是否进行了任何研究,但这里有一个为您提供一些有效解决方案/解决方法的线程:stackoverflow.com/questions/9483364/…

标签: html css


【解决方案1】:

这里是解决方案。使用一些示例中的图像..

.bgImg {
  width: 500px;
  height: 500px;
  background-image: url(http://i.imgur.com/tmM8Bpy.jpg);
  -webkit-transition: background-image 0.5s linear;
  -moz-transition: background-image 0.5s linear;
  -ms-transition: background-image 0.5s linear;
  transition: background-image 0.5s linear;
}

.bgImg:hover {
  background-image: url(http://i.imgur.com/FWqOONj.jpg);
}
<div class="bgImg"></div>

【讨论】:

    【解决方案2】:

    使用background css 属性而不是background-image 属性它将起作用...

        .ow-outer {
        background-color: #fff;
        width: 200px;
        height: 200px;
        border-radius: 50%;
        border: 1px solid #fff;
        text-align: center;
        padding: 20px;
        background: url('https://www.hello.com/img_/hello_logo_hero.png');
        background-size: 120px;
        background-repeat: no-repeat;
        background-position: center;
        transition: all 5s ease-in-out;
    }
    .ow-outer:hover {
        background: url('https://images.chesscomfiles.com/uploads/v1/blog/287126.d6272c47.630x354o.36990d3fc701.png');
    }
    <div class="ow-outer"></div>

    【讨论】:

    • 这没什么区别。
    【解决方案3】:

    background-image 上的转换无法跨浏览器工作,因此请改用伪元素

    使用opacity

    .ow-outer {
        position: relative;
        background-color: #fff;
        width: 200px;
        height: 200px;
        border-radius: 50%;
        border: 1px solid #fff;
        text-align: center;
        padding: 20px;
        background: url(http://placehold.it/200)  no-repeat center;
        background-size: 120px;
    }
    .ow-outer::before {
        content: '';
        position: absolute;
        left: 0; top: 0; right:0; bottom: 0;
        background: url(http://placehold.it/200/f00) no-repeat center;
        background-size: inherit;
        opacity: 0;
        transition: opacity 0.3s ease-in-out;
    }
    .ow-outer:hover::before {
        opacity: 1;
    }
    <div class="ow-outer"></div>

    使用transform: scale

    .ow-outer {
        position: relative;
        background-color: #fff;
        width: 200px;
        height: 200px;
        border-radius: 50%;
        border: 1px solid #fff;
        text-align: center;
        padding: 20px;
        background: url(http://placehold.it/200)  no-repeat center;
        background-size: 120px;
    }
    .ow-outer::before {
        content: '';
        position: absolute;
        left: 0; top: 0; right:0; bottom: 0;
        background: url(http://placehold.it/200/f00) no-repeat center;
        background-size: inherit;
        transform: scale(0);
        transition: transform 0.3s ease-in-out;
    }
    .ow-outer:hover::before {
        transform: scale(1);
    }
    <div class="ow-outer"></div>

    【讨论】:

    • 我测试了opacity 代码,效果很好,这个解决方案很漂亮
    【解决方案4】:

    对过渡使用不透明度。它不适用于图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2018-08-03
      相关资源
      最近更新 更多