From: http://www.cnblogs.com/BearsTaR/
I often go to Tudou network and find that the function of loading images on the Tudou homepage is very interesting. The thumbnails of all videos on the Tudou homepage from the "Entertainment" section are not loaded after the page is opened, instead, when you drag the scroll bar to the "Entertainment" section, you start to load the image. The advantage of this is, of course, that if a user does not need to view the following content, all the image requests are removed, which is helpful for reducing the server pressure.
Implementation:
In fact, the implementation principle of Tudou is very simple,
1. First, set the srcof all images with delayed delay to the connection (sprite.gif) of the same small image, and put the connection of the real image into the alt attribute of the image.Code:
<A class = "inner" target = "new" Title = "the heaviest street dance runners and softest street dance runners in history" href = "http://www.tudou.com/programs/view/Utmt1_6Z-lU/">
</A>
2. Bind the window. scroll event, reset the SRC value of all images whose classes are lazyimg in the event, and find the following js on the Tudou homepage:
VaR o = function (){
VaR S = tui. Pos. scrolltop (), q = C;
If (Q. Box [0]) {
VaR r = Q. Box. offset (). Top;
If (R-S> 0 & r-TUI.pos.windowHeight () <s ){
Q. INIT ()
} Else {
Q. Stop ()
}
}
If (! H | S <590) {return true}
Tui. widget. quickplaylist. Load ();
H = false
};
O ();
$ (Window). BIND ("scroll", O );
I have not followed the implementation of The tui. widget. quickplaylist. Load () method. The JS of Tudou is compressed and obfuscated. It looks tired, but you can understand the principle.
Instance:
As mentioned above, it is more practical to come to an instance.
View Source
Print?
01 |
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
02 |
< Html Xmlns = Http://www.w3.org/1999/xhtml" > |
04 |
meta HTTP-equiv = "Content-Type" content = "text/html; charset = UTF-8 " /> |
05 |
< Script Type = "Text/JavaScript" SRC = Http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" > </ Script > |
08 |
See images: < IMG SRC = Http://at-img4.tdimg.com/board/2010/5/tylc-115X55.jpg" /> |
10 |
< Div ID = "Lazybox" Style = "Margin-top: 100px ;" > |
11 |
Images not seen at the beginning: |
12 |
< IMG Width = "120" Height = "90" Style = "Border: 1px solid blue ;" Class = "Lazyimg" ALT = Http://i01.img.tudou.com/data/imgs/ I /051/720/095/p.jpg" SRC = Http://css.tudouui.com/skin/__g/img/sprite.gif" Coords = "_ Daa" /> |
13 |
< IMG Width = "120" Height = "90" Style = "Border: 1px solid blue ;" Class = "Lazyimg" ALT = Http://i01.img.tudou.com/data/imgs/ I /051/871/396/m20.jpg" SRC = Http://css.tudouui.com/skin/__g/img/sprite.gif" Coords = "_ Dba" /> |
15 |
< Div Style = "Height: 1000px ;" > |
18 |
< Script Type = "Text/JavaScript" > |
20 |
$ (Window). BIND ("scroll", function (){ |
22 |
$ (Window). Unbind ("scroll "); |
25 |
VaR T = $ (document). scrolltop (); |
27 |
// The scroll height exceeds 50 and the image is loaded. |
29 |
$ ("# Lazybox. lazyimg"). Each (function (){ |
30 |
$ (This). ATTR ("src", $ (this). ATTR ("Alt ")); |
Copy the above Code to the local run to see the effect.