


jQuery determines whether the scroll bar has scrolled to the bottom of the page script
In fact, it is very simple. We only need to use the three parameters of clientHeight, offsetHeight, and scrollTop to determine our current position. Let me introduce an example to you.
Example, judge to the bottom
The code is as follows
$(window).scroll(function () { if ($(document).scrollTop() + $(window).height() >= $(document).height()) { alert("哦哦,到底了."); } });
If you want to pull to the bottomautomatically load the content. Just register a scroll bar event:
First, we pull the scroll bar from the top to the bottom. What changes is the value of scrollTop, and this value has a range.
This interval is: [0, (offsetHeight - clientHeight)]
That is, the change in the entire process of scroll bar pulling is within the range of 0 to (offsetHeight - clientHeight).
1. Determine whether the scroll bar has scrolled to the bottom: scrollTop == (offsetHeight – clientHeight)
2. When the scroll bar is within 50px from the bottom: (offsetHeight – clientHeight) – scrollTop <= 50
3. Within 5% of the bottom of the scroll bar: scrollTop / (offsetHeight – clientHeight) >= 0.95
The code is as follows
scrollBottomTest =function(){ $("#contain").scroll(function(){ var $this =$(this), viewH =$(this).height(),//可见高度 contentH =$(this).get(0).scrollHeight,//内容高度 scrollTop =$(this).scrollTop();//滚动高度 //if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容 if(scrollTop/(contentH -viewH)>=0.95){ //到达底部100px时,加载新内容 // 这里加载数据.. } }); }
js judgment
Determine whether the vertical scroll bar has reached the bottom
After clarifying the above concepts, the coding is actually relatively simple. The following is a sample code:
The code is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>下拉滚动条滚到底部了吗?</title> <script language="javascript" src="jquery-1.4.2.min.js"></script> <script language="javascript"> $(document).ready(function (){ var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度) var nScrollTop = 0; //滚动到的当前位置 var nDivHight = $("#div1").height(); $("#div1").scroll(function(){ nScrollHight = $(this)[0].scrollHeight; nScrollTop = $(this)[0].scrollTop; if(nScrollTop + nDivHight >= nScrollHight) alert("滚动条到底部了"); }); }); </script> <body> <div id="div1" style="overflow-y:auto; overflow-x:hidden; height:500px;"> <div style="background-color:#ccc; height:750px;">IE 和 FF 下测试通过</div> </div> </body> </html>
Code explanation:
The height of the inner div is 750 and the height of the outer div is 500, so the vertical scroll bar needs to scroll a distance of 750-500=250 to reach the bottom. See the statement nScrollTop + nDivHight >= nScrollHight.
In the program, detecting and executing the ifjudgment statement in the scroll event of the external div consumes a lot of CPU resources. Use the mouse to drag the scroll bar, and this event will be triggered as long as there is a change of one pixel. But when you click the arrows at both ends of the scroll bar, the event is triggered much less frequently. Therefore, the scroll event of the scroll bar should be used with caution.
This example judges the situation when there is no horizontal scroll bar. When there is a horizontal scroll bar, the situation will change slightly, so in the nScrollTop + nDivHight >= nScrollHight statement, you need to use ">=" Comparison operator , and when there is no horizontal scroll bar, the equal sign "=" is sufficient. You can actually test it. You can also determine whether the horizontal scroll bar has scrolled to the end.
The above is the detailed content of jQuery determines whether the scroll bar has scrolled to the bottom of the page script. 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: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

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:

Title: Introduction to how to delete a page of content in Word When editing a document using Microsoft Word, you may sometimes encounter a situation where you need to delete the content of a certain page. You may want to delete a blank page or unnecessary content on a certain page in the document. In response to this situation, we can take some methods to quickly and effectively delete a page of content. Next, some methods to delete a page of content in Microsoft Word will be introduced. Method 1: Delete a page of content First, open the Word document that needs to be edited. Certainly

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
