原图:

Home  >  Article  >  Web Front-end  >  JS+CSS设置img在DIV中只显示Img垂直居中的部分_javascript技巧

JS+CSS设置img在DIV中只显示Img垂直居中的部分_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:19:07956browse

一个Div中显示Img,img的宽和Div相同,但高不固定,要求只显示Img垂直居中的部分。

示例:
JS+CSS设置img在DIV中只显示Img垂直居中的部分_javascript技巧 

原图:
JS+CSS设置img在DIV中只显示Img垂直居中的部分_javascript技巧

代码实现:

复制代码 代码如下:


JS+CSS设置img在DIV中只显示Img垂直居中的部分_javascript技巧onload="this.style.marginTop = (parseInt(this.parentNode.style.height) - this.height)/2 + 'px';this.style.marginLeft = (parseInt(this.parentNode.style.width) - this.width) /2 + 'px'" />


点击下载源文件
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn