JS自动缩放页面图片
2016-07-17 09:30
531 查看
/** * 缩略图 * * @param bool isScaling 是否缩放 * @param int width 宽度 * @param int height 高度 * @param string loadindPic 表示“正在加载中”的图片地址 */ jQuery.fn.LoadImage = function (isScaling, width, height, loadindPic) { if (loadindPic == null) { loadindPic = "../images/msg_img/loading.gif"; } return this.each(function () { var _this = $(this); var src = $(this).attr("src") var img = new Image(); img.src = src; // 自动缩放图片 var autoScaling = function () { if (isScaling) { if (img.width > 0 && img.height > 0) { if (img.width / img.height >= width / height) { if (img.width > width) { _this.width(width); _this.height((img.height * width) / img.width); } else { _this.width(img.width); _this.height(img.height); } } else { if (img.height > height) { _this.height(height); _this.width((img.width * height) / img.height); } else { _this.width(img.width); _this.height(img.height); } } } } } // 处理ff下会自动读取缓存图片 if (img.complete) { autoScaling(); return; } $(this).attr("src", ""); var loading = $("<img alt=\"加载中...\" title=\"图片加载中...\" src=\"" + loadindPic + "\" />"); _this.hide(); _this.after(loading); $(img).load(function () { autoScaling(); loading.remove(); _this.attr("src", this.src); _this.show(); //$('.photo_prev a,.photo_next a').height($('#big-pic img').height()); }); }); } // 应用举例 $(function () { $('#Article .content img').LoadImage(true, 660, 660, 'http://127.0.0.12:8088/statics/images/s_nopic.gif'); })
相关文章推荐
- Selenium+PhantomJS自动续借图书馆书籍(下)
- JavaScript跳转到页面某个锚点#
- 细说JavaScript对象(4): for in 循环
- 细说JavaScript对象(3):hasOwnProperty
- 细说JavaScript对象(2):原型对象
- 细说JavaScript对象(1):对象的使用和属性
- javaScript--05 DOM基础 12.12
- javaScript--05 DOM基础 12.11
- JavaScript--05 DOM基础 12.9 12.10 复制、克隆节点
- JavaScript传递变量:值传递?引用传递?
- 编写高性能JavaScript
- 学习Javascript的8张思维导图
- JavaScript实现异步的4种方法
- Javascript闭包深入解析及实现方法
- JavaScript模板引擎的应用场景及实现原理
- 编写高性能的JavaScript事件
- JavaScript数组操作函数方法详解
- JavaScript事件冒泡、事件捕获和阻止默认事件
- 强类型 JavaScript 的解决方案
- JavaScript模块化开发的那些事