今天在工做中遇到需求,须要用CSS3写一个卡片翻转效果,这个效果看起来简单,可是仍是涉及到一些不经常使用的CSS3中的3D转换的属性以及实现该效果的思路,因此这里总结一下。css
原文连接:http://mrzhang123.github.io/2016/08/17/FlipCards/html
项目地址:https://github.com/MrZhang123/Practice/tree/master/FlipCardsgit
目前浏览器都不支持perspective
属性github
在chrome和Safari中须要使用-webkit-perspective
,而在Firefox中须要使用-moz-perspective
来定义。web
perspective
属性定义3D元素距视图的距离,以像素计。该属性容许改变查看3D元素的视图。当为元素定义chrome
perspective
属时,其子元素会得到透视效果,而不是元素自己。换句话说,设置这个元素是为了给该元素的子元素用。浏览器
number:元素距离视图的距离,以像素计测试
none:默认值,与0相同。不设置透视动画
Firefox支持transfrom-style
属性。3d
Chrome、Safari和Opera支持代替的-webkit-transform-style
属性。
transform-style
属性固定如何在3D空间中呈现被嵌套的元素。通常给父元素设置让其全部子元素跟着父元素一块儿位置移动,通常会设置。
flat:子元素将不保留其3D位置(默认值)
perserve-3d:子元素将保留其3D位置
IE10+、Firefox、Opera、Chrome均支持transition
属性。Safari支持替代的-webkit-transition
属性。可是IE9-不支持该属性。
transition
属性是一个简写属性,用于设置四个过渡属性:
transiton-property:规定设置过渡效果的CSS属性的名称
transiton-duration:规定完成过渡效果须要多少秒或毫秒
transiton-timing-funciton:规定速度效果的速度曲线
transition-delay:规定过渡效果什么时候开始
transiton-duration
必须设置,不然时长为0 ,不会有过渡效果
只有IE10+和Firefox支持backface-visibility
,Opera15+、Safari和Chrome支持替代的-webkit-backface-visibility
backface-visibility
属性定义当前元素不面向屏幕时是否可见,若是元素在旋转后不但愿看到背面,则能够使用。
visible:背面是可见的(默认值)
hidden:背面是不可见的
要实现相似的翻牌效果,首先咱们须要有一张能够翻的牌,这张牌由两个元素重叠组成,位于上层正面咱们看到,而位于下层的背面咱们看不到而且自己是绕Y轴旋转过的,这样,当鼠标移动上去后,同时让正面和背面执行旋转就能够实现翻牌效果。
基本结构代码以下:
<div id="content"> <ul> <li> <a href="#" > <div> <h3>测试正面1</h3> <p>文字文字文字文字文字文字文字文字文字文字文字</p> </div> <div> <h3>测试背面1</h3> <p>文字文字文字文字文字文字文字文字文字文字文字</p> </div> </a> </li> </ul> </div>
ul,li { margin:0; padding:0; list-style:none; } #content ul li{ width: 225px; height: 180px; } #content ul li a{ position: relative; display: block; width: 100%; height: 100%; } #content ul li a > div{ position: absolute; left: 0; height: 0; width: 100%; height: 100%; color: #fff; } #content ul li a div:first-child{ background-color: rgb(255, 64, 129); z-index:2; } #content ul li a div:last-child{ background:rgb(0, 188, 212); z-index:1; } #content ul li a div h3{ margin: 0 auto 15px; padding: 15px 0; width: 200px; height: 16px; line-height: 16px; font-size: 14px; text-align: center; border-bottom: 1px #fff dashed; } #content ul li a div p{ padding: 0 10px; font-size: 12px; text-indent: 2em; line-height: 18px; }
这样就让两个div叠在一块儿了,可是若是要进行翻转的话,首先是须要将背面自己就翻过去,当鼠标放上去以后翻转过来,让咱们看到,因此咱们须要给背面添加翻转180°的属性,鼠标放上去以后让它翻转到0°,同时为保证每一个浏览器渲染统一,给正面加一个翻转0°,鼠标移动上去以后翻转-180°,而且是一个动画,因此要添加过渡。因此给正面背面添加CSS以下:
#content ul li a > div{ -webkit-transition:.8s ease-in-out; -moz-transition:.8s ease-in-out; } #content ul li a div:first-child{ -webkit-transform:rotateY(0); -moz-transform:rotateY(0); } #content ul li a div:last-child{ -webkit-transform:rotateY(180deg); -moz-transform:rotateY(180deg); } #content ul li a:hover div:first-child{ -webkit-transform:rotateY(-180deg); -moz-transform:rotateY(-180deg); } #content ul li a:hover div:last-child{ -webkit-transform:rotateY(0); -moz-transform:rotateY(0); }
在添加这些CSS3属性后,能够实现翻转,可是发现只看到正面,没有看到背面,这又是为何呢,前面提到有一个属性backface-visibility
,这个属性用于控制在翻转后,元素的背面是否可见,默认是可见的,因此就会挡着背面那个元素,咱们须要手动设置元素翻转后背面不可见,因此须要设置:
#content ul li a > div{ -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; }
这样设置以后,因为正面的元素在翻转后背面不可见,咱们就能够看到背面的元素了。
可是,仔细观察会发现这个翻转彷佛并非那么立体,彷佛在两条平行线之间实现了翻转,因此咱们须要设置一个观察点距离视图的距离,这时候就须要给父元素添加perspective
属性,这个属性的值通常为800px ~ 1000px,这个范围内的值会看上去合理。因此给父元素添加:
#content ul li a{ -webkit-perspective: 800px; -moz-perspective: 800px; }
至此,就实现了一个翻转卡牌的效果,可是这里须要解决一个问题,由于perspective
属性不被IE支持(Microsoft Edge支持),因此须要进行降级,个人作法是直接显示隐藏。那么如何识别IE9+浏览器呢?在stackoverflow中我找到了答案:
* html .ie6{ property:value; }
or
html .ie6{ _property:value; }
*+html .ie7{ property:value; }
or
*:first-child+html ie7{ property:value; }
@media screen\9{ ie67{property:value;} }
or
.ie67{ *property:value;}
or
.ie67{ #property:value;}
@media \0screen\,screen\9{ ie678{property:value;} }
html>/**/body .ie8{property:value;}
or
@media \0screen{ ie8{property:value;} }
.ie8{property/*\**/:value\9;}
@media screen\0{ ie8910{property:value;} }
@media screen and (min-width:0\0) and (min-resolution: .001dpcm){ /*IE9 CSS*/ .ie9{property:value;} }
@media screen and (min-width:0\0) and (min-resolution: +72dpi){ /*IE9+ CSS*/ .ie9up{property:value;} }
@media screen and (min-width:0){ .ie910{property:value;} }
_:-ms-lang(x), ie10 {property:value;}
_:-ms-lang(x), ie10up{property:value;}
or
@media all and (-ms-high-contrast:none),(-ms-high-contrast:active){ .ie10up{property:value;} }
_:-ms-fullscreen, :root .ie11up{property:value;}