


Detailed explanation of jQuery determining whether the web page has scrolled to the bottom of the browser
In some requirements, new content needs to be loaded when the user scrolls to the bottom of the browser. The author here introduces how to use Jquery to determine whether the user has browsed to the bottom of the web page. Hope it helps everyone.
Before understanding the following knowledge points, the author will introduce a few concepts here.
$(window).height(); //Used to get the height of the browser display area
$(window).width(); //Used to get the browser display area The width of
$(document.body).height(); //Get the height of the page document
$(document.body).width(); //Get the width of the page document
$(document).scrollTop(); //Get the vertical distance from the vertical scroll bar to the top
$(document).scrollLeft(); //Get the horizontal scroll bar to the left Horizontal distance
Through the above knowledge points, you can know: the height of the browser display area + the distance from the top of the vertical scroll bar
With this conclusion, it will be easy to implement. The following code is implemented to determine whether the user has browsed to the bottom of the web page.
$(window).scroll(function(){ var h=$(document.body).height();//网页文档的高度 var c = $(document).scrollTop();//滚动条距离网页顶部的高度 var wh = $(window).height(); //页面可视化区域高度 if (Math.ceil(wh+c)>=h){ alert("我已经到底部啦"); } })
If you need to determine whether the user has browsed to a certain element, then you only need to change the height of the web page document above to the distance between a certain element and the top of the web page. That's it. For example:
$(window).scroll(function(){ var h=$("#p").offset().top;//id为p的元素距离网页顶部的距离 var c = $(document).scrollTop();//滚动条距离网页顶部的高度 var wh = $(window).height(); //页面可视化区域高度 if (Math.ceil(wh+c)>=h){ alert("我已经到底部啦"); } })
Readers need to note that in the judgment condition, wh+c is the smallest integer that is greater than or equal to this number. After the author's testing, there is no problem on IE7, 8, 9, and 11.
Next, the author encapsulates the above code into a plug-in.
(function ($) { //backcall是回调函数,count表示回调函数被执行的次数,count只有在元素通过滚动条滑出的时候才起作用 $.fn.inBottom = function (backcall){ //判断当前元素是否在目前屏幕可视化区域之内 if(this.offset().top >= $(window).height()){ this.inScroll(backcall,count); }else{ this.inWindow(backcall); } }; //直接加载回调函数 $.fn.inWindow = function (backcall){ backcall(); }; //滚动监听滑动条,元素滚动到屏幕底部的时候,加载回调函数 $.fn.inScroll = function (backcall,count) { var $this=this; $(window).scroll(function(){ //获得这个元素到文档顶部的距离 var awayDocTop=$this.offset().top; //获得滚动条的top var sTop=$(document).scrollTop(); //获得可视化窗口的高度 var wh=$(window).height(); if(Math.ceil(wh+sTop)>=awayDocTop){ if(count>0){ backcall(); count--; } }; }); }; })(jQuery);
Then after readers introduce the above plug-in file, they can call it through code similar to the following.
$("#p").inBottom(function(){ alert("我被回调了"); },1);
Related recommendations:
css control div fixed at the bottom of the browser example code
How to implement a fixed and floating DIV at the bottom of the browser?_html/css_WEB-ITnose
##How to implement a fixed and floating DIV at the bottom of the browser (using only css)? _html/css_WEB-ITnose
The above is the detailed content of Detailed explanation of jQuery determining whether the web page has scrolled to the bottom of the browser. For more information, please follow other related articles on the PHP Chinese website!

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


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

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
