- 论坛徽章:
- 0
|
原帖由 荒野狐狸 于 2008-4-7 19:37 发表 ![]()
有一个图片容器( )和一组图片,鼠标移动到某一图片上时,图片容器加载该图片。如果宽度大于400则等于400,高度大于300则等于300.
例如:
原始图片为500*200,则显示在容器里为400*200
原始图片为200*500,则显 ...
- function toSize(elm,options){
- var jelm=$(elm),w=jelm.width(),h=jelm.height(),width,height;
- /*原来没有这一段,我实例写个伪代码,你自己可以改改*/
- if(!options){
- options.max.width=(w==500)?400:xxxxxxx;
- options.max.height=(h==500)?300:xxxxxxx;
- }
- /********/
- if(options.max){
- options.max.width = options.max.width || w;
- options.max.height= options.max.height || h;
- width = options.max.width<w ? options.max.width : w;
- height = options.max.height<h ? options.max.height : h;
- }
- if(options.min){
- options.min.width = options.min.width || w;
- options.min.height= options.min.height || h;
- width = options.min.width > w ? options.min.width : w;
- height = options.min.height > h ? options.min.height : h;
- }
- height = Math.round(width * h / w);
- jelm.css({width:width + 'px',height:height+'px'});
- }
复制代码
以前自己写的,使用了jQuery,不过改成基本的javascript也很容易对于你的应用类似使用 onXXXXX="toSize(this);"
[ 本帖最后由 achun.shx 于 2008-4-8 00:28 编辑 ] |
|