使用方法:

1.定义ResizeImg(obj)方法

 function ResizeImg(obj) {
var boxHeight = $(".box").height();
var boxWidth = $(".box").width();
var imgHeight = $(obj).height();
var imgWidth = $(obj).width();
$(obj).css("position", "relative");
if (imgHeight * boxWidth >= imgWidth * boxHeight) {
//调整后,高度超出的情况
var afterImgHeight = boxWidth * imgHeight / imgWidth;
$(obj).css("width", boxWidth + "px").css("height", afterImgHeight + "px");
var offsetTop = (afterImgHeight / 2) - (boxHeight / 2);
$(obj).css("top", "-" + offsetTop + "px"); // -20px
}
else {
//调整后,宽度超出的情况
var afterImgWidth = boxHeight * imgWidth / imgHeight;
$(obj).css("height", boxHeight + "px").css("width", afterImgWidth + "px");
var offsetLeft = (afterImgWidth / 2) - (boxWidth / 2);
$(obj).css("left", "-" + offsetLeft + "px"); // -20px
}
console.log("resize ok.");
}

2.在img标绑定onload事件处理方法为ResizeImg.

<img src="xxx.jpg"  onload="ResizeImg(this)"/>

3.注意:

不设置图片的宽,高;

需设置图片定位属性,position: relative;

效果如下图:

js脚本控制图片水平与垂直居中的更多相关文章

  1. DIV里面的图片水平与垂直居中的方法

    <div class=“box”> <img /> </div> 1.水平居中: 1)box设置  text-align:center ;    text-alig ...

  2. DIV或者DIV里面的图片水平与垂直居中的方法

    <div class=“box”> <img /> </div> 水平居中的常用方式: text-align:center ——这可以实现子元素字体,图片的水平居中 ...

  3. 使图片水平并垂直居中的一个Hack

    淘宝的一个前端面试题:使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中. 想起了vertical-align:middle;但是不行,后来才知道还要di ...

  4. css设置图片水平及垂直居中

    .box { /*非IE的主流浏览器识别的垂直居中的方法*/ display: table-cell; vertical-align:middle; /*设置水平居中*/ text-align:cen ...

  5. JS/JQuery控制图片宽度

    function changeImgWidth(){ for (i = 0; i <$('#info img').length; i++) { var imgWidth=$('#info img ...

  6. jQuery.rotate.js(控制图片转动)

    jQuery.rotate.js笔记   1. jQuery.rotate.js是什么 一个开源的兼容多浏览器的jQuery插件用来对元素进行任意角度的旋转动画. 这个库开发的目的是为了旋转img的, ...

  7. js 滚轮控制图片缩放大小和拖动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. css 水平、垂直居中

    水平居中 行内元素 行内元素:(img.span.文字等行内元素),通过在父级元素设置 text-align:center 使元素水平居中. 块级元素 块级元素:(div.p.h1...h6.ul.l ...

  9. js控制图片缩放、水平和垂直方向居中对齐

    已測试兼容 IE6,IE7,IE8,火狐FF,谷歌chrome. 这里使用了jquery插件,假设你不使用jquery,略微改造一下也非常快. 网上查了些资料,用css控制兼容性不好,看去非常揪心.于 ...

随机推荐

  1. ELK平台介绍

    在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自:http://baidu.blog.51cto.com/71938/1676798 日志主要包括系统日志.应用程序日志和安全 ...

  2. {sharepoint} Setting List Item Permissions Programatically in sharepoint

    namespace Avinash { class Program { static void Main(string[] args) { SetListItemPermission(); } sta ...

  3. Android DatepickerDialog(日期选择器)的使用

    效果图如下: 日期和时间选择对话框,首先是要获得当前时间,这里用 java类中的Calendar来获得日期和时间(也可以用Date,但是不提倡,Date部分方法已经注释为过时), Calendar是一 ...

  4. linux :故障提示:Error:No suitable device found: no device found for connection "System eth0"

    解决:1./etc/udev/rules.d/70-persistent-net.rules 中的mac地址2.网卡配置文件ifcfg-eth0中的mac地址3.ifconfig中的mac地址以上三个 ...

  5. thinkphp --- 写入日志

    在开发过程中,对于一些参数,不好直接输入或者打印调试,特别是在微信开发过程中,这个时候,通过日志来查看信息就显得格外重要. 下面是在TP3.2.3框架中,写入日志的方法: public functio ...

  6. [转] mysql数据库分离和附加

    1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql 2.导 ...

  7. shiro框架的学习

    1shiro框架是什么:是一个权限控制的框架2shiro框架有什么作用:权限管理,管理那些资源是否需要登录才能访问.控制某些资源需要那些权限才能访问3shiro框架怎样使用: 1在web.xml配置s ...

  8. 使用 Visual Studio 将 ASP.NET Web 应用部署到 Azure

    原文地址:https://www.azure.cn/zh-cn/documentation/articles/web-sites-dotnet-get-started 配置新的 Web 项目 下一步是 ...

  9. MYSQL查看数据表最后更新时间

    MYSQL查看数据表最后更新时间 - 拨云见日 - CSDN博客 https://blog.csdn.net/warnerwu/article/details/73352774 mysql> S ...

  10. This module embeds Lua, via LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.

    openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers https://github.com/openre ...