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

前端之家收集整理的这篇文章主要介绍了javascript – 检测故意的顶部和底部额外滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用 javascript检测一个有意的额外顶部/底部滚动. $(window).scrollTop()和window.pageYOffset没用,因为它们停在Top 0,我想达到类似top -X的东西.对于底部,假设我的文档高度为500,底部就像底部5XX.

编辑:
示例代码可以是:

  1. $(window).scroll(function(){
  2.  
  3. if(intentionalScrollTop){
  4. // Do something
  5. }else if(intentionalScrollDown){
  6. // Do something
  7. }
  8.  
  9. });

Gif例子:

解决方法

我从你的问题中理解的不仅是“检测到过度滚动”……
但也可以用它来创建你在问题中显示的动画.

我使用包装div做了一个解决方案,就像我2天前评论的那样.

您可以在CodePen或下面的代码段中看到它.

  1. $(document).ready(function(){
  2. var at_Top=true;
  3. var at_Bottom=false;
  4. var prevIoUs_scrolled;
  5. var triggerTime=0;
  6. var scroll_dir=false; // false=>Down true=>up
  7. var scrolled_bottom = $("body").height() - $(window).height();
  8. var animationDelay = 300;
  9. var animationTimeout = 350; //Max delay between 2 triggers is 1 sec.
  10. //So keep this value under 400ms
  11. //Because one complete animation is 300ms+350ms+300ms.
  12. //To have longer animation delays,add time to the triggerDelay
  13. var triggerDelay=0; // You can add more delay to allow the next trigger (in seconds).
  14. $(window).scroll(function(){
  15. var scrolled=$(window).scrollTop();
  16.  
  17. // Reached the top?
  18. if(scrolled==0){
  19. at_Top=true;
  20. }else{
  21. at_Top=false;
  22. }
  23.  
  24. // Reached the bottom?
  25. if(scrolled==scrolled_bottom){
  26. at_Bottom=true;
  27. }else{
  28. at_Bottom=false;
  29. }
  30. // Scroll direction
  31. if( $(this).scrollTop() > prevIoUs_scrolled ){
  32. scroll_dir=false; //scroll down
  33. }else{
  34. scroll_dir=true; //scroll up
  35. }
  36. // Keep prevIoUs scrollTop position in memory
  37. prevIoUs_scrolled = $(this).scrollTop();
  38. animationTrigger();
  39. });
  40.  
  41. function animationTrigger(){
  42. if(at_Top && scroll_dir){
  43. console.log("Scrolling when at top.");
  44. $("#wrapper").stop().animate({"margin-top":"3em"},animationDelay);
  45. setTimeout(function(){
  46. $("#wrapper").stop().animate({"margin-top":0},animationDelay);
  47. },animationTimeout);
  48. clearTimeout(clearConsole);
  49. var clearConsole = setTimeout(function(){
  50. console.clear();
  51. },3000);
  52. }
  53. if(at_Bottom && !scroll_dir){
  54. console.log("Scrolling when at bottom.")
  55. $("#header").stop().animate({"height":0},animationDelay);
  56. $("#footer-spacer").stop().animate({"height":"3em"},animationDelay);
  57. setTimeout(function(){
  58. $("#header").stop().animate({"height":"3em"},animationDelay);
  59. $("#footer-spacer").stop().animate({"height":0},3000);
  60. }
  61. }
  62. // KEYBOARD ARROWS UP/DOWN AND PAGE UP/DOWN SCROLLING
  63. $(window).on("keydown",function(e){
  64. //console.log(e.which);
  65. if( (e.which==38) || (e.which==33) ){ // Arrow up or Page up
  66. scroll_dir=true;
  67. }
  68. if( (e.which==40) || (e.which==34) ){ // Arrow down or Page down
  69. scroll_dir=false;
  70. }
  71. // Limit triggers to 1 per second... Because when holding a key down for long,it triggers too fast...
  72. var thisSecond = new Date().getSeconds()
  73. if( (triggerTime != thisSecond) || (triggerTime < (thisSecond - triggerDelay) ) ){
  74. animationTrigger();
  75. triggerTime=thisSecond;
  76. }
  77. })
  78.  
  79. // WHEEL SCROLLING
  80. // Inspired from this SO answer: https://stackoverflow.com/a/7309786/2159528
  81.  
  82. //Firefox
  83. $(window).bind('DOMMouseScroll',function(e){
  84. var scrolled2=$(window).scrollTop();
  85. if(e.originalEvent.detail > 0) {
  86. scroll_dir=false; //scroll down
  87. //console.log("down");
  88. }else {
  89. scroll_dir=true; //scroll up
  90. //console.log("up");
  91. }
  92. // Limit triggers to 1 per second... Because wheel turns quite fast.
  93. var thisSecond = new Date().getSeconds()
  94. if( (triggerTime != thisSecond) || (triggerTime < (thisSecond - triggerDelay) ) ){
  95. animationTrigger();
  96. triggerTime=thisSecond;
  97. }
  98. });
  99.  
  100. //IE,Opera,Safari
  101. $(window).bind('mousewheel',function(e){
  102. if(e.originalEvent.wheelDelta < 0) {
  103. scroll_dir=false; //scroll down
  104. }else {
  105. scroll_dir=true; //scroll up
  106. }
  107. // Limit triggers to 1 per second... Because wheel turns quite fast.
  108. var thisSecond = new Date().getSeconds()
  109. if( (triggerTime != thisSecond) || (triggerTime < (thisSecond - triggerDelay) ) ){
  110. animationTrigger();
  111. triggerTime=thisSecond;
  112. }
  113. });
  114.  
  115. }); // End Document.ready
  1. body,wrapper{
  2. padding:0;
  3. margin:0;
  4. height:100;
  5. }
  6. #page{
  7. height:1500px;
  8. width:100%;
  9. }
  10. #header,#footer{
  11. height:3em;
  12. padding:0.5em;
  13. background-color:cyan;
  14. }
  15. #content{
  16. height:calc(100% - 8em); /* -8em for header and footer... (height: 3em + padding: 2x 0,5em) */
  17. padding:0 0.5em;
  18. overflow:hidden;
  19. }
  20. #footer-spacer{
  21. height:0px;
  22. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <div id="wrapper">
  3. <div id="page">
  4. <div id="header">
  5. This is the page's top
  6. </div>
  7. <div id="content">
  8. <h1>Scroll this page to see the overscroll effect at top and bottom</h1>
  9. <br>
  10. <ul>
  11. <li>using the mouse wheel</li>
  12. <li>keyboard arrows</li>
  13. <li>keyboard page up/down</li>
  14. </ul>
  15. <br>
  16. <br>
  17. Content...<br>
  18. Content...<br>
  19. Content...<br>
  20. Content...<br>
  21. Content...<br>
  22. Content...<br>
  23. Content...<br>
  24. Content...<br>
  25. Content...<br>
  26. Content...<br>
  27. Content...<br>
  28. Content...<br>
  29. Content...<br>
  30. Content...<br>
  31. Content...<br>
  32. Content...<br>
  33. Content...<br>
  34. Content...<br>
  35. Content...<br>
  36. Content...<br>
  37. Content...<br>
  38. Content...<br>
  39. Content...<br>
  40. Content...<br>
  41. Content...<br>
  42. Content...<br>
  43. Content...<br>
  44. Content...<br>
  45. Content...<br>
  46. Content...<br>
  47. Content...<br>
  48. Content...<br>
  49. Content...<br>
  50. Content...<br>
  51. Content...<br>
  52. Content...<br>
  53. Content...<br>
  54. Content...<br>
  55. Content...<br>
  56. Content...<br>
  57. Content...<br>
  58. Content...<br>
  59. Content...<br>
  60. Content...<br>
  61. Content...<br>
  62. Content...<br>
  63. Content...<br>
  64. Content...<br>
  65. Content...<br>
  66. Content...<br>
  67. Content...<br>
  68. Content...<br>
  69. Content...<br>
  70. Content...<br>
  71. Content...<br>
  72. Content...<br>
  73. Content...<br>
  74. Content...<br>
  75. Content...<br>
  76. Content...<br>
  77. Content...<br>
  78. Content...<br>
  79. Content...<br>
  80. Content...<br>
  81. Content...<br>
  82. Content...<br>
  83. Content...<br>
  84. Content...<br>
  85. Content...<br>
  86. Content...<br>
  87. Content...<br>
  88. Content...<br>
  89. Content...<br>
  90. Content...<br>
  91. Content...<br>
  92. Content...<br>
  93. Content...<br>
  94. Content...<br>
  95. Content...<br>
  96. Content...<br>
  97. Content...<br>
  98. Content...<br>
  99. Content...<br>
  100. Content...<br>
  101. Content...<br>
  102. Content...<br>
  103. Content...<br>
  104. Content...<br>
  105. Content...<br>
  106. Content...<br>
  107. </div>
  108. <div id="footer">
  109. This is the page's bottom
  110. </div>
  111. <div id="footer-spacer"></div>
  112. </div>
  113. </div>

页面位于顶部或底部时检测滚动尝试…
用它来设置你展示的动画是另一个.

顶部的方法
包装器的边距从0em到3em动画,将标题内容向下推.
因此页面“看起来”像过度滚动.

底部方法
这是一个挑战……
不能与包装器的margin-bottom顶部相同,因为它可以工作……但是在视口下面,这不是我们想要的.

所以在这种情况下,我将“内容”定义为height:calc(100% – 8em)(页眉和页脚都有高度:3em和填充:0.5em)只是为了确保包装器100%填充.动画的空div在页脚下…当它的高度从0em到3em时,它通过向上推动页脚来创建过度滚动“幻觉”.

请注意,标题会在同一时间缩回,以释放空间.此时,标题不可见,为什么不呢?

拖动滚动条,旋转鼠标滚轮并按下键盘上4个“常用”键中的1个(箭头和向上/向下翻页)时,此脚本可以正常工作.

我留下了很多console.log(),您可以使用它来探索它是如何工作的,并改进动画并使其成为您的品味.

猜你在找的JavaScript相关文章