最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how could I create a modal with a image in it and the link of the modal is the image itself (on a smaller version))

programmeradmin0浏览0评论

I want to show some image in a thumbnail gallery in which if you click an image then it will appear larger in a modal.

If i keep the link of the modal as an image then it does not show anything at header or footer other than the close sign in top right corner.

I am using bootstrap. I also want to know if i could replace the img tag to <a> tag to open the modal section. Please help.

Here is the code:

<div class="col-lg-3 col-md-4 col-xs-6 thumb">
        <img id="image-modal" class="img-responsive img-rounded" src="<?php echo base_url();?>img/Desert.jpg" data-target="#myModal" data-toggle="modal" alt="">
        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Modal Header</h4>
                    </div>
                    <div class="modal-body">
                        <img class="img-responsive" src="<?php echo base_url();?>img/Desert.jpg" alt="">
                    </div>
                    <div class="modal-footer">
                        <p>This is footer</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

I want to show some image in a thumbnail gallery in which if you click an image then it will appear larger in a modal.

If i keep the link of the modal as an image then it does not show anything at header or footer other than the close sign in top right corner.

I am using bootstrap. I also want to know if i could replace the img tag to <a> tag to open the modal section. Please help.

Here is the code:

<div class="col-lg-3 col-md-4 col-xs-6 thumb">
        <img id="image-modal" class="img-responsive img-rounded" src="<?php echo base_url();?>img/Desert.jpg" data-target="#myModal" data-toggle="modal" alt="">
        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Modal Header</h4>
                    </div>
                    <div class="modal-body">
                        <img class="img-responsive" src="<?php echo base_url();?>img/Desert.jpg" alt="">
                    </div>
                    <div class="modal-footer">
                        <p>This is footer</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

Share Improve this question edited Nov 16, 2015 at 17:05 Shehary 9,99010 gold badges46 silver badges75 bronze badges asked Nov 16, 2015 at 16:34 Erfan BasharErfan Bashar 1491 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Use modal event listener and pass the image to modal

Script

$(document).ready(function () {
    $('#myModal').on('show.bs.modal', function (e) {
        var img = $(e.relatedTarget).attr('src'); // get image
        $('#showimg').attr('src' , img); //load image in modal
    });
});

add id="showimg" to image tag <img> in Modal Body

<div class="modal-body">
    <img class="img-responsive" src="" alt="" id="showimg">
</div>

Fiddle with <img> tag

Yes you can also do it with <a> tag

<a> tag

<a class="btn btn-primary btn-xs" href="imagepath" data-target="#myModal" data-toggle="modal">Open Image</a>

and event listener script will be

$(document).ready(function () {
    $('#myModal').on('show.bs.modal', function (e) {
        var img = $(e.relatedTarget).attr('href'); // get image with <a> tag
        $('#showimg').attr('src' , img); //load image in modal
    });
});

Fiddle with <a> tag

You can do it with Bootstrap only, but this is what seems like it'd help for what you want to do: https://blueimp.github.io/Bootstrap-Image-Gallery/

I use it on a few websites of mine exactly for this case.

try this Bootstrap-Image-Gallery plugin will help you. check Demo.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论