


How to use jquery to determine the scroll bar scrolling direction example code analysis
Simply determine the direction of the scroll bar
function scroll( fn ) { var beforeScrollTop = document.body.scrollTop, fn = fn || function() {}; window.addEventListener("scroll", function() { var afterScrollTop = document.body.scrollTop, delta = afterScrollTop - beforeScrollTop; if( delta === 0 ) return false; fn( delta > 0 ? "down" : "up" ); beforeScrollTop = afterScrollTop; }, false); }
Calling method:
scroll(function(direction) { console.log(direction) });
The above method will cause mobile phone Apple browser events to jump, solutions and code improvements
##
scrollDirect: function (fn) { var beforeScrollTop = document.body.scrollTop; fn = fn || function () { }; window.addEventListener("scroll", function (event) { event = event || window.event; var afterScrollTop = document.body.scrollTop; delta = afterScrollTop - beforeScrollTop; beforeScrollTop = afterScrollTop; var scrollTop = $(this).scrollTop(); var scrollHeight = $(document).height(); var windowHeight = $(this).height(); if (scrollTop + windowHeight > scrollHeight - 10) { //滚动到底部执行事件 fn('up'); return; } if (afterScrollTop < 10 || afterScrollTop > $(document.body).height - 10) { fn('up'); } else { if (Math.abs(delta) < 10) { return false; } fn(delta > 0 ? "down" : "up"); } }, false); }
var upflag=1; var downflag= 1; //scroll滑动,上滑和下滑只执行一次! scrollDirect(function (direction) { if (direction == "down") { if (downflag) { $(".footer_wrap").slideUp(200); downflag = 0; upflag = 1; } } if (direction == "up") { if (upflag) { $(".footer_wrap").slideDown(200); downflag = 1; upflag = 0; } } });
Scroll bar scrolls to the bottom and head judgment
In fact, there are already judgments in the above function, let’s list them again below! Look at the following function!BottomJumpPage: function () { var scrollTop = $(this).scrollTop(); var scrollHeight = $(document).height(); var windowHeight = $(this).height(); if (scrollTop + windowHeight == scrollHeight) { //滚动到底部执行事件 console.dir("我到底部了"); } if (scrollTop == 0) { //滚动到头部部执行事件 console.dir("我到头部了"); } }
$(window).scroll(BottomJumpPage);
Determine whether p scrolls to the bottom
The above method is to determine whether the scroll axis has scrolled to the bottom, but sometimes, when we are doing scroll loading, sometimes we also need to check whether a certain p has scrolled to the bottom and then loaded. So how to judge that the scroll axis of p has scrolled to the bottom? You also need to know several heights: 1. The height of p$("#a certain p").height();2. The height of the scroll axis$(# A certain p)[0].scrollHeight
3. The height from the scroll axis to the top of p$(a certain p)[0].scrollTop;
Write the code as follows:
$(document).ready(function (){ var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度) var nScrollTop = 0; //滚动到的当前位置 var npHight = $("#p1").height(); $("#p1").scroll(function(){ nScrollHight = $(this)[0].scrollHeight; nScrollTop = $(this)[0].scrollTop; if(nScrollTop + npHight >= nScrollHight) alert("滚动条到底部了"); }); });
PS: jQuery scroll bar position control:
Scroll the scroll bar of p to the position of its child elements to facilitate automatic positioning.var container = $('p'), scrollTo = $('#row_8'); container.scrollTop( scrollTo.offset().top - container.offset().top + container.scrollTop() ); // Or you can animate the scrolling: container.animate({ scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop() });?
The above is the detailed content of How to use jquery to determine the scroll bar scrolling direction example code analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Question: How to determine whether the date is the previous day in Go language? In daily development, we often encounter situations where we need to determine whether the date is the previous day. In the Go language, we can implement this function through time calculation. The following will be combined with specific code examples to demonstrate how to determine whether the date is the previous day in Go language. First, we need to import the time package in the Go language. The code is as follows: import("time") Then, we define a function IsYest

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

PHP is a widely used server-side scripting language that is flexible, powerful, and easy to learn. In the process of using PHP for string processing, it is a common requirement to determine whether a string contains Chinese characters and numbers. This article will introduce how to implement a simple function of determining whether a string contains Chinese characters and numbers in PHP, with specific code examples. First, let's take a look at how to determine whether a string contains Chinese characters. The range of Chinese character encoding is: [x{4e00}-x{9fa5}].
