標籤:
本文參閱http://www.appelsiini.net/projects/lazyload
Lazy Load is delays loading of images in long web pages. Images outside of viewport are not loaded until user scrolls to them. This is opposite of image preloading.
Using Lazy Load on long web pages will make the page load faster. In some cases it can also help to reduce server load.
Plugin is inspired by YUI ImageLoader Utility by Matt Mlinac.
消極式載入在長網頁圖片的消極式載入。映像視窗不載入,直到使用者以外的捲軸。這是相反的映像預先載入。
在長時間使用消極式載入web頁面將使頁面載入速度更快。在某些情況下,它也可以協助減少伺服器負載。
外掛程式是由馬特Mlinac受YUI ImageLoader效用。
For those in hurry there are several demo pages: basic options, with fadein effect, noscript fallback, horizontal scrolling, horizontal scrolling inside container, vertical scrolling inside container, page with gazillion images, load images using timeout and load images using AJAX(H).
When checking the demos clear browser cache between each request. You can check what is actually loaded with developers console (Chrome, Safari and IE) or FireBug (Firefox).
這裡點有幾個示範頁:基本選項,與漸顯效果,noscript撤退,水平滾動,水平滾動容器內,容器內垂直滾動,與無數圖片頁面,使用AJAX載入映像使用逾時和載入映像(H)。
當檢查每個請求之間的示範清除瀏覽器緩衝。您可以檢查實際裝載與開發人員控制台(Safari和Chrome IE)或FireBug(Firefox)。
Lazy Load depends on Jquery. Include them both in end of your HTML code:
消極式載入取決於Jquery。包括他們在結束你的HTML代碼:
<script src="jquery.js"></script><script src="jquery.lazyload.js"></script>
You must alter your image tags. Address of the image must be put into data-originalattribute. Give lazy loaded images a specific class. This way you can easily control which images plugin is binded to.
你必須改變你的映像標記。映像的地址必須放入data-original屬性。給懶載入映像一個特定的類。這樣你可以很容易地控制哪些圖片外掛程式綁定。
<img class="lazy" data-original="img/example.jpg" width="640" height="480">$(function() { $("img.lazy").lazyload();});
Setting Threshold
設定閾值
By default images are loaded when they appear on the screen. If you want images to load earlier use threshold parameter. Setting threshold to 200 causes image to load 200 pixels before it appears on viewport.
預設載入圖片時出現在螢幕上。如果你想要圖片載入之前使用閾值參數。閾值設定為200年導致映像載入之前出現在視窗200像素。
$("img.lazy").lazyload({ threshold : 200});
Event to Trigger Loading
事件觸發載入
You can use jQuery event such as click or mouseover. You can also use custom events such as sporty or foobar. Default is to wait until user scrolls down and image appears on the viewport. To load images only when user clicks them you could do:
您可以使用jQuery點擊或滑鼠移至上方等事件。您還可以使用定製的事件,如運動或foobar。預設是等到使用者向下滾動和形象出現在視窗。載入圖片只有當使用者點擊他們你可以:
$("img.lazy").lazyload({ event : "click"});
Using Effects
使用效果
By default plugin waits for image to fully load and calls show(). You can use any effect you want. Following code uses fadeIn effect. Check how it works at effect demo p
預設外掛程式等待形象完全載入和調用顯示()。你可以使用任何你想要的效果。下面的代碼使用漸顯效果。這是示範頁面。
$("img.lazy").lazyload({ effect : "fadeIn"});
Images Inside Container
圖片內容器
You can also use plugin for images inside scrolling container, such as div with scrollbar. Just pass the container as jQuery object. There is a demo for horizonta and vertical container.
您還可以使用外掛程式的圖片滾動容器內部,比如div捲軸。只是通過jQuery對象的容器。有一個示範臥式鍛機和垂直容器。
#container { height: 600px; overflow: scroll;}
$("img.lazy").lazyload({ container: $("#container")});
When Images Are Not Sequential
當映像並不是連續的
After scrolling page plugin loops though unloaded images. Loop checks if image has become visible. By default loop is stopped when first image outside viewport is found. This is based on following assumption. Order of images on page is same as order of images in HTML code. With some layouts assumption this might be wrong. You can control loading behaviour with failure_limit setting.
滾動頁面外掛程式後迴圈雖然卸載圖片。迴圈檢查圖片是否在可視地區。預設時停止迴圈以外的首家形象視窗。這是基於以下的假設。圖片在頁面的次序是HTML代碼相同圖片的順序。與一些布局中,這樣的假設是錯誤的。你可以控制failure_limit設定載入行為。
$("img.lazy").lazyload({ failure_limit : 10});
Dealing With Invisible Images
處理不可見的映像
There are cases when you have images which are in viewport but not :visible. To improve performance you can ignore .not(":visible") images.
在某些情況下當你有圖片的視窗而不是:可見。以提高效能可以忽略自身之外(":可見")映像。
$("img.lazy").lazyload({ skip_invisible : true});
Jquery之消極式載入