Drop-Down Load more demo (JS implementation)

Source: Internet
Author: User

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 
    1. This.scrollheight <= $ (this). ScrollTop () + $ (this). Height ()


Give a demo that can run and need to introduce jquery.

[JavaScript]View PlainCopy 
  1. <! Doctype=html>
  2. <script src="jquery.js" type="Text/javascript" ></script>
  3. <body>
  4. <div> drop-down load more </div>
  5. <div class="main" style="Height:700px;overflow:auto;" >
  6. <div class="Child" style=' border:1px solid red;margin-top:20px;color:grey;height:800px ' ></div >
  7. </div>
  8. </body>
  9. <script type="Text/javascript" >
  10. $ (document). Ready (function () {
  11. $ (". Main"). Unbind ("scroll"). Bind ("scroll", function (e) {
  12. var sum = this.scrollheight;
  13. if (sum <= $ (this). ScrollTop () + $ (this). Height ()) {
  14. $ (". Main"). Append ($ (". Child"). Clone ());
  15. }
  16. });
  17. });
  18. </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  
    1. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.