I have a small slider that switches the preview image to the main image. It works fine now
blade.php
<div class="custom-carousel-section">
<div class="custom-container">
<div class="custom-carousel">
@if(!empty($article_block_images)) @foreach($article_block_images as $article_block_image)
<div class="custom-carousel__title">
@if($loop->first)
<span>{{ $article_block_image->title }}</span>
@endif
</div>
@endforeach @endif
@if(!empty($article_block_images)) @foreach($article_block_images as $article_block_image)
<div class="main-image">
@if($loop->first)
<img src="{{ $article_block_image->main_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
@endif
</div>
@endforeach @endif
<div class="img-to-select">
@if(!empty($article_block_images)) @foreach($article_block_images as $article_block_image)
<div @if($loop->first) class="img-to-select__item selected" @else class="img-to-select__item" @endif>
<img src="{{ $article_block_image->preview_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}" data-main-src="{{ $article_block_image->main_image }}">
</div>
@endforeach @endif
</div>
</div>
</div>
</div>
js
$('.img-to-select__item').click(function () {
$('.img-to-select__item').removeClass('selected');
$(this).addClass('selected');
$('.main-image > img').attr('src', $(this).children('img').data('main-src'));
$('.custom-carousel__title > span').html($(this).children('img').attr('data-title'));
});
But I still have a picture mobile_image, and I want the mobile_image to change instead of main_image at the maximum size of 576px, for this I use <picture>
I change my code to like this
<div class="main-image">
@if($loop->first)
<picture>
<source srcset="{{ $article_block_image->mobile_image }}" media="(max-width: 576px)" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
<source srcset="{{ $article_block_image->main_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
<img src="{{ $article_block_image->main_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
</picture>
@endif
</div>
But my slider just stops working
The main image eventually changes to mobile, but the slider itself does not switch
In this case, the preview images also echo, but the main image itself does not change
My slider essentially works like this
I just need to put what in the main-image class in <picture> and change the image depending on the screen size