对象位置的关键帧动画在 Safari 中不起作用
Keyframe animation for object-position not working in safari
我有一个盒子和盒子里的一张图片。我正在使用 CSS3 object-fit 属性 用图像填充框。我也需要一个移动的动画。我正在使用关键帧动画来移动图像。
这是我的HTML
<div class="item">
<img src="https://placeimg.com/640/480/any">
</div>
<br>
<button>
animation
</button>
我的 CSS 在这里,
.item {
width: 200px;
height: 200px;
border: 1px solid #000;
}
.item img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 0px 0;
}
.item.active img {
-webkit-animation: objectMove 2s;
animation: objectMove 2s;
/* object-position: 100px 0; */
}
@-webkit-keyframes objectMove {
from {
object-position: 0 0;
}
to {
object-position: 100px 0;
}
}
@keyframes objectMove {
from {
object-position: 0 0;
}
to {
object-position: 100px 0;
}
}
这是我的 javascript,
$('button').click(function(){ $('.item').addClass('active');
setTimeout(function(){ $('.item').removeClass('active'); }, 2000); })
It works fine in chrome, mozilla and edge. But not working in safari.
Here is the jsfiddle of the same.
谁能帮我解决一下?
Safari 浏览器不完全支持 object-position
和 keyframes
,因此我使用 transform:translateX;
而不是 object-position
$('button').click(function() {
$('.item').addClass('active');
setTimeout(function() {
$('.item').removeClass('active');
}, 2000);
})
.item {
width: 200px;
height: 200px;
border: 1px solid #000;
overflow: hidden;
}
.item img {
width: 100%;
height: 100%;
object-fit: cover;
transform:translateX(0);
}
.item.active img {
-webkit-animation: objectMove 2s;
animation: objectMove 2s;
}
@-webkit-keyframes objectMove {
from {
-webkit-transform:translateX(0);
}
to {
-webkit-transform:translateX(100px);
}
}
@keyframes objectMove {
from {
transform:translateX(0);
}
to {
transform:translateX(100px);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
<img src="https://placeimg.com/640/480/any">
</div>
<br>
<button>
animation
</button>
我有一个盒子和盒子里的一张图片。我正在使用 CSS3 object-fit 属性 用图像填充框。我也需要一个移动的动画。我正在使用关键帧动画来移动图像。
这是我的HTML
<div class="item">
<img src="https://placeimg.com/640/480/any">
</div>
<br>
<button>
animation
</button>
我的 CSS 在这里,
.item {
width: 200px;
height: 200px;
border: 1px solid #000;
}
.item img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 0px 0;
}
.item.active img {
-webkit-animation: objectMove 2s;
animation: objectMove 2s;
/* object-position: 100px 0; */
}
@-webkit-keyframes objectMove {
from {
object-position: 0 0;
}
to {
object-position: 100px 0;
}
}
@keyframes objectMove {
from {
object-position: 0 0;
}
to {
object-position: 100px 0;
}
}
这是我的 javascript,
$('button').click(function(){ $('.item').addClass('active');
setTimeout(function(){ $('.item').removeClass('active'); }, 2000); })
It works fine in chrome, mozilla and edge. But not working in safari.
Here is the jsfiddle of the same.
谁能帮我解决一下?
object-position
和 keyframes
,因此我使用 transform:translateX;
object-position
$('button').click(function() {
$('.item').addClass('active');
setTimeout(function() {
$('.item').removeClass('active');
}, 2000);
})
.item {
width: 200px;
height: 200px;
border: 1px solid #000;
overflow: hidden;
}
.item img {
width: 100%;
height: 100%;
object-fit: cover;
transform:translateX(0);
}
.item.active img {
-webkit-animation: objectMove 2s;
animation: objectMove 2s;
}
@-webkit-keyframes objectMove {
from {
-webkit-transform:translateX(0);
}
to {
-webkit-transform:translateX(100px);
}
}
@keyframes objectMove {
from {
transform:translateX(0);
}
to {
transform:translateX(100px);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
<img src="https://placeimg.com/640/480/any">
</div>
<br>
<button>
animation
</button>