打印

[ 原创 ] 我也浅析阿里巴巴前端招聘考题 [ 另一种解决方案 ] (大家一起进来优化)

本主题由 System 于 2008-2-10 05:00 解除限时高亮
博客阅读 >>

废话不多说,先往下看!


用css布局略过了,我们直接看用 javascript优化布局
引用:
由于我们的用户群喜欢放大看页面  

于是我们给上一题的布局做一次优化。  

当鼠标略过某个区块的时候,该区块会放大25%,  

并且其他的区块仍然固定不动。

  

提示:也许,我们其他的布局也会用到这个放大的效果哦。可以使用任何开源代码,包括曾经你自己写的。  

关键字:javascript、封装、复用
放大有什么好处?

把模块放大肯定是为了保证残障人士或者一些特殊群体,同时也给普通用户群带来良好的体验。那放大,肯定是模块里面所有东西都得放大了包括图片等!在蓝色理想论坛已经有人写好了http://bbs.blueidea.com/thread-2824913-1-5.html,但完美主义是挑剔的。帖子写的很好,但如果放大模块上出现图片,他又要在js里面加上图片的放大效果,如果需求有更改,又给加上别的元素,那又要改?那岂不是变的很不灵活?下面我们做一个相对比较灵活的!怎么实现给卖个关子,请往下看

我们先写出布局
复制内容到剪贴板
代码:
<style type="text/css" media="all">  
/* <![CDATA[ */  
body{margin:100px; font-size:62.5%}  
.box1{
  float:left;
  width:200px;  
  font-size:1.2em;
  background:#f30
}  
.box2{
  clear:left;
  float:left;
  width:200px;
  margin-top:10px;  
  font-size:1.2em;
  background:#f60
}  
.box3{
  width:200px;
  margin-left:210px;
  _margin-left:207px; /* ie6 */  
  font-size:1.2em;
  background:#f90
}  
p{margin:0; padding:1em; line-height:180%}
img{width:10em; height:2em; display:block}  
/* ]]> */  
</style>  
<!--这里给出html-->  
<div class="box1" id="container">
  <p>这是第一个可放大内容的区域,里面可能有N多元素和图片等!<img src="http://www.macji.com/wp-content/themes/macji/img/logo.png"   /></p>
</div>
<div class="box2" id="container2">
  <p>这是第一个可放大内容的区域,里面可能有N多元素和图片等!<img src="http://www.macji.com/wp-content/themes/macji/img/logo.png"  /></p>
</div>
<div class="box3">
  <p>这是第三个区域</p><p>这是第三个区域</p><p>这是第三个区域</p><p>这是第三个区域</p>
</div>
你看到这里时候,没觉得不一样吧,是的,就是CSS单位都是em(除了布局用px,保证结构不乱),对,这是另一个思路,采用css可变单位em进行子元素控制.也许你就有了头绪吧?

我们把JavaScript封装一下
复制内容到剪贴板
代码:
function zoom (contianer, config){
  this.config = config || {x: 1.25, y: 1.25};
  this.contianer = document.getElementById(contianer);
  var _this = this;  
  this.__construct = function (){ //构造函数
    _this.contianer.onmouseover = function (){_this.over();};
    _this.contianer.onmouseout  = function (){_this.out();};
  }();  
  this.over =  function (){ //鼠标经过
    var obj = _this.contianer;
    var width  = obj.offsetWidth;
    var height = obj.offsetHeight;  
    obj.style.position = 'relative';
    obj.style.zIndex = '100000';
    obj.style.fontSize = 1.2 * _this.config.x + 'em';
    obj.style.width = width * _this.config.x + 'px';
    obj.style.height = height * _this.config.y + 'px';
    obj.style.marginRight = (width - width * _this.config.x) + 'px';
    obj.style.marginBottom = (height - height * _this.config.y) + 'px';
  };  
  this.out = function (){ //鼠标离开
    var obj = _this.contianer;
    obj.style.position = '';
    obj.style.fontSize = '';
    obj.style.zIndex   = '';
    obj.style.width    = '';
    obj.style.height   = '';
    obj.style.marginRight = '';
    obj.style.marginBottom = '';
  };
}
是不是很简单啊?调用方法,只需要把要放大的id和放大倍数传进去就可以了!
var container = new zoom(’container’, {x: 1.25, y: 1.25});
var container2 = new zoom(’container2′, {x: 1.25, y: 1.25});

给出演示地址 http://www.macji.com/lab/ali_job.html

[ 本帖最后由 caiying2007 于 2008-2-2 11:50 编辑 ]
顶楼主的创意! 我都忘记了em单位了 果然 用em可以少写很多代码呢 参照字体 这个想法好好
不过设图片宽高都用em 总是感觉有点怪怪的

我来点优化意见 直接把new zoom(’container’, {x: 1.25, y: 1.25});写入<div class=" .....>里面 然后去call它

[ 本帖最后由 sxnow 于 2008-2-2 14:45 编辑 ]
直接把new zoom(’container’, {x: 1.25, y: 1.25});写入<div class=" .....>里面

这想法好,但页面就显的不干净了,应该放在一个js文件里面,然后去包含它,这样比较干净,毕竟不是一个人在做项目
不影响的啊 其它class还是生效的 比如<div id="L_1" class="Font_Blue Box_Zoom{Z:'125%',T:true}" ...> 我是这样写的

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!
恩,这样也不错,但我们一直主张着分离

TOP

可惜不支持IE7,高手能不能把组合好的代码发出来看看~

[ 本帖最后由 mqycn 于 2008-2-5 20:25 编辑 ]

TOP

谁说不支持IE7,那个测试页我就用着没问题
圆通快递是垃圾!

TOP

回复 #6 mqycn 的帖子

这是简单的布局,没有复杂css,支持各种浏览器.

TOP