jquery determines whether the page scroll bar (scroll) is rolled up or down and scrolled to the head or bottom

Source: Internet
Author: User

Webtouch (WebApp) page, anti-Apple mobile Safari browser, online swipe, bottom navigation disappears, swipe to bottom and appear again. Swipe down and the bottom navigation appears.

Encounter problems

1, I started with Swipeup and Swipedown to do, found that because there are scroll bar, will not trigger. So you can only judge that the scroll bar is rolled up and down. About mobile phone gestures, the following article will be introduced, welcome attention!

2, by rolling down to let the bottom of the navigation display or so, Android above the browser can be, but Apple Safari will flash a flash, because Apple Safari automatically has a bounce effect, causing the event to repeat, the solution is to roll down a distance, scroll to a certain distance after the return is roll up or roll.

Simply judging the direction of the scroll bar

JS Code
  1. function Scroll (FN) {
  2. var beforescrolltop = Document.body.scrollTop,
  3. fn = fn | |  function () {};
  4. Window.addeventlistener ("scroll", function () {
  5. var afterscrolltop = Document.body.scrollTop,
  6. Delta = afterscrolltop-beforescrolltop;
  7. if (delta = = 0) return false;
  8. fn (Delta > 0?)  "Down ": "up");
  9. Beforescrolltop = Afterscrolltop;
  10. }, false);
  11. }

Call Method:

JS Code
    1. Scroll (function (direction) {Console.log (direction)});

Above method phone Apple browser event will bounce, workaround and code to improve JS code
  1. Scrolldirect: function (fn) {
  2. var beforescrolltop = Document.body.scrollTop;
  3. fn = fn | | function () {
  4. };
  5. Window.addeventlistener ("scroll", function (event) {
  6. Event = Event | | window.event;
  7. var afterscrolltop = Document.body.scrollTop;
  8. Delta = afterscrolltop-beforescrolltop;
  9. Beforescrolltop = Afterscrolltop;
  10. var scrolltop = $ (this). scrolltop ();
  11. var scrollheight = $ (document). Height ();
  12. var windowheight = $ (this). Height ();
  13. if (scrolltop + windowheight > scrollHeight-10) { //scroll to bottom execution event
  14. fn (' up ');
  15. return;
  16. }
  17. if (Afterscrolltop < | | afterscrolltop > $ (document.body). height-10) {
  18. fn (' up ');
  19. } Else {
  20. if (Math.Abs (delta) <) {
  21. return false;
  22. }
  23. fn (Delta > 0?)  "Down ": "up");
  24. }
  25. }, false);
  26. }

Call Method:

JS Code
  1. var upflag=1;
  2. var downflag= 1;
  3. //scroll Slide, slide up and slide only once!
  4. Crolldirect (function (direction) {
  5. if (Direction = = "Down") {
  6. if (downflag) {
  7. $ (". Footer_wrap"). Slideup (200);
  8. Downflag = 0;
  9. Upflag = 1;
  10. }
  11. }
  12. if (Direction = = "Up") {
  13. if (upflag) {
  14. $ (". Footer_wrap"). Slidedown (200);
  15. Downflag = 1;
  16. Upflag = 0;
  17. }
  18. }
  19. });
Scroll bar Scroll to the bottom and head to judge

In fact, I have the above function has been judged, the following list! Look at the following function!

JS Code
  1. Bottomjumppage: function () {
  2. var scrolltop = $ (this). scrolltop ();
  3. var scrollheight = $ (document). Height ();
  4. var windowheight = $ (this). Height ();
  5. if (scrolltop + windowheight = = scrollheight) { //scroll to bottom execution event
  6. Console.dir ("I went to the bottom");
  7. }
  8. if (scrolltop = = 0) { //scroll to the head of the execution event
  9. Console.dir ("I went to the head");
  10. }
  11. }

Call Method:

JS Code

$ (window). Scroll (bottomjumppage);

jquery determines whether the page scroll bar (scroll) is rolled up or down and scrolled to the head or bottom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.