javascript - Open different image in lightbox -
i have image light-box in bootstrap
, want achieve when user clicks on image, image pops different.
so have thumb icons, , when user clicks 1 of them full image displayed. code like:
<a href="full_image_url" ..> <img src="thumb_img_url" ...></a>
i tried edit <a href="#" ..>
location of other image didn't work..
var $lightbox = $('#lightbox'); $('[data-target="#lightbox"]').on('click', function(event) { var $img = $(this).find('img'), src = $img.attr('src'), alt = $img.attr('alt'), css = { 'maxwidth': $(window).width() - 100, 'maxheight': $(window).height() - 100 }; $lightbox.find('.close').addclass('hidden'); $lightbox.find('img').attr('src', src); $lightbox.find('img').attr('alt', alt); $lightbox.find('img').css(css); }); $lightbox.on('shown.bs.modal', function(e) { var $img = $lightbox.find('img'); $lightbox.find('.modal-dialog').css({ 'width': $img.width() }); $lightbox.find('.close').removeclass('hidden'); });
body { padding: 30px 0px; } #lightbox .modal-content { display: inline-block; text-align: center; } #lightbox .close { opacity: 1; color: rgb(255, 255, 255); background-color: rgb(25, 25, 25); padding: 5px 8px; border-radius: 30px; border: 2px solid rgb(255, 255, 255); position: absolute; top: -15px; right: -55px; z-index: 1032; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="col-xs-6 col-sm-3"> <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox"> <img src="https://s3.amazonaws.com/ooomf-com-files/lqcnpak3scm0bdyd5aa0_img_4060_1%20copy.jpg" alt="..."> </a> </div> <div class="col-xs-6 col-sm-3"> <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox"> <img src="https://s3.amazonaws.com/ooomf-com-files/z3lxxzfmre65fc3dmhnp_woody_unsplash_dsc0129.jpg" alt="..."> </a> </div> </div> <div id="lightbox" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mylargemodallabel" aria-hidden="true"> <div class="modal-dialog"> <button type="button" class="close hidden" data-dismiss="modal" aria-hidden="true">×</button> <div class="modal-content"> <div class="modal-body"> <img src="" alt="" /> </div> </div> </div> </div>
you have attached event handler click event of <a data-target="#lightbox'>
. can find parent div , next sibling div , image inside it. incase of last div can first sibling. this:
var $lightbox = $('#lightbox'); $('[data-target="#lightbox"]').on('click', function(event) { var imagediv = $(this).parent().is(':last-child') ? $(this).parent().siblings().first() : $(this).parent().next(); var $img = $(imagediv).find('img'), src = $img.attr('src'), alt = $img.attr('alt'), css = { 'maxwidth': $(window).width() - 100 , 'maxheight': $(window).height() - 100 }; $lightbox.find('.close').addclass('hidden'); $lightbox.find('img').attr('src', src); $lightbox.find('img').attr('alt', alt); }); $lightbox.on('shown.bs.modal', function(e) { var $img = $lightbox.find('img'); $lightbox.find('.modal-dialog').css({ 'width': $img.width() }); $lightbox.find('.close').removeclass('hidden'); });
body { padding: 30px 0px; } #lightbox .modal-content { display: inline-block; text-align: center; } #lightbox .close { opacity: 1; color: rgb(255, 255, 255); background-color: rgb(25, 25, 25); padding: 5px 8px; border-radius: 30px; border: 2px solid rgb(255, 255, 255); position: absolute; top: -15px; right: -55px; z-index: 1032; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="col-xs-6 col-sm-3"> <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox"> <img src="https://s3.amazonaws.com/ooomf-com-files/lqcnpak3scm0bdyd5aa0_img_4060_1%20copy.jpg" alt="..."> </a> </div> <div class="col-xs-6 col-sm-3"> <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox"> <img src="https://s3.amazonaws.com/ooomf-com-files/z3lxxzfmre65fc3dmhnp_woody_unsplash_dsc0129.jpg" alt="..."> </a> </div> </div> <div id="lightbox" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mylargemodallabel" aria-hidden="true"> <div class="modal-dialog"> <button type="button" class="close hidden" data-dismiss="modal" aria-hidden="true">×</button> <div class="modal-content"> <div class="modal-body"> <img src="" alt="" /> </div> </div> </div> </div>
Comments
Post a Comment