【发布时间】:2015-05-13 04:00:46
【问题描述】:
我的网站目前在一个滚动布局中包含三个部分。带有两个部分的标题:关于和联系人(这些是 div 框),当您滚动到页面底部时会显示动画。我想要实现的是当用户向下滚动并点击每个(div 框)部分的底部而不是网站底部时发生动画。
我认为我需要实现 .offset() 函数但不确定这是否正确?
任何帮助将不胜感激。
CSS
.header {
display: none;
position: relative;
left: 0px;
right: 0px;
top: 500px;
height: 80px;
width: 100%;
background:red;
color: #fff;
text-align: center;
}
JS
var header = $('.header'),
extra = 10; // In case you want to trigger it a bit sooner than exactly at the bottom.
header.css({ opacity: '0', display: 'block' });
$(window).scroll(function() {
var scrolledLength = ( $(window).height() +extra) + $(window).scrollTop(),
documentHeight = $(document).height();
console.log( 'Scroll length: ' + scrolledLength + ' Document height: ' + documentHeight )
if( scrolledLength >= documentHeight ) {
header
.addClass('top')
.stop().animate({ top: '20', opacity: '1' }, 800);
}
else if ( scrolledLength <= documentHeight && header.hasClass('top') ) {
header
.removeClass('top')
.stop().animate({ top: '500', opacity: '0' }, 800);
}
});
【问题讨论】:
-
由于 stackoverflow 社区说询问指向 github repo 的链接是否不起作用,我将在此处发布链接。这是否有效:github.com/FREE-FROM-CMS/load_more engine.js 顶部的变量之一可能是您需要使用的,但如果不是,您可以针对内容 div 执行鼠标悬停。
-
对不起,我不太确定我是否跟随.. 请原谅我,因为我只是新手,还在学习(慢慢哈)@php_purest
-
你在做什么类型的动画?
-
它做你想做的事了吗?
-
您将在我创建的 JS Fiddle 中看到我试图实现的动画。您会注意到它在用户滚动到文档底部后开始。 - 一旦用户滚动到名为“fillWindow”而不是整个文档的 div 框的底部,我试图让动画开始。-至于动画,名为“header”的 div 框沿 y 向上动画-axis 并从 0 淡入到 100%。然后一旦用户离开页面中的点,就会反转。@php_purest
标签: javascript jquery html css scroll