Web Front-end
JS Tutorial
A method written in jquery to determine whether the div scroll bar has reached the bottom [recommended]_jquery
A method written in jquery to determine whether the div scroll bar has reached the bottom [recommended]_jquery
There are many concepts related to scroll bars in jQuery, but there are three properties related to the dragging of scroll bars, namely: scrollTop, scrollLeft, and scrollHeight. Among them, there are almost no application tips for the scrollHeight property found on the Internet, and I just need to use it.
We now only discuss the scrollTop and scrollHeight properties related to vertical scrolling.
1. Correct understanding of the relevant properties of the scroll bar:
Suppose there is the following Html code:
<div id="div1" style="overflow-y:auto; overflow-x:hidden; height:500px;"> <div style="height:750px;"> </div> </div>
Since the height of the inner p tag is longer than the outer one, and the outer p allows the vertical scroll bar to appear automatically, you can see the vertical scroll bar after opening it with a browser. Drag the scroll bar down a certain distance, and the page effect you see is as follows (a and b on the right are marked with PS after I captured the picture):
![1483924946895029.jpg A method written in jquery to determine whether the div scroll bar has reached the bottom [recommended]_jquery](https://img.php.cn//upload/image/495/464/122/1483924946895029.jpg)
So, what are the scrollTop and scrollHeight properties of the external p here?
Some people say that scrollTop is equal to the a marked in the picture. scrollHeight is equal to the height of the outer p, 500px. Actually, none of them are right.
In fact, a and b marked in the picture have no specific meaning for us to program and write js code. It only has symbolic meaning.
In fact, in js code, the scroll bar is abstracted as a "point". scrollHeight is actually not the "height of the scroll bar" (b), but represents the height that the scroll bar needs to scroll, that is, the height of the internal p is 750px. And scrollTop indicates how much the current position of the scroll bar (a point) occupies in 750px, not the a marked in the picture.
At this time, we were very impressed by the designers of Windows. The scroll bar design was so beautiful that it deceived many simple-minded mouse operators. The distances between a and b respectively identify the distance the scroll bar has been scrolled and the distance that needs to be scrolled. There is a corresponding relationship between them, but these are not considered by programmers like us who develop applications. They are programs that design the GUI graphical interface of the operating system. members considered.
2 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:
<!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" mce_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 750-500=250 distance, it will reach the bottom, see the statement nScrollTop + nDivHight >= nScrollHight.
In the program, detecting and executing if judgment statements 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 by clicking on the arrows at both ends of the scroll bar, the event will be 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 a method written in jquery to determine whether the div scroll bar reaches the bottom [Recommended]_jquery content. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
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
AI Hentai Generator
Generate AI Hentai for free.
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
1380
52
Detailed explanation of jQuery reference methods: Quick start guide
Feb 27, 2024 pm 06:45 PM
Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded
How to use PUT request method in jQuery?
Feb 28, 2024 pm 03:12 PM
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
In-depth analysis: jQuery's advantages and disadvantages
Feb 27, 2024 pm 05:18 PM
jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,
How to remove the height attribute of an element with jQuery?
Feb 28, 2024 am 08:39 AM
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
jQuery Tips: Quickly modify the text of all a tags on the page
Feb 28, 2024 pm 09:06 PM
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: <
Use jQuery to modify the text content of all a tags
Feb 28, 2024 pm 05:42 PM
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:
How to tell if a jQuery element has a specific attribute?
Feb 29, 2024 am 09:03 AM
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
Understand the role and application scenarios of eq in jQuery
Feb 28, 2024 pm 01:15 PM
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


