jquery图片尺寸调整插件图片垂直居中自适应容器
来源:互联网 发布:唐朝有多强大 知乎 编辑:程序博客网 时间:2024/05/22 03:09
</pre><p>内容:</p><p><pre name="code" class="javascript">(function($) { $.fn.extend({ resizeImg: function(opt, callback) { var defaults = { w: 200, h: 150 }, opts = $.extend(defaults, opt); //获取图片实际宽高,此方法摘自网络 var getImgWh = function(url, callback) { var width, height, intervalId, check, div, img = new Image(), body = document.body; img.src = url; //从缓存中读取 if (img.complete) { return callback(img.width, img.height); }; //通过占位提前获取图片头部数据 if (body) { div = document.createElement('div'); div.style.cssText = 'visibility:hidden;position:absolute;left:0;top:0;width:1px;height:1px;overflow:hidden'; div.appendChild(img) body.appendChild(div); width = img.offsetWidth; height = img.offsetHeight; check = function() { if (img.offsetWidth !== width || img.offsetHeight !== height) { clearInterval(intervalId); callback(img.offsetWidth, img.clientHeight); img.onload = null; div.innerHTML = ''; div.parentNode.removeChild(div); }; }; intervalId = setInterval(check, 150); }; // 加载完毕后方式获取 img.onload = function() { callback(img.width, img.height); img.onload = img.onerror = null; clearInterval(intervalId); body && img.parentNode.removeChild(img); }; }; this.each(function() { var _this = this; getImgWh(this.src, function(imgWidth, imgHeight) { //计算图片最大宽度 if (imgWidth > opts["w"]) { _this.width = opts["w"]; _this.height = imgHeight * (opts["w"] / imgWidth); imgWidth = opts["w"]; imgHeight = _this.height; } //计算图片最大高度 if (imgHeight > opts["h"]) { _this.height = opts["h"]; _this.width = imgWidth * (opts["h"] / imgHeight); imgHeight = opts["h"]; imgWidth = _this.width; } //水平居中,垂直居中 $(_this).css({ "margin-top": (opts["h"] - imgHeight) / 2, "margin-left": (opts["w"] - imgWidth) / 2 }); }); }); } });})(jQuery);
调用方法
$("ul li img").resizeImg({w:190, //设置图片最大宽度h:80 //设置图片最大高度});
0 0
- jquery图片尺寸调整插件图片垂直居中自适应容器
- jquery图片尺寸调整插件图片垂直居中自适应容器
- css图片垂直居中自适应
- CSS+DIV调整图片样式垂直居中
- 超出容器高度的图片垂直居中,图片高度不定
- 列表图片自适应垂直居中-空白标签+js
- 图片自适应div高宽并且垂直居中
- div,li内部图片自适应水平垂直居中
- 容器垂直水平居中
- 垂直居中及容器内图片垂直居中的CSS解决方法
- 垂直居中及容器内图片垂直居中的CSS解决方法
- Div垂直居中及容器内图片垂直居中的CSS解决方法
- 让文本或图片在div容器中垂直居中
- 让文本或图片在div容器中垂直居中
- jQuery实现图片垂直居中,js怎样实现图片水平垂直居中显示
- css html 如何将图片img标签 水平居中 垂直居中 和水平垂直居中,能自适应浏览器缩放。
- 自适应弹出框-垂直居中
- 自适应布局下的图片垂直水平居中,自适应手机横竖屏
- LR接口性能测试提示Code - 60990 Error: Two Way Communication Error: Function two_way_comm_post_message / two_
- Unity3D 多种播放音效的方式
- 视频监控相关研究方向
- UBIFS文件系统分析四之UBIFS的六个area
- css3学习笔记1
- jquery图片尺寸调整插件图片垂直居中自适应容器
- NGUI的Scroll View的制作
- Linux Mint 17 安装Codeblocks 13.12
- Linux下VMWare的装卸命令
- WinDbg F9时“code not found breakpoint not set”
- IDE 快捷键
- cocoapods specs 镜像
- UIDatePicker介绍
- php的ord函数——解决中文字符截断问题