css3和js网页制作实例:网页头像展示效果# t& L2 h) A9 a- {
今天就为大家分享一个非常有创意的CSS3头像展示效果,交互性强,是一个基于css3和js来实现的,代码量不多,但是最终的效果却很让人赞叹,所以特别推荐给大家。其中关于本示例的一些css样式呈现,个人认为还有很大的发挥空间,例如鼠标经过的时候而不是切换成一个背景色和文字,而是替换成其他的相关图片(例如改为人物的其他表情),这将会是非常的有意思的。& M3 j4 G2 r$ Y+ l
- O9 J8 \, r* I+ {; m/ F) z. y
) ~/ E. g, n- V4 X" r z4 f0 Y" I
好了,接下来我们一起看看其效果的制作方法。
$ g6 ~# i }* E0 F7 yHTML代码结构
: w+ x* b( b% j" W- L' d& J$ w( H4 g- P, n) j% F) z& D
- <ul class="grid"><li><a href="http://www.jiawin.com/creative-css3-avatar-display"><img src="images/1.jpg" alt="" /><div class="info"><p>1</p></div></a></li></ul>
复制代码 看了上面代码是极其简单的,就一个列表元素,你需要放多少张图片就写多少<li>。<li>里面的结构如上。 c. k. {+ c$ R
CSS样式表
5 i5 {4 V# Q$ w" ^+ h0 x% Q+ s7 p0 \( H) J4 C9 |* K) B
接下来就是我们的css样式表了,在这里只是一个示例,大家在这部分可以自由发挥,定义出你们的个性来。5 r# x0 h& z) s8 l: |
- body {font-family: Helvetica, Arial, sans-serif;font-size: 16px;-webkit-font-smoothing: antialiased;-moz-font-smoothing: antialiased;font-smoothing: antialiased;margin:0;padding:0;background-color: #E6E6E6;}.grid {margin: 100px auto 80px auto;padding: 0px;width: 100%;height: auto;overflow: hidden;max-width: 1000px;-webkit-box-shadow:0px 40px 50px rgba(0, 0, 0, 0.3);-moz-box-shadow:0px 40px 50px rgba(0, 0, 0, 0.3);box-shadow:0px 40px 50px rgba(0, 0, 0, 0.3);}.grid li {width: 10%;background: #000000;float: left;position: relative;overflow: hidden;}.grid img {float: left;width: 100%;height: auto;position: relative;-webkit-transform-style: preserve-3d;-webkit-backface-visibility: hidden;-webkit-transform: translate3d(0, 0, 0);-moz-transform-style: preserve-3d;-moz-backface-visibility: hidden;-moz-transform: translate3d(0, 0, 0);transform-style: preserve-3d;backface-visibility: hidden;transform: translate3d(0, 0, 0);}.grid .info {position: absolute;width: 100%;height: 100%;padding: 15px;background: #DC584C;display: none;z-index: 2;-webkit-transform-style: preserve-3d;-webkit-backface-visibility: hidden;-webkit-transform: translate3d(0, 0, 0);-moz-transform-style: preserve-3d;-moz-backface-visibility: hidden;-moz-transform: translate3d(0, 0, 0);transform-style: preserve-3d;backface-visibility: hidden;transform: translate3d(0, 0, 0);}.grid p {font-size: 22px;font-weight: bolder;color: #FFF;}
复制代码 JavaScript代码
W+ E" E( _" A- E/ {' v3 f$ L
0 y5 D! j; h. @$ N% f最后,我们需要借助JavaScript来完成一些特殊的互动(动画效果)。
# k1 e8 @: E% R2 O$ J# {(function() { h& V* z: @/ {$ V" N4 @; _
$(function() {& S3 _/ E n2 a9 d+ U: r. _( a
var columns, current, easing, grid, hideItem, showItem, time,
4 Q; U9 i1 L2 z3 M_this = this;) J% c7 O" p" y7 y# N4 w
grid = $(".grid");# ^" v0 y4 B! m: B1 n! M- o
current = {
& u+ U1 z4 F' q) O0 Pindex: -1,
0 B Z' j; T- ?. ?" O3 A/ [column: 0,
3 A; S% l% w7 `+ Bline: 0
" e/ P% W' r9 r' a' H' Z};. G! m, d9 y, ]8 G
columns = 10;: k+ Y$ j: k& o) V4 M
easing = "cubic-bezier(0.165, 0.840, 0.440, 1.000)";) j& S& N- K1 x2 b4 E B$ F" t
time = 400;. [$ q) P6 `: [6 g# Y/ H
grid.on("mouseenter", "a", function(e) {* T: G" j& t& J" [7 a2 K
var column, el, image, index, info, item, line;& A( C" L* D8 C% I- ^
el = $(e.currentTarget);
9 b; w. P+ M" Dinfo = el.find(".info");& \ b# a! F. F4 D3 A# {
image = el.find("img");: y% _, c% s6 `% R* V* a1 m
index = el.parent().index();- p- l9 a" J6 B( U7 s0 `
column = index % columns;; p. E) x1 f7 G. ?6 @' Y9 ?0 x# \$ x
line = Math.floor(index / columns);
2 B# }6 _2 Y2 x# v# V6 ~' w$ [) O. uconsole.log(index, column, line);
* ?1 i |9 O4 V6 G2 v/ ^# Litem = {' h2 _6 k3 L; d& j* ?- a
el: el,- V/ T$ p# P$ h; b
index: index,
9 l4 p! G: f, x9 n1 ]9 pcolumn: column,* W% w: D! }8 h) ^
line: line,
8 }. `& o9 R3 v8 B0 |$ minfo: info,2 a# a, o5 ~: s, H- F0 M
image: image
9 }* `" y' v! h( l# d; f5 n};& W* G) r* \6 q2 I0 F a
if (current.el && current.index === index) return;: O, ~( R; V ^. O9 a
if (line === current.line && column > current.column) {$ T' a5 j9 ?3 Y% S* ?6 m K
showItem(item, "-100%", 0, "25%", 0);8 V( t' H# c. i& H6 i
hideItem(current, "100%", 0, "-25%", 0);7 O: n L n+ n3 R3 W7 H/ o7 O
} else if (line === current.line && column < current.column) { showItem(item, "100%", 0, "-25%", 0); hideItem(current, "-100%", 0, "25%", 0); } else if (line > current.line && column === current.column) {$ g. f" a6 \) _2 A
showItem(item, 0, "-100%", 0, "25%");
$ Q+ Q ~4 F5 h* N- ] ^$ w8 chideItem(current, 0, "100%", 0, "-25%");
- R Y* b/ N" d Q. v( A} else {
$ Q* X$ n; \9 O% q g/ ishowItem(item, 0, "100%", 0, "-25%");
) J6 A9 R5 G4 J. QhideItem(current, 0, "-100%", 0, "25%");
4 D. f) X% n3 L. m+ Z" T7 O/ v}: c4 v& b4 k ~9 C
return current = item;
5 z, D* ?% S- s6 F) z});5 o* d1 @+ {6 X6 i
showItem = function(item, infoX, infoY, imageX, imageY) {. v4 I- E5 _, N2 d9 Y
item.info.stop().css({
2 `6 N. n3 J/ I" X8 B, ?x: infoX,& Z& ~3 ~+ G* f
y: infoY,
/ v4 P5 ^, P) ]display: "block", \, e, V9 f, D' Y
}).transition({
, u' a- h- K8 _9 ^4 `x: 0,) ?$ s: W. w! t2 Y1 T
y: 0,0 f( a: z6 F1 q* q# _
duration: time,
1 U( ]5 o. |3 X$ d8 Beasing: easing
. K u0 r3 ]/ Q0 G3 c+ s});
! t& J* j5 q$ ~* areturn item.image.stop().transition({
! N; x: }9 E4 _4 O. ?x: imageX,: |* k- f) y& }+ ^8 b
y: imageY,# i& n1 E" D, v( O* ^: e6 D# C
opacity: .5,2 H; F) X& C8 w; o3 a
duration: time,3 {8 _. _. F: x, o6 a
easing: easing
9 `6 A% p6 B* P }});7 H! j( y& r* @3 z- h
};
z9 Y5 ]! W8 i1 Treturn hideItem = function(item, infoX, infoY, imageX, imageY) {: U( y& i# \, U4 o2 u$ B; a
if (!item.el) return;
+ m0 b3 Y! m" m/ f- Bitem.info.stop().transition({
, K$ \7 c* H( ? K5 Mx: infoX,
5 x0 I: K0 n# i! V# x" L) n; dy: infoY,; ~$ q/ \ [1 U& e. K) e
duration: time,* G R, ?, B7 ^* @5 K9 w+ r# p
easing: easing5 x- ^3 g, k; n& h: F
});
, D* R7 ]* t7 w8 f: Creturn item.image.stop().css({) L7 w9 H+ _7 U. T3 m C: \
x: imageX,
3 d/ s: V2 P2 N! z) Jy: imageY,
. X% _2 ]$ Z: R. Q0 }, ]opacity: .5
; ]3 a: a! n% L$ ^}).transition({# Z" a5 @) U9 J/ B2 @
x: 0,9 Y! n& c* e/ u d3 v) J4 G
y: 0,* }6 r. \" d+ o1 B/ t: k* u
opacity: 1,: s6 V* H# k7 A/ H$ T, e
duration: time, M3 Q+ q% G6 ?& D1 C* G
easing: easing
& x& `/ @( _6 i! Y- h2 y});, n" p8 L) l- y9 U4 e' q
};) V4 Q+ M# c( {! F ~
});
5 P4 O- ^; Z p7 V}).call(this);
! n4 c# l8 u7 w; n- 到这里,我们的示例基本上就完成了,但是按照上面几步来,最终是没效果的,那是因为我们还没引入jQuery库和一个jQuery插件(jQuery Transit)。
复制代码- <script src="http://code.jquery.com/jquery-latest.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.transit/0.9.9/jquery.transit.min.js"></script>
复制代码 jQuery Transit是一个利用了CSS3的过渡和转换特性来实现动画特效的 jQuery插件,与jQuery的animate方法有着同样的语法。此插件支持这个特性所提供的大部分方法,同时增加了 jQuery 独有的技术:回调(callbacks)、自动增加浏览器CSS前缀、链盒(chaining)等。由于此插件使用了真正的CSS3规则,所以它不能兼容旧版本的浏览器。$ L0 ^) ]9 D+ b
! j0 q$ ~& ~$ R* ]4 t# y) W i5 i下载附件:http://www.webjx.com/files/soft/1_130711103319.rar* R- V, Y) l O4 M6 H4 b. C7 w
今天的示例就到这里了,希望大家喜欢,如果有问题可以在下方留言,一起探讨研究。 |
|