【问题标题】:Resize div with aspect ratio to fit content css image使用纵横比调整 div 大小以适合内容 css 图像
【发布时间】:2013-11-12 20:39:54
【问题描述】:
您好,我有一个 div,作为背景,我有一个宽度为 1920 像素,高度为 1200 像素的图像。
div 有 100% 的宽度和window.innerHeight 准备好文档;
背景css图片有以下规则:background-size: 100% auto !important;
让他们反应灵敏
我想做的是根据 window.resize 计算 div 元素的新高度并将其设置为与背景图像 css 高度匹配。
【问题讨论】:
标签:
javascript
jquery
css
【解决方案1】:
newHeight = (1200 * divWidth) / 1920
【解决方案2】:
如果我理解你的话,你不需要 js
background: url(../img/image.png) no-repeat center top fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;