发新话题
打印

[方法技巧] 用CSS构建iframe效果

用CSS构建iframe效果

iframe应用很普遍,通常的需求有两种:
0 U+ `2 s' `8 u1 p8 C5 u
: H3 e( X9 e$ n% w2 v1 x% r( G  `1,获取iframe效果,就是带一个滚动条,可以省不少版面。
* g' a  \0 V! K+ e4 w2,要嵌一个页面,实现框架链接。2 i6 ~5 C5 \; H+ h7 ]/ f
) t/ M0 G+ i# M! Z% T2 s
如果不方便使用iframe,可以有如下解决方案:8 h/ l+ k4 W4 [9 h7 z; O
& m* }0 l( t5 Y2 u3 i  r, q+ p1 Q
第一种需求如果我们使用css布局来实现,即可以少一个页面,也可以提高效率。5 |; a: K  j! s; D
第二种需求可以采用xmlhttp远程获取。! f$ a3 n9 c, v% @2 z) d$ z
/ R7 y3 D: f2 k9 o
A. 直接模拟iframe
, {: F# t1 P% @  x& U# C) f& B8 i. L# v+ I* s. y& ~, O* U8 }
用层做容器
) f! l- m0 q' k1 V% N; V1 t. B- E! x5 x% P4 `/ k
  引用: 9 ]9 n. U* {" x2 A
#content { overflow:auto; height:200px; width:600px; background:#f00;} 0 I% ~" V$ m/ Q) ]' ?# y7 b5 D. ]' M

) f# z# t2 x% W% h
# o- ]% F1 J7 `' E7 O
% a6 @: D  F1 X- |/ G( {效果演示源代码:
9 I. `0 ?  d3 d% Y  n% U% z6 h; b' x- ?& ?9 E
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 s$ o9 t: t6 _' L' A4 p "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 l: c; @5 o9 y. c$ N<html xmlns="http://www.w3.org/1999/xhtml">( [/ ~  {5 i) u4 l& A0 ?0 T0 E
<head>
: Q5 O& G& G& A0 D5 ~<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />2 T, ^9 t1 r% g; ~4 y6 C, s  y  c
<title>用CSS构建iframe效果 - Css Iframe - RexSong.com</title>9 U+ T- B* n. R- ^
<style># c1 V5 S! p0 |+ ?1 h
* { font:12px Arial;}  a$ C- b6 H% W6 m( U' r
html { overflow:hidden; border:0; height:100%;}/ Q( _8 t( X& y% S7 y
body { overflow:hidden; height:100%;}
% q9 Z. R( U- N#content { overflow:auto; height:185px; width:600px; background:#ccc; padding:10px;}- l# D- Q/ C& @0 ]7 j; a7 l( Y' `
</style>
. C  ]$ q1 _9 O7 ]2 U: d& y4 ^# |</head>; n3 b$ T& ^* P6 Z. ~8 c
<body>
" o3 m/ u( U! Q- y6 _<h4 style="font-weight:bold;">用CSS构建iframe效果</h4>
/ {- o6 O) X$ g% a# l6 k2 l" U! n# ?) E<p>来自:<a href="http://www.webjx.com/" target="_self">网页教学网</a></p>. s1 ~8 y" d* O# t7 v
<p>A. 直接模拟iframe,用层做容器</p>3 u* \0 S2 L/ V3 f- }6 l' V
<div id="content">; z( _9 }6 t# [0 Z/ u/ [" B7 Q
<p>·<a class="grey-s" : c( `1 @6 Q3 s7 D; K
href="http://www.webjx.com/485/2354485.shtml">仅用css编写无限分级弹出菜单</a><br />
( ]( O* {& U9 g, @; C# i) X5 G   ·<a
: O' b5 E, O7 {# a2 r* Bclass="grey-s" + `" K3 U) }+ `/ L
href="http://www.webjx.com/442/2354442.shtml">浏览器滚动条的参数总结</a><br />
$ ]" G2 B/ i- Y- W6 U   ·<a 8 E; v5 X) t+ |. |( b; M; u5 k$ z
class="grey-s"
) e5 M; A8 s0 a, ?! x4 yhref="http://www.webjx.com/436/2354436.shtml">引入css样式表的四种方式介绍</a><br />1 U$ G9 n4 }' H$ K; B) j- e$ ?% C" n
   ·<a & t# ?* `8 x) p  R& q9 B0 z
class="grey-s" + d6 I7 d" w4 Q$ ~3 U
href="http://www.webjx.com/424/2354424.shtml">两种方式实现的文字竖排效果</a><br />
3 B- Q6 K1 b. ~   ·<a
2 Y8 i- l- M6 j- S# W4 pclass="grey-s"
" r) ~0 f2 a. ]& Shref="http://www.webjx.com/336/2352836.shtml">网页设计高级布局40例参考</a><br />) r2 R: b+ Q* O' Z
    ·<a " V% b) a2 e0 \% F* x' P& [
class="grey-s" 0 e" o6 m* d7 a5 y
href="http://www.webjx.com/251/2352751.shtml">从GMail认识W3C标准和Ajax</a></p>& p$ ]% Y" \! ^) I. `# b3 M
</div>' W7 Q# ]7 m9 Y9 U! m- q
</body>
% _, m7 v; O5 ?2 }% n</html>! E" |0 ]- ]; v& A7 [0 d, o
" t) a) W% p: u# h' z6 c

4 {" [$ b6 ?3 `) K* y: o9 L4 m6 N1 B, D, I- \, C; w( u& l
用body做容器
1 c" P9 ?% P7 {+ T' J
; o: H# J$ Z$ \  |& Q$ p; O) ]4 w
$ o" B; d" [* a) D0 Q! A
" f3 }) e. t9 E- ~# d/ k  引用: 7 B% u9 u4 ^. {; h$ g
html { overflow:hidden; height:100%; background:#fff; border:0;}* p0 a6 F1 H. c2 Q0 I
* html { padding:100px 0 0 100px;}$ o" U" ]; W; x5 C% v
body { overflow:scroll; background:#f00; margin:0; position:absolute; top:100px; left:100px; bottom:0; right:0;}9 M* y: y3 Q4 V; k
* html body { height:100%; width:100%;} ) A1 G! s1 o, w6 Z% A* d& g3 @

4 }; F5 S7 n3 ~: q5 L( A/ F7 ?7 V% s6 c) ]3 }; z; A

; M; i) v9 y, R6 j效果演示源代码:
$ m! i2 i- h* e+ c* }- y7 J2 T4 ?" }5 \% e; Y) q
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- \1 k9 o$ e9 t5 W "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1 b6 w4 x; N+ u$ S<html xmlns="http://www.w3.org/1999/xhtml">
9 E& y' h: Q5 }<head>
/ K4 k6 J2 q, i8 C' ]<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />5 E9 u8 j. w! U& |: `7 J4 F- J
<title>用CSS构建iframe效果 - Css Iframe - RexSong.com</title>
; S: Q) \; {/ H9 \$ M0 E; F<style>
: i7 M" P8 f+ S/ h7 `7 r1 }* { font:12px Arial;}
% r, D+ n2 Y) B. Z7 O1 l/ xhtml { overflow:hidden; height:100%; background:#fff; border:0;}
" ~. p1 k1 S& I+ `* html { padding:200px 0 0 100px;}
6 n7 q' f. ?3 D/ v0 Abody { overflow:auto; background:#ccc; margin:0; position:absolute; top:200px; left:100px; bottom:0; right:0;}" b  P5 [5 `/ |/ p- E* i! \
* html body { height:100%; width:100%; position:static;}
% ^$ x4 B9 q! p  w3 F#info { position:fixed; top:10px; left:10px;}
& S' X" X0 K. Y/ L' m% A+ h* html #info { position:absolute;}
1 K! `1 B3 S4 }: o</style>5 U  f8 u- f( `! y0 r, O& ~4 }
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
& c. I4 f8 n6 u* l' ^</script>- |; m$ n/ G( V# Z" c& u
<script type="text/javascript">
7 s0 \2 _! |4 d2 Y+ r. U& x& Z- ]_uacct = "UA-67927-3";. t( n; M3 ^# q
urchinTracker();) `' \& ?! I% U! B3 h. B
</script>5 f0 b! ^% `4 F' K
</head>) X6 [+ S9 k0 J& G, ^
<body>4 B. r5 l% e5 F0 Y: y% D
<div id="info">' j/ X3 m0 x4 r0 p# g  Y
<h4 style="font-weight:bold; margin-top:0;">用CSS构建iframe效果</h4>0 q5 P+ s6 k8 Q' \: R
<p>来自:<a href="http://www.webjx.com">网页教学网</a></p>
. {% d7 v, A5 L( x0 v# I1 D5 O <p>A. 直接模拟iframe,用body做容器</p>
& e) V7 \, H2 K8 U  f0 k</div>  t4 |; p  V; V) n1 y
<p>·<a class="grey-s"
3 S* w, ]  s) \4 z# thref="http://www.webjx.com/485/2354485.shtml">仅用css编写无限分级弹出菜单</a><br />
0 B8 c% ~) N/ w7 X4 c$ y   ·<a 8 r8 w  p2 y. @1 u$ H" s& J
class="grey-s" , n2 ^' ~- `: r3 Y
href="http://www.webjx.com/442/2354442.shtml">浏览器滚动条的参数总结</a><br />
! U5 ?7 ~. _) o+ M3 ~   ·<a
2 C$ }; M) p& u) M, {9 Sclass="grey-s" & I- m  z) R8 e) Q5 y+ W8 M% j
href="http://www.webjx.com/436/2354436.shtml">引入css样式表的四种方式介绍</a><br />
' u6 r6 y' Q+ m" B5 I   ·<a 9 t- \$ r2 E8 v9 `3 C1 k# b: {
class="grey-s" ' T1 z8 A) y/ N. o3 f! g) ~
href="http://www.webjx.com/424/2354424.shtml">两种方式实现的文字竖排效果</a><br />) c: A* O; W/ e1 k4 Q3 ~; E, A
   ·<a 5 c9 ^/ W. I& A& x
class="grey-s" " H; V' S% O* ^
href="http://www.webjx.com/336/2352836.shtml">网页设计高级布局40例参考</a><br />; @! b6 U: O" z
    ·<a 7 u' y2 Y, K2 D9 C
class="grey-s"
. N) u: r# M! P- k: yhref="http://www.webjx.com/251/2352751.shtml">从GMail认识W3C标准和Ajax</a></p>
; M' M# k2 g2 R# w  d/ L( v
( ^0 a! \) K, p1 w</body>
9 t% v0 b. [: p4 ]" U; b</html>
3 l# I' F6 [9 l0 D$ n$ M# d7 v$ L$ V$ b* z, |9 Q
) U4 s# _) i. m: R
8 u+ {, p# J4 ^/ A
B. 绝对定位模拟iframe4 F% `( T' W$ Z( V5 d3 i: w% R

/ z8 X' J; M# Q- O& Y% A滚动条在外边4 Q+ N) {: b, I+ Y, Q2 s
. v9 {  n- r) L
1 B+ |% K6 t' t. N
% m. y9 L! z) h; o
  引用: + s1 c  k( T# d' F4 |( s8 S
html { overflow:hidden; border:0; height:100%; max-height:100%;}
+ v& u) K) Y7 R! Y* i  @body { overflow:hidden; margin:0; height:100%; max-height:100%; position:relative;}
8 o: X( M: j4 c0 D* l! X6 i( U1 C: b1 ?* {: _3 r( h
#head { position:absolute; top:0; right:15px; width:100%; height:100px; background:#f00; z-index:2;}
7 l# K4 y/ t% [5 ~% i#foot { position:absolute; bottom:0; right:15px; width:100%; background:#f00; height:50px;z-index:2;}
& p, @& Q5 q8 @
0 H, z2 p: k1 \0 q* B' a# P#content { height:100%; position:relative; z-index:1; overflow:auto;} $ b- G# F4 X1 `; a
3 C3 l" N$ O0 s

% g% m3 F! E# _# U2 ^! R7 u2 c$ J
; D# @. J( B: Y: q8 O7 G效果演示源代码:0 U# k% ]% H6 b7 J
" @6 c, `* X. j" J7 J% M" J& H9 U
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">& _7 x, X1 d' q6 b
<html xmlns="http://www.w3.org/1999/xhtml">
5 ~: R: \8 ^# K$ V<head>
: ?9 z; ]/ J% T<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />, J! n" q7 R2 Q0 F) Y( ?) }
<title>用CSS构建iframe效果 - Css Iframe - RexSong.com</title>
! {* f3 I2 Y- i* \<style>: M1 e" ?% [' S1 R3 Z5 u; F; `& @$ H8 A
* { font:12px Arial;}& n1 B! O, T+ |
html { overflow:hidden; border:0; height:100%;}* {( \) w6 j: i) w  ^
body { overflow:hidden; margin:0; height:100%; position:relative;}
( h2 s  z& p# F, L6 W+ y) m; E6 K#content { height:100%; position:relative; z-index:1; overflow:auto;}
! ?1 j2 _! o) B5 y7 V#head { position:absolute; top:0; right:16px; width:100%; height:100px; background:#ccc; z-index:2;}% d+ {2 A+ d) H9 P
#foot { position:absolute; bottom:0; right:16px; width:100%; background:#ccc; height:50px;z-index:2;}/ l2 @' }4 Y( [+ Z$ n4 p3 p. L
</style>  ^8 p+ t9 e& X& Z
<script type="text/javascript">
2 ~3 o6 H% R* E/ o# ?' r_uacct = "UA-67927-3";* m8 Y% K4 ~8 }/ D) \8 d
urchinTracker();
% S0 ~2 P3 g8 y6 o! T6 Q7 T</script>% n! R0 E4 O# s' W
</head>
- R% i! a) ~3 |; L! M4 E5 s<body>. P  |$ z' j5 v) M) y
<div id="head">, w) F8 o, A/ z: _3 Y
<div style=" padding:12px 0 0 26px;_padding:6px 0 0 26px;">% G. Z) t# f, ], U8 q3 d
  <h4 style="font-weight:bold; margin-top:0;">用CSS构建iframe效果</h4>
6 Y! I5 @. R% @, e# @; c, Z& Z/ ^<p>来自:<a href="http://www.webjx.com/" target="_self">网页教学网</a></p>4 v; d8 K  M3 F: T
  <p>B. 绝对定位模拟iframe,滚动条在外边</p>+ d8 ]9 F2 p9 k# Z: v
</div>2 ~- d- f' y+ o' [1 I# r3 I
</div>, H0 _) F* S( t0 K
<div id="content">
  O) `: p. |- o. \" j <p>·<a class="grey-s" ) `: e% g1 n0 `+ W# O
href="http://www.webjx.com/485/2354485.shtml">仅用css编写无限分级弹出菜单</a><br />( Q7 \3 Q) S1 d% E
   ·<a
4 e! @) W2 q( s+ B: w: e  gclass="grey-s"
& q) m3 g/ w& Q; Ihref="http://www.webjx.com/442/2354442.shtml">浏览器滚动条的参数总结</a><br />
% g, ~& ]! s3 G' S0 l# i   ·<a
: @. w  y4 d% D# @( g- z; yclass="grey-s" * z1 Z3 F! F5 W5 o. ]
href="http://www.webjx.com/436/2354436.shtml">引入css样式表的四种方式介绍</a><br />* M- r& b0 @8 o8 M# W$ ^/ o& J1 R
   ·<a : G# v7 O6 T% B" Z4 |/ L# j$ c
class="grey-s" : n* w- k* x/ L  B
href="http://www.webjx.com/424/2354424.shtml">两种方式实现的文字竖排效果</a><br />
$ h4 o( O5 M: c   ·<a   m. G7 ^9 _1 V% Y) i: l: T0 T
class="grey-s"
! M: s* U. _& z* l9 _- @+ _- x: c" ?href="http://www.webjx.com/336/2352836.shtml">网页设计高级布局40例参考</a><br />0 T# V4 J' Q, B# n! x8 P' ~( q
    ·<a
8 d. p2 v- [& `$ c9 n9 F/ Jclass="grey-s"
2 E, V8 V5 s/ w! N1 Ohref="http://www.webjx.com/251/2352751.shtml">从GMail认识W3C标准和Ajax</a></p>+ s$ n* i) E% W5 {5 T* e
</div>0 x. H& x4 n+ X- X# f/ S
</body>
0 S7 }- ]/ n9 ^" v3 A- H+ l</html>
( C. X2 `9 U0 O. e, }( I5 F+ |
# Q+ u2 F. V6 B. n& _3 E0 {* g! z) B- r! [5 `$ t! J5 R
滚动条在里边- q, _3 h& u2 q  ~

# |; m+ H7 k, @2 c! \2 X+ e, R& c5 f- n
4 J" J6 x2 h+ Z& g. F& k7 L
  引用: * g, N- d: N2 `$ x; B* _( I' z# b
html { height:100%; max-height:100%; border:0; overflow:hidden;}
2 t3 c: n/ U2 A8 u* ]6 _. X* html { padding:100px 0 50px 0;}1 ?/ r* R( q0 T$ X, y1 m) ]
body { height:100%; max-height:100%; margin:0; overflow:hidden;}4 I$ ?* T' ~, \- ^0 Q- w

7 d8 r  s  ?# ~#content { position:absolute; top:100px; bottom:50px; left:0; right:0; z-index:3; width:100%; overflow:auto;}
, N1 ^! O+ |  J) Q5 I* html #content { top:100px; bottom:0; height:100%;}
" `) Y8 \" x% r# ]7 b9 A: \* _3 A; b1 ~$ Z1 |9 S! j0 M( t. S$ u- Y0 `
#head { position:absolute; margin:0; top:0; left:0; width:100%; height:100px; background:#f00; z-index:5;}
9 y# j- d# X1 |0 N& ]/ T2 _#foot { position:absolute; margin:0; bottom:0; left:0; width:100%; height:50px; z-index:5; background:#f00;}
1 j/ X1 R8 ^# L* ?$ i; D* t
, U3 {. d: ?: c+ S, C! q$ j, o: Y& F/ O

0 Q* O4 q9 H; L" m6 X7 |效果演示代码:; ^  U; l4 n: f2 b. A5 F

, R% L2 t0 B' N2 O2 R<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 }% I9 J( Y6 w
<html xmlns="http://www.w3.org/1999/xhtml">
( ?' q/ ^" f$ ?! D3 ~, }/ X<head>, D# i( I/ b; {. z6 k8 A( b8 t
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
: ]: V: P$ X9 E7 d<title>用CSS构建iframe效果 - Css Iframe - RexSong.com</title>
4 J1 \) |5 z1 E9 H6 W6 V% A<style type="text/css">
. l8 g; U7 ^1 @2 K* { font:12px Arial;}
' I0 \# M! B- e5 K8 H4 V! a  phtml { height:100%; max-height:100%; border:0; overflow:hidden;}
4 Y- z) F5 X% T4 Z0 t0 A* html { padding:100px 0 50px 0;}: }$ h0 I" x  U+ s* w- w
body { height:100%; max-height:100%; margin:0; overflow:hidden;}5 Y' G: p2 C% F, p! K- s
#content { position:absolute; top:100px; bottom:50px; left:0; right:0; z-index:3; width:100%; overflow:auto;}. c( I0 z/ `8 m0 }5 k2 I9 ]
* html #content { top:100px; bottom:0; height:100%;}
- x! l/ P& \: _5 ~" L#head { position:absolute; margin:0; top:0; left:0; width:100%; height:100px; background:#ccc; z-index:5;}
( S6 W: k: Q! u) X$ g2 C#foot { position:absolute; margin:0; bottom:0; left:0; width:100%; height:50px; z-index:5; background:#ccc;}
* |9 g( c1 z% m' |0 ?</style>+ X: {7 @' m6 S
<script type="text/javascript">
! ^, K; C& U6 R9 }: \" r_uacct = "UA-67927-3";
( c" W! K7 ~( i1 F: o4 m% iurchinTracker();
& K) Y6 Q7 V- E! s</script>7 s( t; d& R0 F2 E
</head>! {$ c: b; J$ O6 X  d
<body>
' n) j( p& t+ d3 p* O<div id="head">
' G& r4 Y9 }  D# w4 p+ c <div style=" padding:12px 0 0 10px;_padding:6px 0 0 10px;">+ r8 O7 V  y4 k+ [/ {  d. r' O
  <h4 style="font-weight:bold; margin-top:0;">用CSS构建iframe效果</h4>. z# K- S8 a9 z' C
<p>来自:<a href="http://www.webjx.com/" target="_self">网页教学网</a></p>
1 s! h3 [1 K% g& a- x; Q. D8 k! w% K  <p>B. 绝对定位模拟iframe,滚动条在里边</p>. ^' R: ~) X* d2 u. r: \
</div>
; b) m- a* ]# r; K</div>
/ i1 _; v* Q1 p: p& [<div id="content">
3 ^& p  v* g) b5 P: l1 \! P <p>·<a class="grey-s"
* m/ a0 d# ~- @0 q1 u% Vhref="http://www.webjx.com/485/2354485.shtml">仅用css编写无限分级弹出菜单</a><br />
7 m6 Q* t& S1 l% w! j   ·<a 1 ^- i2 g; Y0 f. l9 i) L' ]! ]
class="grey-s"
. t5 ]: E8 e/ J' W" q0 S$ Uhref="http://www.webjx.com/442/2354442.shtml">浏览器滚动条的参数总结</a><br />) K% }# f/ P& Z
   ·<a & [# j* ]- t4 F  _7 c
class="grey-s" ; ?1 [2 Q# e* y
href="http://www.webjx.com/436/2354436.shtml">引入css样式表的四种方式介绍</a><br />. e; }  e! H1 b9 U
   ·<a ' t* m2 B# B4 D# p$ C* `4 M
class="grey-s" 1 {" o4 \/ ?1 u, k1 E
href="http://www.webjx.com/424/2354424.shtml">两种方式实现的文字竖排效果</a><br />
) h) ?( q5 X# \  ]* o7 _   ·<a
2 g$ |' [  B/ c6 P% [. v% E5 ^$ Kclass="grey-s" ' P% i- d! |3 t: ~; g6 u) P  m
href="http://www.webjx.com/336/2352836.shtml">网页设计高级布局40例参考</a><br />( D7 K0 S1 ^- e, P
    ·<a : a+ W1 u6 o  J. ]: @) O
class="grey-s" + K  {, U/ u5 |& |
href="http://www.webjx.com/251/2352751.shtml">从GMail认识W3C标准和Ajax</a></p>& {8 `% ^3 P4 [# ~4 h  b$ H, i
</div>1 _- E8 T* i5 U
</body>
5 n  L% l3 c0 e</html>
发新话题