背景大小的过渡效果无法实现

16

我正在尝试对悬停时的背景图像进行过渡效果。 目前这是我的代码:

HTML

<div class="item-one"></div>

CSS

->

CSS(层叠样式表)

.item-one { 
    height: 345px;
    width: 256px;    
    background: url('http://placehold.it/256x345') scroll no-repeat center center;
    background-size: cover;
    -webkit-transition: background-size 1500ms linear;
    -moz-transition: background-size 1500 linear;
    -o-transition: background-size 1500 linear
    -ms-transition: background-size 1500ms linear;
    transition: background-size 1500ms linear;
}

.item-one:hover {
    background-size: 150%;  
}

查看JSFIDDLE

但是这对我不起作用,在不同的浏览器中测试过。其他转换,如背景颜色,按预期工作。这个属性上有任何限制吗?


正如@fsn所提到的,你在第8行缺少了分号。 - areim
3个回答

31

似乎在Chrome中无法正常工作,这就是为什么我认为我的项目没有工作的原因,还有你的JS Fiddle链接。Firefox可以正常显示,但Chrome没有过渡效果。 - Lee
2
如果你想让它在Chrome中工作,你需要给出两个参数。就像这样 background-size:100% 100%; - Carle B. Navy

2
您打错字了:
.item-one { 
...
-o-transition: background-size 1500 linear
...
}

以下是可用版本:
.item-one {
    background-size: 50%;
    webkit-transition: background-size 1500ms linear;
    -moz-transition: background-size 1500 linear;
    -o-transition: background-size 1500 linear;
    -ms-transition: background-size 1500ms linear;
    transition: background-size 1500ms linear;
}

.item-one:hover {
    background-size: 100%;
}

现在工作正常了,之前由于使用了'background-size:cover'导致无法正常工作:

    **‘cover’:**

    Scale the image, while preserving its intrinsic 
    aspect ratio (if any), to the smallest size such 
    that both its width and its height can completely
    cover the background positioning area.

1

尝试使用 transform: scale(150%) 对 item-one 进行缩放,对容器设置特定大小和 overflow: hidden;

<div class=container>
  <div class="item-one"></div>
</div>

样式表:

 .container {
    overflow: hidden;
  }
  .item-one:hover {
   transform: scale(150%);
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接