CSS Background image size transition(CSS 背景图片大小转换)
问题描述
I am working on a simple markup with resizing a div's background img. See the fiddle:
http://jsfiddle.net/zeYZL/
I need this to be animated with a simple CSS transition. and I tried doing this:
#tile:hover {
background-size:550px 550px;
background-position:-50px -50px;
transition:all 0.5s ease;
-webkit-transition:all 0.5s ease;
-o-transition:all 0.5s ease;
-moz-transition:all 0.5s ease;
}
But then the background image clips upon the hover. (See http://jsfiddle.net/5Hm9u/)
Any tips on how to fix it?
Greetings, Chris
JSFiddle
You need to put background
properties outside the hover
too, so it knows what to return to when not on hover.
e.g.
.tile{
float:left;
margin:1px;
width:450px;
height:450px;
overflow:hidden;
background-size:450px 450px;
background-position:0px 0px;
}
And if you want it to transition on mouseover and when you take the mouse off, you put the transition on .tile
and not .tile:hover
JSFiddle
Side note
You should avoid using inline styles where you can use a stylesheet.
Put your background image in .tile
rather than using the style
attribute.
Full CSS:
.tile{
float:left;
margin:1px;
width:450px;
height:450px;
overflow:hidden;
background-size:450px 450px;
background-image:url(http://www.placehold.it/450x450);
background-position:0px 0px;
transition:all 0.5s ;
-webkit-transition:all 0.5s ;
-o-transition:all 0.5s ;
-moz-transition:all 0.5s ;
}
.tile:hover {
background-size:550px 550px;
background-position:-50px -50px;
}
这篇关于CSS 背景图片大小转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS 背景图片大小转换
基础教程推荐
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06