L
L
Lev K2016-05-25 19:27:57
Yii
Lev K, 2016-05-25 19:27:57

How to adjust the size of the modal window to the image (picture) in yii2?

Hello.
When you click on the image, a modal window "with full image" appears.
I did it like this

<!--модальное окно для изображений -->
    <div class="modal fade" id="my-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-body">
                </div>
            </div>
        </div>
    </div>
Событие, нажатие на картинку
$('body').on('click', '.image', function (e) {
        var modalContainer = $('#my-modal');
        var modalBody = modalContainer.find('.modal-body');
        modalContainer.modal({show:true});
        var url = $(this).attr('src');
        $.ajax({
            url: 'modalimage', 
            type: 'GET',
            data: 'url='+url,
            success: function (data) {
                $('.modal-body').html(data);
                modalContainer.modal({show:true});
            },
            error: function(){
                alert('Error in modal Image');
            }
        });        
    });
Файл, который рендерится в модальное окно
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>

<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<div id="success"></div> <!-- For success message -->

<div class="gb-user-form">

    <?php $form = ActiveForm::begin(['options' => ['class' => 'modal-form']]); ?>

    <?= Html::img(Url::to([$url]), ['class' => 'img-modal']) ?>

    <?php ActiveForm::end(); ?>

</div>

It is necessary that, if the modal window is larger than the image, it should be adjusted to fit its dimensions, if the image is larger, it should shrink to the size of the modal window. something like this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sts, 2016-05-25
@stunoff

https://stackoverflow.com/questions/16152275/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question