【发布时间】:2015-05-15 07:29:34
【问题描述】:
我正在尝试实现本网站http://www.dubsmash.com/ 中所做的全长响应式背景图像
我尝试将background-size: cover; background:url('image') no-repeat center center; overflow:hidden; min-height:100% 应用于html
但结果不一样。
在 dubsmash 网站中查看图像始终居中的方式。他们使用 jquery 来动态计算 window.resize 上包装 div 内图像的高度、宽度和左侧、顶部属性。
我的问题是有人可以帮助我理解他们如何计算宽度,顶部和左侧到img
更新:
到目前为止我的代码: HTML:
<div class="bgHome" style="position:fixed;z-index:-1;top:0;left:0;overflow:hidden">
<img src="http://www.dubsmash.com/wp-content/themes/dubsmash2.0/pics/woman.jpg" style="position:absolute;top:0"/>
</div>
jQuery:
$(document).ready (function(){
checkDims();
});
$(window).resize(checkDims);
function checkDims(){
a = $(window).height();
b = $(window).width();
$('.bgHome').css({"height":a,"width":b});
$('.bgHome img').css({"height":a,"width":b});
}
更新-2:
<html></html>
CSS:
html{
background-size:100%;top:0; left:0;
background:url('http://www.dubsmash.com/wp-content/themes/dubsmash2.0/pics/woman.jpg') no-repeat center center;
overflow:hidden;width:100%;height:100%
}
【问题讨论】:
-
你需要视差效果吗?您是否还希望您的背景图像在滚动时移动?
-
我不是在寻找视差效果和没有滚动。我只是想知道我是否可以在设置背景图像时使用类似的概念
-
是的,您可以,例如,您要拍摄第一张图片吗?
-
他们也在改变背景位置
-
@Scott,我没有看到 div 或 img 标签的背景位置属性
标签: css background