背景大小转换在 Chrome 中不起作用
Background-size transition not work in Chrome
我正在尝试转换 background-size
和 background-color
。
- Chrome:
background-size
的转换无效
- Firefox:两者都工作正常
我还创建了一个fiddle。
.highlight {
display: block;
position: relative;
/*min-height: 800px;*/
min-height: 200px;
background-position: center center;
background-repeat: no-repeat;
/*padding-top: 200px;*/
padding-top: 80px;
/*background-size: cover;*/
}
.highlight:before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .25);
content: "";
}
.highlight {
position: relative;
height: 200px;
cursor: pointer;
background-size: auto 110%;
background-position: center center;
-moz-transition: background-size 3s ease;
-webkit-transition: background-size 3s ease;
transition: background-size 3s ease;
}
.highlight:hover {
background-size: auto 130%;
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: background-size 3s ease;
-webkit-transition: background-size 3s ease;
transition: background-size 3s ease;
}
.highlight:before {
content: ' ';
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
.highlight:hover:before {
background-color: rgba(0, 0, 0, 0.8);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
<div class="highlight" style="background-image:url(http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg);">
</div>
有人帮我解决这个问题吗?或者可以确定正在打破这些转变?
谢谢
可以使用transform: scale(1.3)
和transition: all
实现背景大小过渡效果。
已更新 fiddle。
注意:我还添加了一个 wrapper 来隐藏溢出。
.container {
overflow: hidden;
}
.highlight {
display: block;
position: relative;
/*min-height: 800px;*/
min-height: 200px;
background-position: center center;
background-repeat: no-repeat;
/*padding-top: 200px;*/
padding-top: 80px;
/*background-size: cover;*/
}
.highlight:before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .25);
content: "";
}
.highlight {
position: relative;
height: 200px;
cursor: pointer;
background-size: auto 110%;
background-position: center center;
-moz-transition: all 3s ease;
-webkit-transition: all 3s ease;
transition: all 3s ease;
}
.highlight:hover {
transform: scale(1.3);
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: all 3s ease;
-webkit-transition: all 3s ease;
transition: all 3s ease;
}
.highlight:before {
content: ' ';
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
.highlight:hover:before {
background-color: rgba(0, 0, 0, 0.8);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
<div class="container">
<div class="highlight" style="background-image:url(http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg);">
</div>
</div>
这是另一个在 Chrome 和 Firefox 上都能正常工作的解决方案:
HTML代码:
<div class="highlight">
<div class="filter">
</div>
<img src="http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg" alt="image" />
</div>
css:
.highlight {
display: block;
position: relative;
text-align: center;
overflow: hidden;
}
.highlight .filter {
background-color:rgba(0,0,0,0.4);
position: absolute;
width: 100%;
height: 100%;
z-index: 2;
transition: background-color 3s ease;
}
.highlight:hover > img{
transform: scale(1.3);
}
.highlight:hover > .filter {
background-color:rgba(0,0,0,0.8);
}
.highlight img {
height: 280px;
transition: transform 3s ease;
}
这不起作用的原因是 background-size
在使用诸如 cover
、contain
或 auto
.
MDN 进一步说明:
Animatable: yes, as a repeatable list of , a simple list of , a length, percentage or calc(); when both values are lengths, they are interpolated as lengths; when both values are percentages, they are interpolated as percentages; otherwise, both values are converted into a calc() function that is the sum of a length and a percentage (each possibly zero), and these calc() functions have each half interpolated as real numbers. This means keyword values are not animatable.
因此,将 original/final 值调整为实际数字(或长度,因为它们更恰当地引用),您将解决这个问题。
In background-size you have to give width otherwise it wont work.
Check this fiddle i have modified your code :
我正在尝试转换 background-size
和 background-color
。
- Chrome:
background-size
的转换无效 - Firefox:两者都工作正常
我还创建了一个fiddle。
.highlight {
display: block;
position: relative;
/*min-height: 800px;*/
min-height: 200px;
background-position: center center;
background-repeat: no-repeat;
/*padding-top: 200px;*/
padding-top: 80px;
/*background-size: cover;*/
}
.highlight:before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .25);
content: "";
}
.highlight {
position: relative;
height: 200px;
cursor: pointer;
background-size: auto 110%;
background-position: center center;
-moz-transition: background-size 3s ease;
-webkit-transition: background-size 3s ease;
transition: background-size 3s ease;
}
.highlight:hover {
background-size: auto 130%;
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: background-size 3s ease;
-webkit-transition: background-size 3s ease;
transition: background-size 3s ease;
}
.highlight:before {
content: ' ';
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
.highlight:hover:before {
background-color: rgba(0, 0, 0, 0.8);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
<div class="highlight" style="background-image:url(http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg);">
</div>
有人帮我解决这个问题吗?或者可以确定正在打破这些转变?
谢谢
可以使用transform: scale(1.3)
和transition: all
实现背景大小过渡效果。
已更新 fiddle。
注意:我还添加了一个 wrapper 来隐藏溢出。
.container {
overflow: hidden;
}
.highlight {
display: block;
position: relative;
/*min-height: 800px;*/
min-height: 200px;
background-position: center center;
background-repeat: no-repeat;
/*padding-top: 200px;*/
padding-top: 80px;
/*background-size: cover;*/
}
.highlight:before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .25);
content: "";
}
.highlight {
position: relative;
height: 200px;
cursor: pointer;
background-size: auto 110%;
background-position: center center;
-moz-transition: all 3s ease;
-webkit-transition: all 3s ease;
transition: all 3s ease;
}
.highlight:hover {
transform: scale(1.3);
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: all 3s ease;
-webkit-transition: all 3s ease;
transition: all 3s ease;
}
.highlight:before {
content: ' ';
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.4);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
.highlight:hover:before {
background-color: rgba(0, 0, 0, 0.8);
-moz-transition: background-color 3s ease;
-webkit-transition: background-color 3s ease;
transition: background-color 3s ease;
}
<div class="container">
<div class="highlight" style="background-image:url(http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg);">
</div>
</div>
这是另一个在 Chrome 和 Firefox 上都能正常工作的解决方案:
HTML代码:
<div class="highlight">
<div class="filter">
</div>
<img src="http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg" alt="image" />
</div>
css:
.highlight {
display: block;
position: relative;
text-align: center;
overflow: hidden;
}
.highlight .filter {
background-color:rgba(0,0,0,0.4);
position: absolute;
width: 100%;
height: 100%;
z-index: 2;
transition: background-color 3s ease;
}
.highlight:hover > img{
transform: scale(1.3);
}
.highlight:hover > .filter {
background-color:rgba(0,0,0,0.8);
}
.highlight img {
height: 280px;
transition: transform 3s ease;
}
这不起作用的原因是 background-size
在使用诸如 cover
、contain
或 auto
.
MDN 进一步说明:
Animatable: yes, as a repeatable list of , a simple list of , a length, percentage or calc(); when both values are lengths, they are interpolated as lengths; when both values are percentages, they are interpolated as percentages; otherwise, both values are converted into a calc() function that is the sum of a length and a percentage (each possibly zero), and these calc() functions have each half interpolated as real numbers. This means keyword values are not animatable.
因此,将 original/final 值调整为实际数字(或长度,因为它们更恰当地引用),您将解决这个问题。
In background-size you have to give width otherwise it wont work.
Check this fiddle i have modified your code :