Commit a63221892928b68c0feda2272ad5282d61aa2f4e
1 parent
a1dded18
Lazyload
Showing
4 changed files
with
97 additions
and
2 deletions
Show diff stats
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace artweb\artbox\assets; | |
| 4 | + | |
| 5 | +use yii\web\AssetBundle; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Main frontend application asset bundle. | |
| 9 | + */ | |
| 10 | +class LazyAsset extends AssetBundle | |
| 11 | +{ | |
| 12 | + public $sourcePath = '@artweb/artbox/resources'; | |
| 13 | + public $js = [ | |
| 14 | + 'artbox-lazy.js', | |
| 15 | + ]; | |
| 16 | + | |
| 17 | + public $depends = [ | |
| 18 | + 'artweb\artbox\assets\LazyLoadAsset', | |
| 19 | + ]; | |
| 20 | + | |
| 21 | +} | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace artweb\artbox\assets; | |
| 4 | + | |
| 5 | +use yii\web\AssetBundle; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Main frontend application asset bundle. | |
| 9 | + */ | |
| 10 | +class LazyLoadAsset extends AssetBundle | |
| 11 | +{ | |
| 12 | + public $sourcePath = '@bower/jquery.lazyload'; | |
| 13 | + public $js = [ | |
| 14 | + 'jquery.lazyload.js', | |
| 15 | + ]; | |
| 16 | + | |
| 17 | + public $depends = [ | |
| 18 | + 'yii\web\JqueryAsset', | |
| 19 | + ]; | |
| 20 | + | |
| 21 | +} | ... | ... |
components/artboximage/ArtboxImageHelper.php
| ... | ... | @@ -53,14 +53,25 @@ |
| 53 | 53 | * @param array|string $preset |
| 54 | 54 | * @param array $imgOptions |
| 55 | 55 | * @param int $quality |
| 56 | + * @param bool $lazy | |
| 56 | 57 | * |
| 57 | 58 | * @return string |
| 58 | 59 | * @see Html::img() |
| 59 | 60 | */ |
| 60 | - public static function getImage($file, $preset, $imgOptions = [], $quality = 65) | |
| 61 | + public static function getImage($file, $preset, $imgOptions = [], $quality = 65, $lazy = false) | |
| 61 | 62 | { |
| 62 | 63 | $preset_alias = is_array($preset) ? array_keys($preset)[ 0 ] : null; |
| 63 | - return Html::img(self::getImageSrc($file, $preset, $preset_alias, $quality), $imgOptions); | |
| 64 | + $src = self::getImageSrc($file, $preset, $preset_alias, $quality); | |
| 65 | + if($lazy) { | |
| 66 | + if(isset($imgOptions['class'])) { | |
| 67 | + $imgOptions['class'] = $imgOptions['class'].' artbox-lazy'; | |
| 68 | + } else { | |
| 69 | + $imgOptions['class'] = 'artbox-lazy'; | |
| 70 | + } | |
| 71 | + return Html::img(''); | |
| 72 | + } else { | |
| 73 | + return Html::img($src, $imgOptions); | |
| 74 | + } | |
| 64 | 75 | } |
| 65 | 76 | |
| 66 | 77 | /** | ... | ... |
| 1 | +$(function() { | |
| 2 | + $('img.artbox-lazy').lazyload(); | |
| 3 | + lazyThreshold(); | |
| 4 | + lazyEvent(); | |
| 5 | + lazyEffect(); | |
| 6 | +}); | |
| 7 | +function lazyThreshold() { | |
| 8 | + $.each($('img.artbox-lazy-threshold'), function(index, value) { | |
| 9 | + var threshold = 200; | |
| 10 | + var attribute = $(value).attr('data-threshold'); | |
| 11 | + if(attribute) { | |
| 12 | + threshold = attribute; | |
| 13 | + } | |
| 14 | + this.lazyload({ | |
| 15 | + threshold: threshold | |
| 16 | + }); | |
| 17 | + }); | |
| 18 | +} | |
| 19 | +function lazyEvent() { | |
| 20 | + $.each($('img.artbox-lazy-event'), function(index, value) { | |
| 21 | + var event = 'click'; | |
| 22 | + var attribute = $(value).attr('data-event'); | |
| 23 | + if(attribute) { | |
| 24 | + event = attribute; | |
| 25 | + } | |
| 26 | + this.lazyload({ | |
| 27 | + event: event | |
| 28 | + }); | |
| 29 | + }); | |
| 30 | +} | |
| 31 | +function lazyEffect() { | |
| 32 | + $.each($('img.artbox-lazy-effect'), function(index, value) { | |
| 33 | + var effect = 'fadeIn'; | |
| 34 | + var attribute = $(value).attr('data-effect'); | |
| 35 | + if(attribute) { | |
| 36 | + effect = attribute; | |
| 37 | + } | |
| 38 | + this.lazyload({ | |
| 39 | + effect: effect | |
| 40 | + }); | |
| 41 | + }); | |
| 42 | +} | |
| 0 | 43 | \ No newline at end of file | ... | ... |