css3和js网页制作实例:网页头像展示效果$ e/ f$ d: E$ k; t' i, M
今天就为大家分享一个非常有创意的CSS3头像展示效果,交互性强,是一个基于css3和js来实现的,代码量不多,但是最终的效果却很让人赞叹,所以特别推荐给大家。其中关于本示例的一些css样式呈现,个人认为还有很大的发挥空间,例如鼠标经过的时候而不是切换成一个背景色和文字,而是替换成其他的相关图片(例如改为人物的其他表情),这将会是非常的有意思的。, J% d/ C# E3 ^ c; h& {- ^
- p0 T& ]: c' q0 X" g+ t
6 t$ c5 h x$ X; n( S! I
好了,接下来我们一起看看其效果的制作方法。4 `/ w. k. k- \, A8 x P
HTML代码结构. x; b" M Z/ [% I8 Y7 {
4 B! |4 |5 b, j; 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>里面的结构如上。3 G ]& ~( o3 l1 L! R+ T5 P/ d$ u
CSS样式表* h) i$ j( j# ]0 s8 C
$ q' P9 u" m2 H4 y; a5 l接下来就是我们的css样式表了,在这里只是一个示例,大家在这部分可以自由发挥,定义出你们的个性来。
$ Q; o# e# _- _- 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代码3 O: }/ e3 v: T" A5 ?9 k, I6 T
* e$ s; k8 Z. Q, Z最后,我们需要借助JavaScript来完成一些特殊的互动(动画效果)。, C3 E0 \8 U% t' W5 V8 S/ T$ f
(function() {% w! h/ i( X/ u# ]3 h
$(function() {, h: p8 P4 L' }7 S" x
var columns, current, easing, grid, hideItem, showItem, time,
' g- f7 O( t# C* ~, x! K+ ]- a* a_this = this;
5 F- e* o7 f) g) ]) w/ Zgrid = $(".grid");
# K3 ]' \, v# P3 _: b) T7 H$ tcurrent = {
! f* d9 Z4 K% K9 b6 G2 J9 _9 q: bindex: -1,2 G8 @$ e4 H6 u" a/ v
column: 0,
- t9 c3 u7 f3 a/ i2 I4 yline: 0( f) I/ ^: m/ M
};: I5 k1 m( ?* F- k
columns = 10;6 A' c7 @" C$ ]
easing = "cubic-bezier(0.165, 0.840, 0.440, 1.000)";
4 D0 o+ @0 ^0 K1 `- c, @) l. Z3 q% ktime = 400;
6 v+ e) Z8 ]/ u/ ggrid.on("mouseenter", "a", function(e) {/ {: K' ~4 B4 g+ A% o0 x1 D. {
var column, el, image, index, info, item, line;2 S; U5 U' K" C8 q0 z- ^9 t: a# O
el = $(e.currentTarget);
2 H- D8 a/ J" m% V* T" B7 |9 ^5 B finfo = el.find(".info");
B( ~% O8 [- I% ~image = el.find("img");" B9 ?% i7 Q% C+ o a+ ^
index = el.parent().index();/ i) ]" C' B+ b- E0 Q% H9 l- W# `! m
column = index % columns;. K/ p- q$ B9 p* M. i+ h
line = Math.floor(index / columns);3 f; w5 M+ f) k2 R I' `
console.log(index, column, line);/ _' Y5 k: C# Y5 f6 z& m
item = {
3 J( i! G7 A N; e4 \8 Uel: el,5 u+ H: g) ^+ y$ O$ ~" K# e
index: index,
8 C/ g, u) [. b, I% z+ N3 m5 [column: column,
+ P S% [8 ]3 q7 B6 a. tline: line," s( i' x5 n; F
info: info,9 h2 {; M5 i2 U4 G$ B3 k" L
image: image
$ t( w5 X! l: [9 \" a) B c};
2 \4 ^7 \6 k1 o. j4 ~if (current.el && current.index === index) return;- j3 L2 d: Y( S& O, o, X7 S
if (line === current.line && column > current.column) {" `$ o' X" C( y0 n6 \% E6 u
showItem(item, "-100%", 0, "25%", 0);$ O/ v% N2 {8 t+ c) D5 {; V7 w
hideItem(current, "100%", 0, "-25%", 0);5 \ S3 h; `3 ?& M- @* Y
} 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) {7 B3 {7 J9 F6 o/ S
showItem(item, 0, "-100%", 0, "25%");
8 f( G' A: B0 m. p+ [+ N1 vhideItem(current, 0, "100%", 0, "-25%");! F" s! E$ ]1 V" H. \
} else {
0 @2 A2 C9 h. ^! N3 u: ZshowItem(item, 0, "100%", 0, "-25%");$ \+ I. v7 N; m9 X& j% ^, Q4 O
hideItem(current, 0, "-100%", 0, "25%");
; H% c1 d4 Q4 T}% g( Q/ l, }9 Z* B. q% a) a
return current = item;
, g2 n6 k. I$ [% N! e' y) e8 S7 \});
0 X( o' h5 n6 {9 h' M/ W# pshowItem = function(item, infoX, infoY, imageX, imageY) {
4 e( d6 M2 d3 I+ R% Yitem.info.stop().css({
6 K4 G- F- |* x* ?) Sx: infoX,8 ^3 Y6 b# y) `. F+ i. I7 V
y: infoY,+ P* @7 H4 {% F0 t5 v1 k
display: "block"
+ W _7 r1 _' H4 ]% I}).transition({
1 p. ?3 A! A" |x: 0,
, Y3 G8 R% M l& w) H- e* uy: 0,
& `5 M# u5 g& rduration: time,
5 R4 N% T" Y( |. f& d0 d8 deasing: easing. `) Y; V+ x" L/ U6 g r( c
});
+ U6 c7 b; C! ^6 y1 hreturn item.image.stop().transition({
6 W+ H* V9 G# e9 `3 [$ a- i% n7 D+ Vx: imageX,6 {( I/ p; ]# G
y: imageY,6 N! k" `, X/ M2 n
opacity: .5,
5 s- K. q/ C! V/ ^1 ~+ P5 wduration: time,
- ]/ d2 U8 z- L- i9 f. [ Reasing: easing6 s* I+ }# k7 H* t
});. ^! ~, `& `' x# F1 N# N$ z, i* K
};* \ v d7 r6 y9 T3 {* n2 u" o3 d
return hideItem = function(item, infoX, infoY, imageX, imageY) {
7 l) E! k' K6 o' lif (!item.el) return;
+ s* V0 j1 c( d9 f) b& }item.info.stop().transition({; n$ W6 C1 }2 }- B/ _% d
x: infoX,
, T' K4 j" k# O" c9 Jy: infoY,
- E1 T$ |% G9 L8 u0 ~; Xduration: time,
X' O' N% y1 a1 K! ]7 ueasing: easing
+ f# t! M3 s9 W/ Y) n0 \});% J$ X- r( S; v
return item.image.stop().css({" u' C5 P+ A0 o0 n- H/ C
x: imageX,
+ T! G+ ]0 ^" n5 Ly: imageY,
; {5 f0 h. D' _+ v1 i& I6 _2 u3 ]9 mopacity: .59 _) G: g0 b$ ?
}).transition({
3 ]; e: j# Z& Dx: 0,
+ J6 X+ S) e3 p; m2 f% D- l) t0 oy: 0,1 u5 q3 ~, o" m' v$ ^5 A
opacity: 1,' U6 d2 d$ g( ?* _3 I' L# `
duration: time,7 z4 u1 i4 N* R6 e/ K! |; Y( S
easing: easing
+ G4 z1 x K* o% `5 c4 K});
) H1 Y& h$ {9 {! M/ S: A5 W ?};
/ E1 B+ N0 Z/ d7 M});
5 b: n8 f( Q5 d}).call(this);2 y$ j, a7 V) m- t( M1 G2 Y% S+ Z
- 到这里,我们的示例基本上就完成了,但是按照上面几步来,最终是没效果的,那是因为我们还没引入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规则,所以它不能兼容旧版本的浏览器。
* a# N8 ^) L9 g* h! q: m; P; Z& Q
( n1 o. w- _0 C2 a* E1 \, _. F下载附件:http://www.webjx.com/files/soft/1_130711103319.rar
& z, i9 A; h* B' @3 b4 Z9 r今天的示例就到这里了,希望大家喜欢,如果有问题可以在下方留言,一起探讨研究。 |
|