Magento acceleration tool-lazyload
Magento
Speed is a headache. I am introducing a front-end acceleration method. Speaking of lazyload, I believe many people are familiar with it. Now many websites have used this technology, and he can delay loading pictures of long pages. The acceleration of magento is obvious. The lazyload I am talking about today goes through Popo
Transformed (forced me to add external connections to him ......). Below I put some test data, a list page of a website I am working on.
Obviously, we can see the gap. Next, write down the usage:
First, add jquery and lazyload.
Lazyload:
View plain
Copy to clipboard
Print
?
- (
Function
($ ){
- $. FN. lazyload = Function
(Options ){
- VaR
Settings = {
- Threshold: 0,
- Failurelimit: 0,
- Event: "scroll"
,
- Effect: "show"
,
- Container: Window
- };
- If
(Options ){
- $. Extend (settings, options );
- }
- /* Fire one scroll event per scroll. Not one scroll event per image .*/
- VaR
Elements =
This
;
- If
(
"Scroll"
= Settings. Event ){
- $ (Settings. Container). BIND ("scroll"
,
Function
(Event ){
- VaR
Counter = 0;
- Elements. Each (Function
(){
- If
($. Abovethetop (
This
, Settings) |
- $. Leftofbegin (this
, Settings )){
- /* Nothing .*/
- } Else
If
(! $. Belowthefold (
This
, Settings )&&
- ! $. Rightoffold (this
, Settings )){
- $ (This
). Trigger (
"Appear"
);
- } Else
{
- If
(Counter ++> Settings. failurelimit ){
- Return
False
;
- }
- }
- });
- /* Remove image from array so it is not looped next time .*/
- VaR
Temp = $. grep (elements,
Function
(Element ){
- Return
! Element. loaded;
- });
- Elements = $ (temp );
- });
- }
- This
. Each (
Function
(){
- VaR
Self =
This
;
- /* When appear is triggered load original image .*/
- $ (Self). One ("appear"
,
Function
(){
- If
(!
This
. Loaded ){
- $ ("<IMG alt ="
"/>"
)
- . BIND ("LOAD"
,
Function
(){
- $ (Self)
- . Hide ()
- . ATTR ("src"
, $ (Self). ATTR (
"Original"
))
- [Settings. effect] (settings. effectspeed );
- Self. Loaded = true
;
- })
- . ATTR ("src"
, $ (Self). ATTR (
"Original"
));
- };
- });
- /* When wanted event is triggered load original image */
- /* By triggering appear .*/
- If
(
"Scroll"
! = Settings. Event ){
- $ (Self). BIND (settings. event, Function
(Event ){
- If
(! Self. Loaded ){
- $ (Self). Trigger ("appear"
);
- }
- });
- }
- });
- /* Force initial check if images shoshould appear .*/
- $ (Settings. Container). Trigger (settings. Event );
- Return
This
;
- };
- /* Convenience methods in jquery namespace .*/
- /* Use as $. belowthefold (element, {threshold: 100, container: Window })*/
- $. Belowthefold = Function
(Element, settings ){
- If
(Settings. Container === undefined | settings. Container === window ){
- VaR
Fold = $ (window). Height () + $ (window). scrolltop ();
- } Else
{
- VaR
Fold = $ (settings. Container). offset (). Top + $ (settings. Container). Height ();
- }
- Return
Fold <= $ (element). offset (). Top-settings. threshold;
- };
- $. Rightoffold = Function
(Element, settings ){
- If
(Settings. Container === undefined | settings. Container === window ){
- VaR
Fold = $ (window). Width () + $ (window). scrollleft ();
- } Else
{
- VaR
Fold = $ (settings. Container). offset (). Left + $ (settings. Container). Width ();
- }
- Return
Fold <= $ (element). offset (). Left-settings. Threshold ;}; $. abovethetop =
Function
(Element, settings ){
If
(Settings. Container === undefined | settings. Container === window ){
VaR
Fold = $ (window). scrolltop ();}
Else
{
VaR
Fold = $ (settings. Container). offset (). Top ;}
Return
Fold >=$ (element). offset (). Top + settings. Threshold + $ (element). Height ();
- };
- $. Leftofbegin = Function
(Element, settings ){
- If
(Settings. Container === undefined | settings. Container === window ){
- VaR
Fold = $ (window). scrollleft ();
- } Else
{
- VaR
Fold = $ (settings. Container). offset (). Left;
- }
- Return
Fold> = $ (element). offset (). Left + settings. Threshold + $ (element). Width ();
- };
- /* Custom selectors for your convenience .*/
- /* Use as $ ("IMG: below-the-fold"). Something ()*/
- $. Extend ($. expr [':'
], {
- "Below-the-fold"
:
"$. Belowthefold (A, {threshold: 0, container: Window })"
,
- "Above-the-fold"
:
"! $. Belowthefold (A, {threshold: 0, container: Window })"
,
- "Right-of-fold"
:
"$. Rightoffold (A, {threshold: 0, container: Window })"
,
- "Left-of-fold"
:
"! $. Rightoffold (A, {threshold: 0, container: Window })"
- });
- $ (Function
(){
- $ ("IMG [original]"
). Lazyload ({
- Threshold: 200, effect: "fadein"
- });
- });
- }) (Jquery );
(Function ($) <br/> {$. FN. lazyload = function (options) {var settings = {threshold: 0, <br/> failurelimit: 0, event: "scroll", effect: "show", container: window <br/>}; if (options) {$. extend (settings, options);}/* fire one scroll <br/> event per scroll. not one scroll event per image. */var elements = <br/> This; If ("scroll" = settings. event) {<br/> $ (settings. container ). BIND ("scroll", func Tion (event) {var counter = 0; <br/> elements. each (function () {if ($. abovethetop (this, settings) | <br/> $. leftofbegin (this, settings) {/* nothing. */} else if <br/> (! $. Belowthefold (this, settings )&&! $. Rightoffold (this, <br/> Settings) {$ (this ). trigger ("appear");} else {If (counter ++> <br/> Settings. failurelimit) {return false ;}});/* remove image from <br/> array so it is not looped next time. */var temp = $. grep (elements, <br/> function (element) {return! Element. loaded ;}); elements =$ (temp) ;}); <br/>} this. each (function () {var self = This;/* When appear is triggered <br/> load original image. */$ (Self ). one ("appear", function () {If <br/> (! This. loaded) {$ (" "). BIND ("LOAD", function () {<br/> $ (Self ). hide (). ATTR ("src", $ (Self ). ATTR ("original") <br/> [settings. effect] (settings. effectspeed); self. loaded = true ;}) <br/>. ATTR ("src", $ (Self ). ATTR ("original") ;};});/* When wanted event is <br/> triggered load original image * // * by triggering appear. */If <br/> ("scroll "! = Settings. Event) {$ (Self). BIND (settings. event, <br/> function (event) {If (! Self. loaded) {$ (Self ). trigger ("appear") ;}}); <br/>});/* force initial check if images shoshould appear. */<br/> $ (settings. container ). trigger (settings. event); return this ;};/* <br/> convenience methods in jquery namespace. * // * use as <br/> $. belowthefold (element, {threshold: 100, container: Window}) */<br/> $. belowthefold = function (element, settings) {If (settings. container <br/> === undefi Ned | settings. container === window) {var fold = <br/> $ (window ). height () + $ (window ). scrolltop ();} else {var fold = <br/> $ (settings. container ). offset (). top + $ (settings. container ). height () ;}< br/> return fold <= $ (element ). offset (). top-settings. threshold ;}; <br/> $. rightoffold = function (element, settings) {If (settings. container <br/>== undefined | settings. container === window) {var fo LD = <br/> $ (window ). width () + $ (window ). scrollleft ();} else {var fold = <br/> $ (settings. container ). offset (). left + $ (settings. container ). width () ;}< br/> return fold <= $ (element ). offset (). left-settings. threshold ;}; <br/> $. abovethetop = function (element, settings) {If (settings. container <br/>== undefined | settings. container === window) {var fold = <br/> $ (window ). scrolltop ();} else {VaR Fold = <br/> $ (settings. container ). offset (). top;} return fold >=< br/> $ (element ). offset (). top + settings. threshold + $ (element ). height () ;}; <br/> $. leftofbegin = function (element, settings) {If (settings. container <br/>== undefined | settings. container === window) {var fold = <br/> $ (window ). scrollleft ();} else {var fold = <br/> $ (settings. container ). offset (). left;} return fold >=< br/> $ (Ele Ment ). offset (). left + settings. threshold + $ (element ). width () ;}; <br/>/* Custom selectors for your convenience. * // * use as <br/> $ ("IMG: below-the-fold "). something () */$. extend ($. expr [':'], {<br/> "below-the-fold": "$. belowthefold (A, {threshold: 0, container: <br/> window}) "," above-the-fold ":"! $. Belowthefold (A, {threshold: 0, <br/> container: Window}) "," right-of-fold ":" $. rightoffold (A, {threshold: <br/> 0, container: Window}) "," left-of-fold ":"! $. Rightoffold (A, {threshold <br/>: 0, container: Window}) "}); $ (function () {<br/> $ ("IMG [original]"). lazyload ({threshold: 200, effect: "fadein"}) ;}); <br/>}) (jquery );
Then modify the image path.
Example: = "<? PHP echo $ this-> getskinurl ('images/co.jpg ')?>" />
Change to = "<? PHP echo $ this-> getskinurl ('images/co.jpg ')?>" Src = "......"
/>.
The image in the SRC below is a 1 pixel transparent GIF image.
In this way, you can try it if you are interested.
Original article link: http://www.magentofront-end.com/magentomuban/232