shahr's avatar
Level 10

How can I both open the modal and have an image gallery?

How can I both open the modal and have an image gallery?

modal.blade.php

<div class="modal fade" id="carouselProduct" data-bs-backdrop="static" data-bs-keyboard="true" tabindex="-1" aria-labelledby="carouselProductLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="carouselProductLabel">Modal title</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Understood</button>
            </div>
        </div>
    </div>
</div>

index.blade.php

<div class="col-lg-4 mb-3">
    <div id="carouselProduct" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-inner">
            @foreach($product->galleries as $gallery)
                <div class="carousel-item {{ $loop->first ? 'active' : '' }}">
                    <img id="zoom" src="{{ asset('storage/'.$gallery->image) }}" class="d-block w-100" alt="{{ $product->title }}">
                </div>
            @endforeach
        </div>
        <div id="gallery" class="carousel-indicators">
            @foreach($product->galleries->take(4) as $gallery)
                <button type="button" data-bs-target="#carouselProduct" data-bs-slide-to="{{ $loop->index }}" class="{{ $loop->first ? 'active' : '' }} thumbnail clearfix" aria-current="true" aria-label="{{ $product->title }}" data-bs-toggle="modal">
                    <img src="{{ asset('storage/'.$gallery->image) }}" data-zoom-image="{{ asset('storage/'.$gallery->image) }}"  class="d-block w-100" alt="{{ $product->title }}">
                </button>
            @endforeach
        </div>
    </div>
</div>
0 likes
1 reply
LaryAI's avatar
Level 58

To both open the modal and have an image gallery, you can modify the index.blade.php file to include a button that triggers the modal and add the necessary JavaScript to handle the gallery functionality.

Here's an example of how you can modify the index.blade.php file:

<div class="col-lg-4 mb-3">
    <div id="carouselProduct" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-inner">
            @foreach($product->galleries as $gallery)
                <div class="carousel-item {{ $loop->first ? 'active' : '' }}">
                    <img id="zoom" src="{{ asset('storage/'.$gallery->image) }}" class="d-block w-100" alt="{{ $product->title }}">
                </div>
            @endforeach
        </div>
        <div id="gallery" class="carousel-indicators">
            @foreach($product->galleries->take(4) as $gallery)
                <button type="button" data-bs-target="#carouselProduct" data-bs-slide-to="{{ $loop->index }}" class="{{ $loop->first ? 'active' : '' }} thumbnail clearfix" aria-current="true" aria-label="{{ $product->title }}" data-bs-toggle="modal" data-bs-image="{{ asset('storage/'.$gallery->image) }}">
                    <img src="{{ asset('storage/'.$gallery->image) }}" data-zoom-image="{{ asset('storage/'.$gallery->image) }}" class="d-block w-100" alt="{{ $product->title }}">
                </button>
            @endforeach
        </div>
    </div>
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#carouselProduct">Open Modal</button>
</div>

In this example, we've added a button with the data-bs-toggle and data-bs-target attributes set to modal and #carouselProduct, respectively. This will trigger the modal when the button is clicked.

We've also added a data-bs-image attribute to each gallery button, which will store the URL of the corresponding image. This will be used later to update the image in the modal when a gallery button is clicked.

Next, we need to add the necessary JavaScript to handle the gallery functionality. Here's an example of how you can do this:

<script>
    $(function() {
        // Update modal image when gallery button is clicked
        $('#gallery button').on('click', function() {
            var image = $(this).data('bs-image');
            $('#carouselProduct .carousel-item.active img').attr('src', image);
        });

        // Reset modal image when modal is closed
        $('#carouselProduct').on('hidden.bs.modal', function() {
            var image = $('#gallery button.active').data('bs-image');
            $('#carouselProduct .carousel-item.active img').attr('src', image);
        });
    });
</script>

In this example, we're using jQuery to handle the gallery functionality. We've added an event listener to each gallery button that updates the image in the modal when the button is clicked. We're also adding an event listener to the modal that resets the image to the original image when the modal is closed.

Note that you'll need to include jQuery in your project for this code to work.

Here's the final index.blade.php file with the added JavaScript:

<div class="col-lg-4 mb-3">
    <div id="carouselProduct" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-inner">
            @foreach($product->galleries as $gallery)
                <div class="carousel-item {{ $loop->first ? 'active' : '' }}">
                    <img id="zoom" src="{{ asset('storage/'.$gallery->image) }}" class="d-block w-100" alt="{{ $product->title }}">
                </div>
            @endforeach
        </div>
        <div id="gallery" class="carousel-indicators">
            @foreach($product->galleries->take(4) as $gallery)
                <button type="button" data-bs-target="#carouselProduct" data-bs-slide-to="{{ $loop->index }}" class="{{ $loop->first ? 'active' : '' }} thumbnail clearfix" aria-current="true" aria-label="{{ $product->title }}" data-bs-toggle="modal" data-bs-image="{{ asset('storage/'.$gallery->image) }}">
                    <img src="{{ asset('storage/'.$gallery->image) }}" data-zoom-image="{{ asset('storage/'.$gallery->image) }}" class="d-block w-100" alt="{{ $product->title }}">
                </button>
            @endforeach
        </div>
    </div>
    <button type="button" class="btn btn-primary

Please or to participate in this conversation.