css3和js网页制作实例:网页头像展示效果
; y+ x9 { C! q2 E今天就为大家分享一个非常有创意的CSS3头像展示效果,交互性强,是一个基于css3和js来实现的,代码量不多,但是最终的效果却很让人赞叹,所以特别推荐给大家。其中关于本示例的一些css样式呈现,个人认为还有很大的发挥空间,例如鼠标经过的时候而不是切换成一个背景色和文字,而是替换成其他的相关图片(例如改为人物的其他表情),这将会是非常的有意思的。, f# W x/ ?: a: V5 N8 b
! F+ L. V$ z1 p& v+ {* m2 H4 X2 b( f
$ {1 H7 w% b1 T, H+ A! S7 H1 V好了,接下来我们一起看看其效果的制作方法。
, Y/ D5 q/ H' K" j# ?' [6 N3 IHTML代码结构3 t H/ f |9 T& ]# v1 p
- G( Q- W( E6 d/ u. S- i" S; f {
- <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>里面的结构如上。0 `' e' _; x9 ~; {3 O/ H; C
CSS样式表0 ~( j! a5 E' \
& z4 m6 e% g0 g; s$ e接下来就是我们的css样式表了,在这里只是一个示例,大家在这部分可以自由发挥,定义出你们的个性来。& O7 {3 f& \4 r- q
- 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代码7 l" b9 |- o6 V: n
. I: R r, l5 _" ?$ M1 b! _
最后,我们需要借助JavaScript来完成一些特殊的互动(动画效果)。
1 Q, S1 C" r6 W6 W2 Q(function() {
- g$ O8 _/ ~7 }% Q* j4 f. P4 j2 N$(function() {
- w$ P. c3 Q; i7 [! p' \+ Mvar columns, current, easing, grid, hideItem, showItem, time,
' N$ G5 p: G4 D) o4 Q. W_this = this;
9 F8 }; g+ l$ ^7 @/ l" pgrid = $(".grid");
( l( N' A7 I3 P7 i+ I% scurrent = {- Y6 d( w. A/ v6 ?0 L: Q
index: -1,( f5 f W3 a) f7 ^+ L" j% {: [8 R" U
column: 0,
8 R* s7 t0 i2 nline: 0
1 W3 }$ Y3 r! w" g5 W0 L};
/ m) b n Y) R* S" [* Z7 I+ icolumns = 10;1 R! @9 x X8 |( S9 X, m. x R2 S
easing = "cubic-bezier(0.165, 0.840, 0.440, 1.000)";
- E; g1 g" \) E1 l; Utime = 400;
' i) X0 s5 g. q4 W( Egrid.on("mouseenter", "a", function(e) {
5 m- _# d* A9 l9 [, a, mvar column, el, image, index, info, item, line;9 z( K) j1 c6 m. ~& m2 M
el = $(e.currentTarget);
) ]- |3 H4 U% h, h9 t& ?; ~info = el.find(".info");
" S% n" z! c( X) d0 M# fimage = el.find("img");
* K' ]) k# p9 X/ {3 ^* G0 o, U8 C4 Z1 bindex = el.parent().index();# m G$ \' m& n O: f3 f
column = index % columns;7 B& ~- F. ]/ H/ `) }) |( H/ O
line = Math.floor(index / columns);
# {3 B) `- ~' o' H$ kconsole.log(index, column, line);
% N% e% G5 y6 Sitem = {
- @/ a; \' K$ V: e$ w% F: Hel: el,' N" F" ]1 a0 K- U2 ?& U
index: index, z& K* {6 c- i% t) L
column: column,
5 V4 a% _) G- w) ~& }9 Xline: line,
- \7 e6 e A4 [info: info,
3 ^* ~$ K5 c. |( dimage: image) P. B* h; S) P
};
& h1 @& C, p/ cif (current.el && current.index === index) return;% C) H X: W" M6 U- _. R+ X
if (line === current.line && column > current.column) {* ~- o& B) R" m+ R) d7 h
showItem(item, "-100%", 0, "25%", 0);0 c5 F+ N9 O7 i0 e0 w. w( w
hideItem(current, "100%", 0, "-25%", 0);
6 g1 W" e; E; _0 F9 ^} 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) {
% t h. M, d! N! m4 xshowItem(item, 0, "-100%", 0, "25%");7 b4 |8 j. \, ~) w' q% d5 ^) C
hideItem(current, 0, "100%", 0, "-25%");, }- W: l1 a! j% x
} else {
6 L& b" i( w8 {showItem(item, 0, "100%", 0, "-25%");: [8 Z8 K: O6 J' z( r0 L6 E
hideItem(current, 0, "-100%", 0, "25%");
! f: @! v" `, {& E% ?}! M; G% u7 f& _& ]: Y( @
return current = item;( c$ M, E$ y p$ ?$ `/ w
});5 X" l: Q, Z) h6 a
showItem = function(item, infoX, infoY, imageX, imageY) {
: b9 ^0 _( |! C$ `5 F( l! R# titem.info.stop().css({
& I# X1 s- }4 G2 ]+ S0 ax: infoX,
* j7 z, w2 Y+ b8 ly: infoY,
: C7 C. ^. Z6 U) ~9 xdisplay: "block"' g" A @6 x. H7 N# y
}).transition({) v0 A H7 h+ O* K( U: c! j0 D; ~
x: 0,- L" T1 y( H8 \" ^
y: 0,
" I: X) d$ N' tduration: time,
3 H# F W: W& h5 v4 F* N! u3 ieasing: easing
+ \, m8 C4 r3 X) I* Y5 M j; q7 T});, _' _9 t5 ~& G4 c: K
return item.image.stop().transition({
1 e, @* e. j; e% q% `x: imageX,0 N& J- i- d# p M, T! B$ g* S9 j
y: imageY,. Y5 n* ^" c" V: K8 B" ]
opacity: .5,& x. A) D5 s i ]& d6 E3 b- J- p
duration: time,0 g$ u. V8 [* ?: Q
easing: easing
8 n" r8 B) Y3 A- r8 `});( e. J+ Q: v8 f9 _) m
};. I& E( {# y5 l
return hideItem = function(item, infoX, infoY, imageX, imageY) {! e5 r; S. q8 v- g) q" ?
if (!item.el) return;$ o" p6 f" I7 }4 g8 `" g6 R$ _
item.info.stop().transition({
6 e, v' y$ j2 Yx: infoX,) R% P( K2 `# I* \; }: J+ K% Y/ Q! s
y: infoY,
6 `0 q9 _# Q+ M( Q. H+ ? p- cduration: time,1 s* C" ~3 |- P2 A
easing: easing
7 o" e8 O J$ _) ~! _});
7 ]* [+ v3 L+ G! t2 q2 O1 Sreturn item.image.stop().css({# B: L) m; k0 g) L p" L
x: imageX,
! ]1 F# X. A% u/ i8 Ey: imageY,* n( q- \0 x% Q$ [( [ F! l n
opacity: .5
& @) Q& X/ Y% g' F \# r}).transition({9 F# `0 U6 D1 s, @/ a* L. q
x: 0,: T2 ~- g8 q5 `/ [( J( @' C* i5 q
y: 0,
+ C9 j8 @1 E' G9 y4 |) B1 U& c! vopacity: 1,
8 C6 F% v+ j+ {duration: time,
, S+ W: p4 F7 ^. o2 n: h+ i5 aeasing: easing- L9 J8 w& X" {8 l
});. W* ]- w1 d& y' n, W, N( O9 d, s; i
};
4 Z. M; J0 \2 ]' c+ n});8 T/ N: z% g) h' H V
}).call(this);
( }( c8 v4 [7 G" h, `& L$ ]- 到这里,我们的示例基本上就完成了,但是按照上面几步来,最终是没效果的,那是因为我们还没引入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规则,所以它不能兼容旧版本的浏览器。* w% D5 m9 u; Y8 E( {8 M0 B
1 C' k O: B4 y/ h! L
下载附件:http://www.webjx.com/files/soft/1_130711103319.rar
$ L P5 g4 ~) D( Y今天的示例就到这里了,希望大家喜欢,如果有问题可以在下方留言,一起探讨研究。 |
|