所以,我花了一些时间在 stackoverflow 上找到了这个很好的答案,以更改滚动时的不透明度,并将其与有条件的类删除/添加相结合:
jsfiddle demo
超级有用的 stackoverflow 线程,带有指向 jsfiddle 的链接,用于更改滚动时的不透明度:
Change div opacity on scroll
就使图像看起来“更小”的效果而言,这实际上是一种错觉。图像的 div 溢出是隐藏的,因此在滚动事件中,javascript 会调整上边距,将其“向下推”。这实际上将图像推到其下方的 下。当我有更多时间时,我会看看我是否也可以将其纳入答案。干杯
具有所需效果的页面:
http://www.envisionboston.com/
代码块由上面提到的 Stack 链接提供:
function fader() {
var b = $('.blue'),
wh = $(window).height(),
dt = $(document).scrollTop(),
elView, opacity;
h = $("#headerArea").height();
b.each(function () {
elView = wh - ($(this).offset().top - dt);
if (window.pageYOffset > (h + 50)) {
$("#headerArea").removeClass("withHeader").addClass("withoutHeader");
} else {
$("#headerArea").removeClass("withoutHeader").addClass("withHeader");
}
if (elView > 0) { // Top of DIV above bottom of window.
opacity = 1 - 1 / (wh + $(this).height()) * elView;
if (opacity > 0) // Bottom of DIV below top of window.
$(this).css('opacity', opacity);
}
});
}
// Event on scroll
$(document).bind('scroll', fader);
原答案:
根据您列出的“site.js”文件的链接,这是实现这一点的代码:
查看 HTML 文件,您会发现被引用的类“.main-image-wrapper”
代码检查是否存在所需的类
if (Y.one('.main-image-wrapper'))
然后说如果页面当前垂直位置大于主图高度+80像素,则添加一个隐藏页眉的类
if ( (window.pageYOffset > (this.mainImageHeight + 80)) ) {
Y.one('body').addClass('header-hidden');
类被上面记录的条件的 else 删除。您还会注意到,在这个 else 条件下,标题高度也发生了变化。
else {
Y.one('body').removeClass('header-hidden');
this.headerHeight = header.get('offsetHeight');
}
有很多样式和其他细微之处正在发生,但基本上:检查您的 y 位置在页面上的位置,如果这大于您的图像高度 + 所需像素数(在这种情况下为 80),然后添加一个类隐藏标题;否则,显示它。
下面的完整代码块供您参考。
if (Y.one('html.no-window-orientation')) {
var scrollStates = function () {
if (Y.one('.main-image-wrapper')) {
// 80 is main content padding
if ( (window.pageYOffset > (this.mainImageHeight + 80)) ) {
Y.one('body').addClass('header-hidden');
this.headerHeight = header.get('offsetHeight');
if (Y.one('.contact-inner-wrapper')) {
Y.all('.contact-inner-wrapper').setStyle('marginTop', (this.headerHeight + 5));
}
} else {
Y.one('body').removeClass('header-hidden');
this.headerHeight = header.get('offsetHeight');
if (Y.one('.contact-inner-wrapper')) {
Y.all('.contact-inner-wrapper').setStyle('marginTop', (this.headerHeight + 5));
}
}
Y.one('.main-image-wrapper').setStyle('opacity', (1 - (window.pageYOffset / parseInt(Y.Squarespace.Template.getTweakValue('bannerImageHeight'), 10))));
Y.one('.main-image-wrapper').setStyle('top', -(window.pageYOffset / 3));
} else {
if ( (window.pageYOffset >= 80) ) {
Y.one('body').addClass('header-hidden');
this.headerHeight = header.get('offsetHeight');
if (Y.one('.contact-inner-wrapper')) {
Y.all('.contact-inner-wrapper').setStyle('marginTop', (this.headerHeight + 5));
}
} else {
Y.one('body').removeClass('header-hidden');
this.headerHeight = header.get('offsetHeight');
if (Y.one('.contact-inner-wrapper')) {
Y.all('.contact-inner-wrapper').setStyle('marginTop', (this.headerHeight + 5));
}
}
}
};
scrollStates();
Y.one(window).on('scroll', function() {
scrollStates();
});
}