【发布时间】:2014-02-12 19:16:41
【问题描述】:
首先,对不起我的英语:-)
我正在尝试完全重新设计我的博客。它必须是响应式的,我想在我的标题中集成视差效果(这就是我使用图片作为背景而不是图像标签的原因)。视差已经有效,但是第二层“标题标题”有问题。如果我调整窗口大小,我的标题背景会自动调整其宽度和高度,但“标题标题”图层不会。
在这里你可以看到问题:http://jsfiddle.net/utkcT/
<div id="header" data-type="background" data-speed="10">
<div id="header-title">
Dieser Text ist zentriert!
</div>
</div>
#header{
width:100%;
height:500px;
background-attachment:fixed;
background-image: url("http://images.nationalgeographic.com/wpf/media-live/photos/000/411/cache/northern-lights-picture-aurora-borealis-september-2011-yukon_41173_600x450.jpg");
background-repeat:no-repeat no-repeat;
background-size: contain;
-webkit-background-size: contain;
-moz-background-size: contain;
}
#header-title{
background: -moz-linear-gradient(bottom, rgba(0,0,0,0.8), rgba(0,0,0,0.2));
background: -webkit-linear-gradient(bottom, rgba(0,0,0,0.8), rgba(0,0,0,0.2));
width:100%;
height:500px;
padding-top:25%;
color:#fff;
text-align:center;
}
$(document).ready(function(){
$(window).on('resize', function(){
$('#header-title').height($('#header').height());
});
// ---------------PARALLAX FUNCTION ------------------------
$('data-type="background"').each(function(){
var $bgobj = $(this); // assigning the object
$(window).scroll(function() {
var yPos = -($window.scrollTop() / $bgobj.data('speed'));
// Put together our final background position
var coords = '50% '+ yPos + 'px';
// Move the background
$bgobj.css({ backgroundPosition: coords });
});
});
});
如您所见,我已经尝试使用 Javascript/JQuery 修复它,但它不起作用,因为“header-title”在“header”标签内。
非常感谢您的建议。
【问题讨论】:
-
您应该避免使用 Javascript 计算尺寸。我不太了解您的问题,但是 background-size: cover; 呢? ?
-
你能解释一下你的问题是什么以及你想达到什么我不确定我是否完全理解?
-
@JimboJones 问题是我有 2 个 id 为“header”和“header-title”的 div。其中一个以图片为背景(背景大小:包含;)并具有视差效果。另一个有背景颜色,顶部透明度较高,底部透明度较低。在通常的分辨率下它们看起来不错,但如果我调整窗口大小并将其缩小,问题是“标题”的高度小于“标题标题”的高度。为了更好地理解,我向您展示了图片中的两种情况。 s14.directupload.net/images/140211/ez98lm2i.jpg
标签: jquery css html responsive-design parallax