JQuery implements the dynamic loading effect of list content and jquery dynamic loading effect

Source: Internet
Author: User

JQuery implements the dynamic loading effect of list content and jquery dynamic loading effect

The List data is dynamically updated using Jquery. The updated data can be the data requested by ajax.

CSS:

.main { width: 100%; margin-top: 100px; text-align: center; font-size: 12.5px;}th, td { border: 1px solid #ccc; line-height: 40px; padding-left: 5px;}.item:hover { background-color: #efefef;}.item:nth-child(2n) { background-color: #efefef;}.ListView { width: 600px; overflow: hidden; margin: 0 auto; padding: 10px; height:372px; border: 1px solid #dddddd;}.ListView .c { width: 1200px; margin: 0 auto; border-collapse: collapse;}.Item { border-bottom: 1px dashed #dddddd; padding: 10px 0 10px 0; overflow: hidden; margin-left:600px;}.Item span { float: left; text-align: left;}.Item span:first-child { color: #6AA8E8;}.Item span:last-child { text-align: center;}

HTML

<Div class = "main"> <div class = "ListView"> <div class = "c"> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <div class = "Item"> <span> test </span> <span> male/0 </span> <span> Sichuan, chengdu, jinjiang district </span> <span> detailed description </span> </div> <p style = "text-align: center; "> <a href =" javascript: void (0); "onClick =" ListView. update (); "> refresh data </a> </p>

JS

<Script type = "text/javascript" src = "/javascript/jquery-1.8.0.min.js"> </script> <script type = "text/javascript" >$ (function () {ListView. init () ;}); var ListView ={ Init: function () {$ (". item span "detail .css (" width ", $ (". listView "). width ()/4 + "px"); for (var I = 0; I <$ (". item "). length; I ++) {var target = $ (". item ") [I]; $ (target ). animate ({marginLeft: "0px"}, 300 + I * 100) ;}, Update: function () {$ (". listView. c. item "). remove (); for (var I = 0; I <10; I ++) {var newItem = $ ("<div class = \" Item \ "> <span> test </span> <span> male/" + I + "</span> <span> Sichuan Province, jinjiang district, Chengdu </span> <span> detailed description </span> </div> "); $ (newItem ). find ("span" ).css ("width", $ (". listView "). width ()/4 + "px"); $ (". listView. c "). append (newItem); $ (newItem ). animate ({marginLeft: "0px"}, 300 + I * 100) ;}}</script>

Attached demo http://demo.jb51.net/js/2015/jquery-dtlb

The effect is not very good. Next let's take a look at the Implementation ideas of the waterfall stream and the js Code for controlling dynamic loading.

The following code mainly controls the loading events when the scroll bar is pulled down.

The code below shows that you can write down your operations, whether loading images or loading record data.

Don't forget to reference the jquery class library

$ (Window ). scroll (function () {var scrollTop = $ (this ). scrollTop (); var scrollHeight = $ (document ). height (); var returns wheight = $ (this ). height (); if (scrollTop + windowHeight = scrollHeight) {// This is the event triggered when the scroll bar reaches the bottom. Write the data to be loaded here, or pull the scroll bar/var page = Number ($ ("# redgiftNextPage "). attr ('currentpage') + 1; // redgiftList (page); // $ ("# redgiftNextPage "). attr ('currentpage', page + 1 );}});

Resolution:

To judge the scroll bar to the bottom, three DOM attribute values are required, namely scrollTop, clientHeight, and scrollHeight.
ScrollTop indicates the scroll distance of the scroll bar on the Y axis.
ClientHeight is the height of the visible area of the content.
ScrollHeight is the height of the visible area of the content plus the overflow (scroll) distance.
From the introduction of these three attributes, we can see that the condition from the scroll bar to the bottom is scrollTop + clientHeight = scrollHeight. (Compatible with different browsers ).

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.