When a front-end page of a project shows a purchased item, it needs to be able to pull the load more. It took a little time to study the function, which had not been done before.
First, we need to add scroll event to the DIV and listen to the scroll bar scrolling action. When does that trigger the load action? When the scroll bar rolls to the end. How to determine the scroll bar roll to the end?
When the height of the scrollbar plus the height of the scroll bar to the top of the div equals the scrollable height of the div, the scroll bar is at the bottom. The formula is as follows.
[JavaScript]View PlainCopy
- This.scrollheight <= $ (this). ScrollTop () + $ (this). Height ()
Give a demo that can run and need to introduce jquery.
[JavaScript]View PlainCopy
- <! Doctype=html>
- <script src="jquery.js" type="Text/javascript" ></script>
- <body>
- <div> drop-down load more </div>
- <div class="main" style="Height:700px;overflow:auto;" >
- <div class="Child" style=' border:1px solid red;margin-top:20px;color:grey;height:800px ' ></div >
- </div>
- </body>
- <script type="Text/javascript" >
- $ (document). Ready (function () {
- $ (". Main"). Unbind ("scroll"). Bind ("scroll", function (e) {
- var sum = this.scrollheight;
- if (sum <= $ (this). ScrollTop () + $ (this). Height ()) {
- $ (". Main"). Append ($ (". Child"). Clone ());
- }
- });
- });
- </script>
If the scroll bar is pulled to the bottom and then loaded, the user experience is affected. It takes time to request resources from the server when it is generally dynamically loaded. A better way is to dynamically load more and request resources from the server when the scrollbar is at a certain distance (C) from the bottom. That is, pre-load, pre-read. The formula is as follows.
[JavaScript]View PlainCopy
- This.scrollheight-c <= $ (this). ScrollTop () + $ (this). Height ()
A JS function, the final discovery is actually a mathematical problem. It's tedious to look at the code of similar functions written by colleagues at first. When the formula is summed up, the idea is immediately clear. Through the phenomenon to see the essence.
Drop-Down Load more demo (JS implementation)