gpt4 book ai didi

javascript - 检测故意的顶部和底部额外滚动

转载 作者:数据小太阳 更新时间:2023-10-29 04:49:07 25 4
gpt4 key购买 nike

我正在尝试使用 javascript 检测有意的额外顶部/底部滚动。 $(window).scrollTop()window.pageYOffset 没有用,因为它们停在 Top 0 我想达到类似顶部-X。对于底部,假设我的文档的高度为 500,底部将类似于 bottom 5XX

编辑:示例代码可以是:

$(window).scroll(function(){

if(intentionalScrollTop){
// Do something
}else if(intentionalScrollDown){
// Do something
}

});

动图示例: enter image description here

最佳答案

我从你的问题中了解到的不仅仅是“检测过度滚动”......
但也可以使用它来创建动画,就像您在问题中展示的那样。

正如我 2 天前评论的那样,我使用包装器 div 制作了一个解决方案。

你可以在CodePen中看到它或下面的代码片段。

$(document).ready(function(){

var at_Top=true;
var at_Bottom=false;
var previous_scrolled;
var triggerTime=0;
var scroll_dir=false; // false=>Down true=>up
var scrolled_bottom = $("body").height() - $(window).height();
var animationDelay = 300;
var animationTimeout = 350; //Max delay between 2 triggers is 1 sec.
//So keep this value under 400ms
//Because one complete animation is 300ms+350ms+300ms.
//To have longer animation delays, add time to the triggerDelay

var triggerDelay=0; // You can add more delay to allow the next trigger (in seconds).

$(window).scroll(function(){
var scrolled=$(window).scrollTop();

// Reached the top?
if(scrolled==0){
at_Top=true;
}else{
at_Top=false;
}

// Reached the bottom?
if(scrolled==scrolled_bottom){
at_Bottom=true;
}else{
at_Bottom=false;
}

// Scroll direction
if( $(this).scrollTop() > previous_scrolled ){
scroll_dir=false; //scroll down
}else{
scroll_dir=true; //scroll up
}

// Keep previous scrollTop position in memory
previous_scrolled = $(this).scrollTop();

animationTrigger();
});

function animationTrigger(){
if(at_Top && scroll_dir){
console.log("Scrolling when at top.");
$("#wrapper").stop().animate({"margin-top":"3em"},animationDelay);
setTimeout(function(){
$("#wrapper").stop().animate({"margin-top":0},animationDelay);
},animationTimeout);
clearTimeout(clearConsole);
var clearConsole = setTimeout(function(){
console.clear();
},3000);
}
if(at_Bottom && !scroll_dir){
console.log("Scrolling when at bottom.")
$("#header").stop().animate({"height":0},animationDelay);
$("#footer-spacer").stop().animate({"height":"3em"},animationDelay);
setTimeout(function(){
$("#header").stop().animate({"height":"3em"},animationDelay);
$("#footer-spacer").stop().animate({"height":0},animationDelay);
},animationTimeout);
clearTimeout(clearConsole);
var clearConsole = setTimeout(function(){
console.clear();
},3000);
}
}

// KEYBOARD ARROWS UP/DOWN AND PAGE UP/DOWN SCROLLING
$(window).on("keydown",function(e){
//console.log(e.which);
if( (e.which==38) || (e.which==33) ){ // Arrow up or Page up
scroll_dir=true;
}
if( (e.which==40) || (e.which==34) ){ // Arrow down or Page down
scroll_dir=false;
}

// Limit triggers to 1 per second... Because when holding a key down for long, it triggers too fast...
var thisSecond = new Date().getSeconds()
if( (triggerTime != thisSecond) || (triggerTime < (thisSecond - triggerDelay) ) ){
animationTrigger();
triggerTime=thisSecond;
}
})

// WHEEL SCROLLING
// Inspired from this SO answer: http://stackoverflow.com/a/7309786/2159528

//Firefox
$(window).bind('DOMMouseScroll', function(e){
var scrolled2=$(window).scrollTop();

if(e.originalEvent.detail > 0) {
scroll_dir=false; //scroll down
//console.log("down");
}else {
scroll_dir=true; //scroll up
//console.log("up");
}

// Limit triggers to 1 per second... Because wheel turns quite fast.
var thisSecond = new Date().getSeconds()
if( (triggerTime != thisSecond) || (triggerTime < (thisSecond - triggerDelay) ) ){
animationTrigger();
triggerTime=thisSecond;
}
});

//IE, Opera, Safari
$(window).bind('mousewheel', function(e){

if(e.originalEvent.wheelDelta < 0) {
scroll_dir=false; //scroll down
}else {
scroll_dir=true; //scroll up
}

// Limit triggers to 1 per second... Because wheel turns quite fast.
var thisSecond = new Date().getSeconds()
if( (triggerTime != thisSecond) || (triggerTime < (thisSecond - triggerDelay) ) ){
animationTrigger();
triggerTime=thisSecond;
}
});

}); // End Document.ready
body,wrapper{
padding:0;
margin:0;
height:100;
}
#page{
height:1500px;
width:100%;
}
#header,#footer{
height:3em;
padding:0.5em;
background-color:cyan;
}
#content{
height:calc(100% - 8em); /* -8em for header and footer... (height: 3em + padding: 2x 0,5em) */
padding:0 0.5em;
overflow:hidden;
}
#footer-spacer{
height:0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<div id="page">
<div id="header">
This is the page's top
</div>
<div id="content">
<h1>Scroll this page to see the overscroll effect at top and bottom</h1>
<br>
<ul>
<li>using the mouse wheel</li>
<li>keyboard arrows</li>
<li>keyboard page up/down</li>
</ul>
<br>
<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
Content...<br>
</div>

<div id="footer">
This is the page's bottom
</div>
<div id="footer-spacer"></div>
</div>
</div>

要在页面位于顶部或底部时检测滚动尝试...
使用它来设置您展示的动画是另一回事。

上层方法:
包装器的 margin0em 动画到 3em,将标题和内容向下推。
所以页面“看起来”像是过度滚动了。

底部方法:
这是一个挑战...
不能使用包装器的 margin-bottom 对顶部做同样的事情,因为它有效……但在视口(viewport)下方,这并不是我们真正想要的。

所以在这种情况下,我将“内容”定义为 height:calc(100% - 8em) (页眉和页脚都有 height:3empadding:0.5em) 只是为了确保包装器 100% 填充。要设置动画的空 div 位于页脚 下方...当其高度从 0em 变为 3em 时,它会创建滚动“错觉”将页脚向上推。

请注意,标题同时缩回,以便释放空间。此时, header 不可见,为什么不呢?

当拖动滚动条、旋转鼠标滚轮并按下键盘上4个“常用”键中的1个(箭头和上/下翻页)。

我留下了很多console.log(),你可以用它们来探索它是如何工作的,改进动画,让它成为你的品味。
;)

关于javascript - 检测故意的顶部和底部额外滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40134744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com