<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*示例图片比例600x900 2:3*/
.ratio{
width: 200px;
background:black url("http://img.hb.aicdn.com/577bb1aed67d6a979b9ba46bf9e8491cb98a4f3f195a0-LPL1qm_fw658") no-repeat;
background-size: contain;
transition: all .8s;
}
.ratio:before{
content: "";
display: block;
/*eleP.width /( padding * eleP.width )= originalW/originalH*/
/*padding-bottom = originalH /originalW */
padding-bottom: 150%;
float: left;
}
.ratio:after{
content: "";
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="ratio"></div>
<script>
function getEleStyleSheetProperty(ele,property) {
var styleSheet = window.getComputedStyle(ele,null);
return +styleSheet[property].replace("px","");
}
setTimeout(function () {
var ele = document.querySelector(".ratio");
ele.style.width =getEleStyleSheetProperty(ele,"width")*1.5+"px"
},1000);
</script>
</body>
</html>
``
设置等比例背景
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 背景设置(参考前边rem布局代码理解) .div{ position:absolute; width:10rem;...
- 1>众所周知,在给系统的Button设置文字和image的时候(非背景颜色),系统默认是左边图片,右边文字,可现在...