【发布时间】:2015-05-07 22:07:25
【问题描述】:
我正在为客户开发一个网站,但我遇到了一些对我来说非常具有挑战性的事情。除了显示我到目前为止所做的代码笔之外,我还包括一个视频链接,显示我正在尝试完成的效果。
我发现了一个插件,它可以让标题保持粘性,直到用户滚动到某个点为止,但我最大的挑战是垂直线的效果。我尝试过的东西会强制线条随着内容向上滚动,并且随着 div 变小,线条应该会变小,如视频所示。有没有一种优雅的方法来创建这种效果而无需大量丑陋的 javascript 编码?
为澄清而编辑:该视频展示了一个线框概念。网站的每个部分都会有背景图像,它将使用 background-attachment:fixed 来创建视差效果。背景图像也会缩放以适应屏幕的全宽/高度。
视频链接:https://www.youtube.com/watch?v=kX_MHb8h-r8&feature=youtu.be
$(function() {
return $("[data-sticky_column]").stick_in_parent({
parent: "[data-sticky_parent]"
});
});
$(window).on("resize", (function(_this) {
return function(e) {
return $(document.body).trigger("sticky_kit:recalc");
};
})(this));
h1 {
margin: 0 0 0 0;
color: #ff0000;
}
.title-container {
padding-top: 30px;
padding-bottom: 30px;
}
.fixedbkg {
width: 100%;
height: 100vh;
display: block;
}
.container {
width: 100%;
display: block;
}
.bg1 {
background-color: #acacac;
background-attachment: fixed;
}
.bg2 {
background-color: #a0a0a0;
background-attachment: fixed;
}
.bg3 {
background-color: #595959;
background-attachment: fixed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="bg1 fixedbkg" data-sticky_parent>
<div class="title-container" data-sticky_column>
<h1>Title 1</h1>
</div>
</div>
<div class="bg2 fixedbkg" data-sticky_parent>
<div class="title-container" data-sticky_column>
<h1>Title 2</h1>
</div>
</div>
<div class="bg3 fixedbkg" data-sticky_parent>
<div class="title-container" data-sticky_column>
<h1>Title 3</h1>
</div>
</div>
</div>
CodePen 链接:http://codepen.io/anon/pen/waMwxP
【问题讨论】:
-
如果背景是纯色的,就像视频中那样,那么你可以给第二个div加上一个粗的border-top,用第一个div的背景颜色,这样它就可以隐藏线条第一个 div 在第二个 div 内容顶部上方一定距离。
-
谢谢,但它实际上是一个图像,图像大小将是全屏。