Home › Forums › WoodMart support forum › Lazyload images from mega menu
Lazyload images from mega menu
- This topic has 10 replies, 2 voices, and was last updated 4 years, 7 months ago by
Jeriss Cloud Center.
-
AuthorPosts
-
July 16, 2020 at 11:06 pm #211789
Jeriss Cloud CenterParticipantDear,
https://xtemos.com/forums/topic/lazyload-images-from-mega-menu/
I’ve raised this issue 6 months ago and so far I still don’t see any update on this problem. Did you implement any fixes for this issue? If not, it would really be time now for you to start considering this problem, it’s a real issue and it can’t keep waiting forever please. It’s a MAJOR problem in the theme.
I’ve been using the workaround of “Ajax Mega Menu” but I really don’t like it, it’s causing users telling me that they must always wait before seeing the menu loaded. It’s not practical at all. Please just fix this lazyload of images on mega menu.
Thanks
July 17, 2020 at 8:10 am #211867
Artem TemosKeymasterHello,
Try to add the following PHP code snippet to the child theme functions.php file to do this
add_action( 'woodmart_after_body_open', function() { remove_action( 'woodmart_attachment', 'woodmart_lazy_attachment_replace', 10) ; add_filter('woodmart_attachment', 'woodmart_custom_lazy_attachment_replace', 10, 3); } ); add_action( 'woodmart_after_header', function() { add_filter('woodmart_attachment', 'woodmart_lazy_attachment_replace', 10, 3); remove_action( 'woodmart_attachment', 'woodmart_custom_lazy_attachment_replace', 10) ; } ); function woodmart_custom_lazy_attachment_replace( $imgHTML, $attach_id, $size ) { if ( preg_match( "/src=['\"]data:image/is", $imgHTML ) ) return $imgHTML; if( $attach_id ) { $lazy_image = woodmart_get_attachment_placeholder( $attach_id, $size ); } else { $lazy_image = woodmart_lazy_get_default_preview(); } return woodmart_custom_lazy_replace_image( $imgHTML, $lazy_image ); } function woodmart_custom_lazy_replace_image( $html, $src ) { $class = woodmart_custom_lazy_css_class(); $new = preg_replace( '/<img(.*?)src=/is', '<img$1src="'.$src.'" data-wood-src=', $html ); $new = preg_replace( '/<img(.*?)srcset=/is', '<img$1srcset="" data-srcset=', $new ); if ( preg_match( '/class=["\']/i', $new ) ) { $new = preg_replace( '/class=(["\'])(.*?)["\']/is', 'class=$1' . $class . ' $2$1', $new ); } else { $new = preg_replace( '/<img/is', '<img class="' . $class . '"', $new ); } return $new; } function woodmart_custom_lazy_css_class() { $class = 'woodmart-lazy-load-custom'; $class .= ' woodmart-lazy-' . woodmart_get_opt( 'lazy_effect' ); return $class; }
and this part to the custom JS in Theme Settings
jQuery('.whb-navigation li').on('hover', function(){ jQuery(this).find('.woodmart-lazy-load-custom').each(function () { var $img = jQuery(this); $img.attr('src', jQuery(this).data('wood-src')); $img.addClass('woodmart-loaded'); }); });
Regards
July 17, 2020 at 8:13 pm #212010
Jeriss Cloud CenterParticipantDear,
I tried the provided codes but they don’t seem to work. Images are not loading at all then.
Can you check this staging in private?
Thanks
July 20, 2020 at 6:52 am #212479
Artem TemosKeymasterPlease, add the code first and then we will check how it works.
July 20, 2020 at 6:58 am #212482
Jeriss Cloud CenterParticipantDear,
I did and it didnt work. I removed it for other testing reasons for another issue. I’ll re add it again
July 20, 2020 at 8:36 am #212535
Jeriss Cloud CenterParticipantI just re-added the codes, can you check please?
July 20, 2020 at 8:38 am #212537
Jeriss Cloud CenterParticipantHere is how it looks, see private
July 21, 2020 at 6:31 am #212796
Artem TemosKeymasterTry to replace the JS code part with the following
jQuery('.whb-navigation li').hover(function() { jQuery(this).find('.woodmart-lazy-load-custom').each(function() { var $img = jQuery(this); $img.attr('src', jQuery(this).data('wood-src')); $img.addClass('woodmart-loaded'); var $categories = $img.parents('.categories-masonry'); if ($categories.length > 0) { $categories.imagesLoaded(function() { $categories.packery(); }); } }); });
July 21, 2020 at 10:00 am #212847
Jeriss Cloud CenterParticipantAbsolutely awesome, it works perfectly ! THANKS !
Are you going to include this feature in the theme or I just keep it as custom code ?
July 21, 2020 at 11:09 am #212856
Artem TemosKeymasterWe suggest you to keep the code while we don’t add this option. Check our changelogs for this.
July 29, 2020 at 8:08 pm #215220
Jeriss Cloud CenterParticipantAlright, you can close this ticket on my site.
Thanks
-
AuthorPosts
The topic ‘Lazyload images from mega menu’ is closed to new replies.
- You must be logged in to create new topics. Login / Register