Loading data from the scroll drop-down to the bottom of the page is the practice of many waterfall stream websites. Let's take a look at how jsonp is implemented. You can use the summary record of the dishes.
Of course, this example uses the jquery library, which will be made into Native js later.
In this example, the data calls a piece of json provided by the sharp jquery book.
First, you must determine how the page is rolled to the bottom, that is, whether the height of scrollTop + window is greater than the height of the document. jquery code is as follows: $ (window ). scrollTop () + $ (window ). height () >=$ (document ). height ();
Bind the scroll event to the window. So the demo of the entire page can do this:
Copy codeThe Code is as follows:
<! 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>
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Style type = "text/css">
* {Margin: 0; padding: 0 ;}
Body {font-size: 12px ;}
P {margin: 5px ;}
. Box {padding: 10px ;}
</Style>
<! -- Introduce jQuery -->
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
$ (Window). bind ('scroll', function () {show ()});
Function show ()
{
If ($ (window). scrollTop () + $ (window). height () >=$ (document). height ())
{
AjaxRead ();
}
}
Function ajaxRead ()
{
Var html = "";
$. Ajax ({
Type: 'get ',
DataType: 'jsonp ',
Url: 'http: // api.flickr.com/services/feeds/photos_public.gne? Tags = car & tagmode = any & format = json & jsoncallback =? ',
BeforeSend: function () {console. log ('loading ...')},
Success: function (data ){
$. Each (data. items, function (I, item ){
Html + = '<div class = "box"> ';
Html + = 'Html + = '<a hreft = "' + item. link + '"> </a>'
Html + = '<div>' + item. tags + '</div> ';
Html + = '</div> ';
});
$ ("# ResText"). append ($ (html ));
},
Complete: function () {console. log ('Mission acomplete .')}
});
}
})
</Script>
</Head>
<Body>
<P> pull down </p> <p> move down pull </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p>/ p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> move down pull </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p>/ p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> move down pull </p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p>/ p> <p> pull down </p> <p> pull down </p> <p> pull down </p> <p> pull down </p>
<Div id = "resText">
</Div>
</Body>
</Html>
Pulling to the bottom is to implement asynchronous data loading. Of course, the actual project should also add how to display and operate if there is no data. You just need to judge these values.