简体   繁体   English

图像弹出使用bootstrap Modal?

[英]Image pop up using bootstrap Modal?

I want to pop up image on clicking it. 我想在点击它时弹出图像。 Images are added by using advanced custom field. 使用高级自定义字段添加图像。 Here is my cod e 这是我的鳕鱼

<?php
 $args=array(
     'post_type' => 'map', 
);  
$staffs = new WP_Query( $args );

if( $staffs->have_posts() ) {
  while( $staffs->have_posts() ) {
    $staffs->the_post();
    ?>
     <div class="div_img">
        <div class="col-md-3">
            <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox"> 
               <h4 class="map_titles"><?php the_title();?></h4>
               <img src="<?php the_field('map_image'); ?>" />
            </a>
        </div>
    </div>
    <div class="map_modal">
        <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-header">
                    <h4 class="modal-title"><?php the_title();?></h4>
                  </div>
                    <div class="modal-body">
                       <img src="<?php the_field( 'map_image' ); ?>" />
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
  }?>

    <?php
};?> 

script 脚本

<script>
 jQuery(document).ready(function() {
    var $lightbox = jQuery('#lightbox');

    jQuery('[data-target="#lightbox"]').on('click', function(event) {
        var $img = jQuery(this).find('img'), 
            src = $img.attr('src'),
            alt = $img.attr('alt'),
            css = {
                'maxWidth': jQuery(window).width() - 100,
                'maxHeight': jQuery(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');
    });
});</script>

when I click on each image each image pop up nicely but the title remain same for all images. 当我点击每个图像时,每个图像都会很好地弹出,但所有图像的标题保持不变。 However my requirement is to show the title of each image with its image .Here is link 但是我的要求是用图像显示每个图像的标题。这是链接

Any help would be highly appreciated 任何帮助将受到高度赞赏

Hi I am not sure if I understand your question, however this is the simple way I will go about it. 嗨,我不确定我是否理解你的问题,但这是我将采取的简单方法。

1; 1; query database and get the image and the title use for each loop. 查询数据库并获取每个循环的图像和标题。

2,Add Data-dir attribute to the image tag and put the title inside like below. 2,将Data-dir属性添加到图像标签 ,然后将标题放入内部,如下所示。

<div class="div_img"> <div class="col-md-3"> <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox"> <h4 class="map_titles"><?php the_title();?></h4> <img src="<?php the_field('map_image'); ?> data-dir="<?php the_title();?>"/> </a> </div> </div>

3.When you grab the image on click using jquery I can see you are grabbing other attribute, grab the data-dir attribute as well like below, and using jquery .html() put the title in the modal-title. 3.当你使用jquery点击图片时我可以看到你正在抓取其他属性,抓住data-dir属性以及如下所示,并使用jquery .html()将标题放在模态标题中。

var title = $img.attr('data-dir'),
$('.modal-title').html(title);

Hopes this help if not let me know. 希望如果没有让我知道这一帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM