Magento acceleration tool-lazyload

Source: Internet
Author: User
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
?
  1. (
    Function
    ($ ){
  2. $. FN. lazyload = Function
    (Options ){
  3. VaR
    Settings = {
  4. Threshold: 0,
  5. Failurelimit: 0,
  6. Event: "scroll"
    ,
  7. Effect: "show"
    ,
  8. Container: Window
  9. };
  10. If
    (Options ){
  11. $. Extend (settings, options );
  12. }
  13. /* Fire one scroll event per scroll. Not one scroll event per image .*/

  14. VaR
    Elements =
    This
    ;
  15. If
    (
    "Scroll"
    = Settings. Event ){
  16. $ (Settings. Container). BIND ("scroll"
    ,
    Function
    (Event ){
  17. VaR
    Counter = 0;
  18. Elements. Each (Function
    (){
  19. If
    ($. Abovethetop (
    This
    , Settings) |
  20. $. Leftofbegin (this
    , Settings )){
  21. /* Nothing .*/

  22. } Else
     
    If
    (! $. Belowthefold (
    This
    , Settings )&&
  23. ! $. Rightoffold (this
    , Settings )){
  24. $ (This
    ). Trigger (
    "Appear"
    );
  25. } Else
    {
  26. If
    (Counter ++> Settings. failurelimit ){
  27. Return
     
    False
    ;
  28. }
  29. }
  30. });
  31. /* Remove image from array so it is not looped next time .*/

  32. VaR
    Temp = $. grep (elements,
    Function
    (Element ){
  33. Return
    ! Element. loaded;
  34. });
  35. Elements = $ (temp );
  36. });
  37. }
  38. This
    . Each (
    Function
    (){
  39. VaR
    Self =
    This
    ;
  40. /* When appear is triggered load original image .*/

  41. $ (Self). One ("appear"
    ,
    Function
    (){
  42. If
    (!
    This
    . Loaded ){
  43. $ ("<IMG alt ="
    "/>"
    )
  44. . BIND ("LOAD"
    ,
    Function
    (){
  45. $ (Self)
  46. . Hide ()
  47. . ATTR ("src"
    , $ (Self). ATTR (
    "Original"
    ))
  48. [Settings. effect] (settings. effectspeed );
  49. Self. Loaded = true
    ;
  50. })
  51. . ATTR ("src"
    , $ (Self). ATTR (
    "Original"
    ));
  52. };
  53. });
  54. /* When wanted event is triggered load original image */

  55. /* By triggering appear .*/

  56. If
    (
    "Scroll"
    ! = Settings. Event ){
  57. $ (Self). BIND (settings. event, Function
    (Event ){
  58. If
    (! Self. Loaded ){
  59. $ (Self). Trigger ("appear"
    );
  60. }
  61. });
  62. }
  63. });
  64. /* Force initial check if images shoshould appear .*/

  65. $ (Settings. Container). Trigger (settings. Event );
  66. Return
     
    This
    ;
  67. };
  68. /* Convenience methods in jquery namespace .*/

  69. /* Use as $. belowthefold (element, {threshold: 100, container: Window })*/

  70. $. Belowthefold = Function
    (Element, settings ){
  71. If
    (Settings. Container === undefined | settings. Container === window ){
  72. VaR
    Fold = $ (window). Height () + $ (window). scrolltop ();
  73. } Else
    {
  74. VaR
    Fold = $ (settings. Container). offset (). Top + $ (settings. Container). Height ();
  75. }
  76. Return
    Fold <= $ (element). offset (). Top-settings. threshold;
  77. };
  78. $. Rightoffold = Function
    (Element, settings ){
  79. If
    (Settings. Container === undefined | settings. Container === window ){
  80. VaR
    Fold = $ (window). Width () + $ (window). scrollleft ();
  81. } Else
    {
  82. VaR
    Fold = $ (settings. Container). offset (). Left + $ (settings. Container). Width ();
  83. }
  84. 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 ();
  85. };
  86. $. Leftofbegin = Function
    (Element, settings ){
  87. If
    (Settings. Container === undefined | settings. Container === window ){
  88. VaR
    Fold = $ (window). scrollleft ();
  89. } Else
    {
  90. VaR
    Fold = $ (settings. Container). offset (). Left;
  91. }
  92. Return
    Fold> = $ (element). offset (). Left + settings. Threshold + $ (element). Width ();
  93. };
  94. /* Custom selectors for your convenience .*/

  95. /* Use as $ ("IMG: below-the-fold"). Something ()*/

  96. $. Extend ($. expr [':'
    ], {
  97. "Below-the-fold"
    :
    "$. Belowthefold (A, {threshold: 0, container: Window })"
    ,
  98. "Above-the-fold"
    :
    "! $. Belowthefold (A, {threshold: 0, container: Window })"
    ,
  99. "Right-of-fold"
    :
    "$. Rightoffold (A, {threshold: 0, container: Window })"
    ,
  100. "Left-of-fold"
    :
    "! $. Rightoffold (A, {threshold: 0, container: Window })"

  101. });
  102. $ (Function
    (){
  103. $ ("IMG [original]"
    ). Lazyload ({
  104. Threshold: 200, effect: "fadein"

  105. });
  106. });
  107. }) (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

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.