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 相关文章推荐
javaScript parseInt字符转化为数字函数使用小结
Nov 05 Javascript
jquery.cookie.js 操作cookie实现记住密码功能的实现代码
Apr 27 Javascript
基于javascript的COOkie的操作实现只能点一次
Dec 26 Javascript
javascript遇到html5的一些表单属性
Jul 05 Javascript
JS触摸事件、手势事件详解
May 04 Javascript
jQuery响应滚动条事件功能示例
Oct 14 jQuery
在LayUI图片上传中,解决由跨域问题引起的请求接口错误的方法
Sep 24 Javascript
微信小程序自定义扫码功能界面的实现代码
Jul 02 Javascript
AngularJs的$http发送POST请求,php无法接收Post的数据问题及解决方案
Aug 13 Javascript
jquery插件实现搜索历史
Apr 24 jQuery
JS如何实现基于websocket的多端桥接平台
May 14 Javascript
JavaScript阻止事件冒泡的方法
Dec 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的Yii框架中的日志功能
2016/03/17 PHP
Laravel框架模板加载,分配变量及简单路由功能示例
2018/06/11 PHP
浅谈laravel数据库查询返回的数据形式
2019/10/21 PHP
PHP笛卡尔积实现原理及代码实例
2020/12/09 PHP
ExtJs GridPanel简单的增删改实现代码
2010/08/26 Javascript
jQuery 表单验证扩展代码(二)
2010/10/20 Javascript
JavaScript中的常见问题解决方法(乱码,IE缓存,代理)
2013/11/28 Javascript
indexedDB bootstrap angularjs之 MVC DOMO (应用示例)
2016/06/20 Javascript
bootstrap weebox 支持ajax的模态弹出框
2017/02/23 Javascript
Vue 2.0入门基础知识之内部指令详解
2017/10/15 Javascript
JS简单实现点击跳转登陆邮箱功能的方法
2017/10/31 Javascript
微信小程序签到功能
2018/10/31 Javascript
jQuery实现的3D版图片轮播示例【滑动轮播】
2019/01/18 jQuery
JS浅拷贝和深拷贝原理与实现方法分析
2019/02/28 Javascript
使用JS判断页面是首次被加载还是刷新
2019/05/26 Javascript
教你完全理解ReentrantLock重入锁
2019/06/03 Javascript
简单了解小程序+node梳理登陆流程
2019/06/24 Javascript
jquery实现进度条状态展示
2020/03/26 jQuery
JS实现省市县三级下拉联动
2020/04/10 Javascript
原生js实现五子棋游戏
2020/05/28 Javascript
使用Python的内建模块collections的教程
2015/04/28 Python
Python 删除连续出现的指定字符的实例
2018/06/29 Python
python 模拟贷款卡号生成规则过程解析
2019/08/30 Python
对python中assert、isinstance的用法详解
2019/11/27 Python
Python实现剪刀石头布小游戏(与电脑对战)
2019/12/31 Python
Python文件操作方法详解
2020/02/09 Python
python MD5加密的示例
2020/10/19 Python
中专生毕业自我鉴定
2013/11/01 职场文书
《生命 生命》教学反思
2014/04/19 职场文书
股东授权委托书范文
2014/09/13 职场文书
2015年市场部工作总结
2015/04/30 职场文书
爱护环境建议书
2015/09/14 职场文书
当你找不到方向的时候,不妨读读刘备的一生
2019/08/05 职场文书
怎样写好演讲稿题目?
2019/08/21 职场文书
如何判断微信付款码和支付宝付款码
2021/04/01 PHP
mybatis 解决从列名到属性名的自动映射失败问题
2021/06/30 Java/Android