jquery实现页面图片等比例放大缩小功能


Posted in Javascript onFebruary 12, 2014

html代码结构:

<a href=""><img src="images/tmp_376x470.jpg" width="300" height="300" alt=""/></a>
<a href=""><img src="images/tmp_409x265.jpg" width="300" height="300" alt=""/></a>
<a href=""><img src="images/tmp_572x367.jpg" width="300" height="300" alt=""/></a>

样式:

a{width:300px;height:300px;background:#fff;border:1px solid #666;display:inline-block} /* 这里需要指定a标签的高宽,背景和边框为可选 */
脚本(jquery可自行添加):
$(function () {
    var imgs = $('a>img');
    imgs.each(function () {
        var img = $(this);
        var width = img.attr('width');//区域宽度
        var height = img.attr('height');//区域高度
        var showWidth = width;//最终显示宽度
        var showHeight = height;//最终显示高度
        var ratio = width / height;//宽高比
        img.load(function () {
            var imgWidth, imgHeight, imgratio;
            $('<img />').attr('src', img.attr('src')).load(function () {
                imgWidth = this.width;//图片实际宽度
                imgHeight = this.height;//图片实际高度
                imgRatio = imgWidth / imgHeight;//实际宽高比
                if (ratio > imgRatio) {
                    showWidth = height * imgRatio;//调整宽度太小
                    img.attr('width', showWidth).css('margin-left', (width - showWidth) / 2);
                } else {
                    showHeight = width / imgRatio;//调高度太小
                    img.attr('height', showHeight).css('margin-top', (height - showHeight) / 2);
                }
            });
        });
    });
});

这样就是实现了图片的等比例放大缩小了。

Javascript 相关文章推荐
Microsoft Ajax Minifier 压缩javascript的方法
Mar 05 Javascript
关于跨站脚本攻击问题
Dec 22 Javascript
js将控件隐藏及display属性的使用介绍
Dec 30 Javascript
node.js中的http.request.end方法使用说明
Dec 10 Javascript
node.js中的buffer.Buffer.isBuffer方法使用说明
Dec 14 Javascript
使用jQuery获得内容以及内容的属性
Feb 26 Javascript
Underscore.js常用方法总结
Feb 28 Javascript
jquery实现文本框textarea自适应高度
Mar 09 Javascript
jquery拖动层效果插件用法实例分析(附demo源码)
Apr 28 Javascript
基于复选框demo(分享)
Sep 27 Javascript
讲解vue-router之什么是动态路由
May 28 Javascript
JavaScript实现左右滚动电影画布
Feb 06 Javascript
javascript获取web应用根目录的方法
Feb 12 #Javascript
使用javascript控制cookie显示和隐藏背景图
Feb 12 #Javascript
raphael.js绘制中国地图 地图绘制方法
Feb 12 #Javascript
js post提交调用方法
Feb 12 #Javascript
JQuery中操作Css样式的方法
Feb 12 #Javascript
Jquery的Tabs内容轮换效果实现代码,几行搞定
Feb 12 #Javascript
js判断设备是否为PC并调整图片大小
Feb 12 #Javascript
You might like
PHP获取当前文件所在目录 getcwd()函数
2009/05/13 PHP
yii框架中的Url生产问题小结
2012/01/16 PHP
PHP容易忘记的知识点分享
2013/04/30 PHP
基于PHP array数组的教程详解
2013/06/05 PHP
TP5多入口设置实例讲解
2020/12/15 PHP
网页禁用右键实现代码(JavaScript代码)
2009/10/29 Javascript
在javascript中对于DOM的加强
2013/04/11 Javascript
JavaScript每天定时更换皮肤样式的方法
2015/07/01 Javascript
jquery实现的点击翻书效果代码
2015/11/04 Javascript
Es6 写的文件import 起来解决方案详解
2016/12/13 Javascript
JS 中使用Promise 实现红绿灯实例代码(demo)
2017/10/20 Javascript
JavaScript实现区块链
2018/03/14 Javascript
vue-router动态设置页面title的实例讲解
2018/08/30 Javascript
atom-design(Vue.js移动端组件库)手势组件使用教程
2019/05/16 Javascript
Node.js 实现简单的无侵入式缓存框架的方法
2019/07/21 Javascript
微信小程序实现底部弹出模态框
2020/11/18 Javascript
python标准日志模块logging的使用方法
2013/11/01 Python
Linux下用Python脚本监控目录变化代码分享
2015/05/21 Python
Python中使用支持向量机SVM实践
2017/12/27 Python
详解python中的线程
2018/02/10 Python
解决seaborn在pycharm中绘图不出图的问题
2018/05/24 Python
Django 视图层(view)的使用
2018/11/09 Python
Python+Pyqt实现简单GUI电子时钟
2021/02/22 Python
Python 元组操作总结
2019/09/18 Python
美国珠宝店:Helzberg Diamonds
2018/10/24 全球购物
康拓普公司Java笔面试
2016/09/23 面试题
简单说说tomcat的配置
2013/05/28 面试题
介绍一下内联、左联、右联
2013/12/31 面试题
EJB3.1都有哪些改进
2012/11/17 面试题
个人求职信范文分享
2014/01/06 职场文书
《落花生》教学反思
2014/02/25 职场文书
《桥》教学反思
2014/04/09 职场文书
销售口号霸气押韵
2015/12/24 职场文书
2019年朋友圈经典励志语录50条
2019/07/05 职场文书
html css3不拉伸图片显示效果
2021/06/07 HTML / CSS
python 进阶学习之python装饰器小结
2021/09/04 Python