Home Forums WoodMart support forum Lazyload images from mega menu

Lazyload images from mega menu

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #211789

    Jeriss Cloud Center
    Participant

    Dear,

    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

    #211867

    Artem Temos
    Keymaster

    Hello,

    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

    #212010

    Jeriss Cloud Center
    Participant

    Dear,

    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

    #212479

    Artem Temos
    Keymaster

    Please, add the code first and then we will check how it works.

    #212482

    Jeriss Cloud Center
    Participant

    Dear,

    I did and it didnt work. I removed it for other testing reasons for another issue. I’ll re add it again

    #212535

    Jeriss Cloud Center
    Participant

    I just re-added the codes, can you check please?

    #212537

    Jeriss Cloud Center
    Participant

    Here is how it looks, see private

    #212796

    Artem Temos
    Keymaster

    Try 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();
                });
            }
        });
    });
    #212847

    Jeriss Cloud Center
    Participant

    Absolutely awesome, it works perfectly ! THANKS !

    Are you going to include this feature in the theme or I just keep it as custom code ?

    #212856

    Artem Temos
    Keymaster

    We suggest you to keep the code while we don’t add this option. Check our changelogs for this.

    #215220

    Jeriss Cloud Center
    Participant

    Alright, you can close this ticket on my site.

    Thanks

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Lazyload images from mega menu’ is closed to new replies.