用 CSS 实现布局
让我们一起来做一个页面。首先,我们需要一个布局。
请使用 CSS 控制 3 个 div,实现如下图的布局。
这题不难,在平时项目开发过程中也经常会碰到:
主要考虑几个问题:1. IE6 的 3 像素 BUG;2. 清楚浮动;
代码:
1 |
div{background:#CCCCCC;} |
2 |
#first{float:left;width:100px; height:150px} |
3 |
#second{clear:left;float:left;margin-top:10px;width:100px;height:150px} |
4 |
#third{zoom:1; width:200px;margin-left:110px;_margin-left:107px;height:310px} |
XML/HTML代码:
2 |
<div id="second"></div> |
用 javascript 优化布局
由于我们的用户群喜欢放大看页面,于是我们给上一题的布局做一次优化。
当鼠标略过某个区块的时候,该区块会放大25%,并且其他的区块仍然固定不动。
也许,我们其他的布局也会用到这个放大的效果。可以使用任何开源代码,包括曾经你自己写的。
关键字:javascript、封装、复用。
惭愧啊,用上边那个布局我怎么也没把它优化出来,硬这头皮用绝对定位改了布局;
所以样式改成了这样:
1 |
body{ margin:0; padding:0} |
2 |
div{background:#CCCCCC; position:absolute} |
3 |
#first{width:100px; height:150px} |
4 |
#second{top:160px;width:100px;height:150px} |
5 |
#third{ width:200px; height:310px; left:110px} |
javascript 要考虑封装、复用。
JavaScript代码:
04 |
var obj=document.getElementById(id); |
05 |
var dW=obj.clientWidth; |
06 |
var dH=obj.clientHeight; |
09 |
obj.onmouseover=function(){ |
10 |
this.style.width=dW*x+"px"; |
11 |
this.style.height=dH*y+"px"; |
12 |
this.style.backgroundColor="#f00″; // 设置调试背景 |
15 |
obj.onmouseout=function() { |
18 |
this.style.padding=""; |
19 |
this.style.backgroundColor=""; |
23 |
zoom("first",1.25,1.25); |
24 |
zoom("second",1.25,1.25); |
25 |
zoom("third",1.25,1.25); |