一、中规中矩的效果
所谓“中规中矩的效果”就是加个投影,贴个胶带什么的。效果如下:
CSS3的box-shadow投影效果,相关代码如下:
代码如下:
-moz-box-shadow: 0 2px 10px 1px rgba(0, 0, 0, 0.2)-webkit-box-shadow: 0 2px 10px 1px rgba(0, 0, 0, 0.2)box-shadow: 0 2px 10px 1px rgba(0, 0, 0, 0.2)
反而是上面的胶带纸效果有点说头,这些微微倾斜的胶带是CSS写出来的,大部分效果源自CSS3,主要有RGBA显示半透明背景色,box-shadow显示淡淡的投影,transform做旋转效果;元素使用after伪类生成,完整代码如下:
代码如下:
.page:after { width: 180pxheight: 30pxcontent: " "margin-left: -90pxborder: 1px solid rgba(200, 200, 200, .8)background: rgba(254, 254, 254, .6)-moz-box-shadow: 0px 0 3px rgba(0, 0, 0, 0.1)-webkit-box-shadow: 0px 0 3px rgba(0, 0, 0, 0.1)box-shadow: 0px 0 3px rgba(0, 0, 0, 0.1)-moz-transform: rotate(-5deg)-webkit-transform: rotate(-5deg)-o-transform: rotate(-5deg)transform: rotate(-5deg)position: absoluteleft: 50%top: -15px}
老外似乎很喜欢使用before和after伪类,国外最近的些教程,技术点等经常见到此玩意。我个人感觉有跟风之嫌,就像是狂热的经济泡沫,不需 要太久,大家会冷静下来重新审视这些曾经上手简单,自我感觉不错的方法。由于目前IE6/7不支持before/after类,所以,某种意义上来说,伪 类的使用少了些兼顾IE下显示的烦恼。
二、外翻卷角纸张效果
纸张一般都是有卷角的,所以,我们可以更近一步,模拟卷角效果来使得纸张的感觉更逼真,这里就要借助于投影了,且是曲线投影。
我们还可以给纸张增加渐变(gradient)效果,以模拟纸张的曲度。
同时,可以给文字增加投影,可以让文字有书写的凹陷感,可以进一步让纸张效果更逼真。
于是,在“中规中矩”纸张基础上,我们做点小手术,结果得到下面的效果:
您可以狠狠地点击这里:渐变卷边纸张效果demo
目前貌似opera浏览器还不支持CSS3渐变,同时webkit核心下浏览器下的gradient渐变的写法已经开始向FireFox浏览器靠拢了。确实,都是CSS3,有必要搞得五花八门吗?
相关渐变CSS代码如下:
代码如下:
background:-moz-linear-gradient(top, #f4f39e, #f5da41 60%, #fe6)background:-webkit-gradient(linear, 0% 0%, 0% 100%, from(#f4f39e), to(#fe6), color-stop(.6, #f5da41))
在60%的位置有个颜色拐点。
关于弧形的投影,也是使用:before/:after伪类实现的,相关代码如下:
代码如下:
.page:before { -webkit-transform: skew(-15deg) rotate(-5deg)-moz-transform: skew(-15deg) rotate(-5deg)-o-transform: skew(-15deg) rotate(-5deg)transform: skew(-15deg) rotate(-5deg)left: 15px} .page:after { -webkit-transform: skew(15deg) rotate(5deg)-moz-transform: skew(15deg) rotate(5deg)-o-transform: skew(15deg) rotate(5deg)transform: skew(15deg) rotate(5deg)right: 15px} .page:before, .page:after { width: 90%height: 20%content: ' '-webkit-box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3)-moz-box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3)box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3)position: absolutebottom: 22pxz-index: -1}
要想查看效果的完整代码,demo页面查看源文件即可,脱光光,一览无遗。
这里的曲线投影卷角效果支持Opera浏览器,如下截图:
三、带分隔线的卷纸纸张效果
上面的纸张卷角效果是通过曲线投影效果来模拟的,而这里,纸张的卷边效果是纸张真的“曲线化”了,而且,还是带隔线的纸张效果哦,您可以参见下面的效果截图(Chrome 9):
这里,Firefox浏览器下的纸张隔线使用的是重复渐变(Repeating Gradients)实现的,对于webkit浏览器,使用的是background-size实现的。实际上,Firefox3.6+已经支持background-size属性了(-moz-background-size), 所以,对于Firefox浏览器,这里使用重复渐变与渐变+background-size是等效的。在FireFox4以及IE9浏览器中 background-size属性已经没有私有属性了,Opera浏览器在9.5版本时候就已经支持background-size属性了,但是存在一 些bug。
这里分隔线实现的相关CSS如下:
代码如下:
background: -moz-repeating-linear-gradient( top, #fcf59b, #fcf59b 29px, #81cbbc 30px )background: -webkit-gradient( linear, left top, left bottom, from(#81cbbc), color-stop(2%, #fcf59b) )background: repeating-linear-gradient( top, #fcf59b, #fcf59b 29px, #81cbbc 30px )-webkit-background-size: 100% 30px
而对于曲边的实现,使用的是border-radius圆角,这里展示下示意的代码:
代码如下:
border-bottom-left-radius: 20px 500pxborder-bottom-right-radius: 500px 30pxborder-top-right-radius: 5px 100px
当然,FireFox浏览器下的写法不是如此,其bottom, left是连起来的,而不是”-”分隔,这里仅仅是示意,这里的微曲线是如何实现的。相信看到上面的代码就知道意思了,这里就不啰嗦了。
还有其他些细节,如如何控制行高让文字与隔线一一对应,这些您要是有兴趣可以去demo页面,通过查看元素等方法一看究竟。
四、最后点老生常谈
难免的,本文的重点在于CSS3,所以就本文而言,IE6/IE7/IE8浏览器就是过来打酱油的,且三个纸张效果从一而终,都是下面这副模样:
虽然没有透明胶效果,没有投影,没有卷边,更没有分隔线,但是,就功能上来讲,一点都不影响使用。所以,无论你是渐进增强还是退求其次,纸张投影效果在实际项目中的应该也不是不可以的,这就要看个人和团队的抉择了
HTML代码
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>用CSS3动画给一个小球创建跳跃的动画效果</title>
<link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
<section class="main">
<div id="ballWrapper">
<div id="ball"></div>
<div id="ballShadow"></div>
</div>
</section>
</body>
</html>
CSS代码(创建一个与html同目录的index.css文件)
#ballWrapper{position:fixedtop:35%left:50%z-index:100margin-left:-70pxwidth:140pxheight:300pxcursor:pointer-webkit-transition:all 5s linear 0s-moz-transition:all 5s linear 0stransition:all 5s linear 0s-webkit-transform:scale(1)-moz-transform:scale(1)-o-transform:scale(1)transform:scale(1)-ms-transform:scale(1)}#ballWrapper:active{cursor:pointer-webkit-transform:scale(0)-moz-transform:scale(0)-o-transform:scale(0)transform:scale(0)-ms-transform:scale(0)}
#ball{position:absolutetop:0z-index:11width:140pxheight:140pxborder-radius:70pxbackground:#bbbbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(187,187,187,1)), color-stop(99%,rgba(119,119,119,1))) background: -webkit-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -o-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -ms-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) box-shadow: inset 0 -5px 15px rgba(255,255,255,0.4), inset -2px -1px 40px rgba(0,0,0,0.4), 0 0 1px #000 cursor: pointer base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2JiYmJiYiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzc3Nzc3NyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(187,187,187,1) 0,rgba(119,119,119,1) 99%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#bbbbbb', endColorstr='#777777', GradientType=0 )-webkit-animation:jump 1s infinite-moz-animation:jump 1s infinite-o-animation:jump 1s infinite-ms-animation:jump 1s infiniteanimation:jump 1s infinite}
#ball::after{position:absolutetop:10pxleft:30pxz-index:10width:80pxheight:40pxborder-radius:40px/20pxbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(232,232,232,1)), color-stop(1%,rgba(232,232,232,1)), color-stop(100%,rgba(255,255,255,0))) background: -webkit-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -o-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -ms-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) content: "" base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2U4ZThlOCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjElIiBzdG9wLWNvbG9yPSIjZThlOGU4IiBzdG9wLW9wYWNpdHk9IjEiLz4KICAgIDxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(232,232,232,1) 0,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#e8e8e8', endColorstr='#00ffffff', GradientType=0 )}
#ballShadow{position:absolutebottom:0left:50%z-index:10margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-transform:scaleY(.3)-moz-transform:scaleY(.3)-o-transform:scaleY(.3)transform:scaleY(.3)-ms-transform:scaleY(.3)-webkit-animation:shrink 1s infinite-moz-animation:shrink 1s infinite-o-animation:shrink 1s infinite-ms-animation:shrink 1s infiniteanimation:shrink 1s infinite}
@-webkit-keyframes jump{0%{top:0-webkit-animation-timing-function:ease-in}
50%{top:140pxheight:140px-webkit-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-webkit-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-webkit-animation-timing-function:ease-out}
95%{top:0-webkit-animation-timing-function:ease-in}
100%{top:0-webkit-animation-timing-function:ease-in}
}
@-moz-keyframes jump{0%{top:0-moz-animation-timing-function:ease-in}
50%{top:140pxheight:140px-moz-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-moz-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-moz-animation-timing-function:ease-out}
95%{top:0-moz-animation-timing-function:ease-in}
100%{top:0-moz-animation-timing-function:ease-in}
}
@-o-keyframes jump{0%{top:0-o-animation-timing-function:ease-in}
50%{top:140pxheight:140px-o-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-o-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-o-animation-timing-function:ease-out}
95%{top:0-o-animation-timing-function:ease-in}
100%{top:0-o-animation-timing-function:ease-in}
}
@-ms-keyframes jump{0%{top:0-ms-animation-timing-function:ease-in}
50%{top:140pxheight:140px-ms-animation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60px-ms-animation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70px-ms-animation-timing-function:ease-out}
95%{top:0-ms-animation-timing-function:ease-in}
100%{top:0-ms-animation-timing-function:ease-in}
}
@keyframes jump{0%{top:0animation-timing-function:ease-in}
50%{top:140pxheight:140pxanimation-timing-function:ease-out}
55%{top:160pxheight:120pxborder-radius:70px/60pxanimation-timing-function:ease-in}
65%{top:120pxheight:140pxborder-radius:70pxanimation-timing-function:ease-out}
95%{top:0animation-timing-function:ease-in}
100%{top:0animation-timing-function:ease-in}
}
@-webkit-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-webkit-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}
}
@-moz-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-moz-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}
}
@-o-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-o-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}
}
@-ms-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-ms-animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}
}
@keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}
50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)animation-timing-function:ease-out}
100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}
}
这是两个按钮或者div,左边的设置左边框圆角,右边的那个设置右边框圆角。具体的参数,你可以参考一下这个教程:border-radius:2em
等价于:
border-top-left-radius:2em
border-top-right-radius:2em
border-bottom-right-radius:2em
border-bottom-left-radius:2em
你也可以根据你的需要设置自己需要的边框。比如只设置左上角圆角。自己尝试一下吧。
http://www.w3school.com.cn/cssref/pr_border-radius.asp