how to popup image over the screen after clicking the image

混江龙づ霸主 提交于 2020-01-07 06:58:21

问题


In my code, there is a lot of image in a table. I want to click a photo & then the photo will popup in a large size over the screen. But I do not have much idea about javascript & jquery. I try to do this but the problem is it popup a box but don't show the picture. I know that there is a problm in script to show the picture. can u please tell me where is the wrong or give me a proper code for popup image after click.

my header link is below. is there need any other link to be added??

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

    <link rel="stylesheet" type="text/css" href="http://w2ui.com/src/w2ui-1.4.2.min.css" />
    <script type="text/javascript" src="http://w2ui.com/src/w2ui-1.4.2.min.js"></script>

here is html part:

<div>
    <td> <img class="btn" onclick="popup()" style="width:100px; height:100px; border-radius:4px;" src="upload/<?php echo $row['image'];?>"></img></td>
</div>

here is the javascript:

<script type="text/javascript">
    function popup() {
        w2popup.open({
            title: 'Image',
            body: '<div class="w2ui-centered"><img src="upload/<?php echo $row['image'];?>"></img></div>'
        });
    }
</script>

can anyone please help me how to show image in the pop up box. thanks in advance.


回答1:


Instead of the onclick attribute, you could assign a class popup_image to your img and attach a click handler when the DOM is ready.

I took the liberty to remove scripts and tags that were not neccessary to demonstrate the result.

$(document).ready(function() {

  $(".popup_image").on('click', function() {
    w2popup.open({
      title: 'Image',
      body: '<div class="w2ui-centered"><img src="' + $(this).attr('src') + '"></img></div>'
    });
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="http://w2ui.com/src/w2ui-1.4.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://w2ui.com/src/w2ui-1.4.2.min.css" />

<img class="btn popup_image" style="width:100px; height:100px; border-radius:4px;" src="http://lorempixel.com/g/200/200/"></img>



回答2:


Replace your jquery with below any try:

<script type="text/javascript">
    function popup() {
       var image = $(this).attr('src');
        w2popup.open({
            title: 'Image',
            body: '<div class="w2ui-centered"><img src="'+image+'"></img></div>'
        });
    }
</script>

I assume the images are displayed properly which you will click




回答3:


I would recommend using Colorbox by Jack Moore. It uses JQuery, and is very customizable.

Colorbox: http://www.jacklmoore.com/colorbox/

Demo: http://www.jacklmoore.com/colorbox/example1/

If you aren't opposed to using PHP, you also may want to check out UberGallery. It uses colorbox internally and can be used simply with

<?php include_once('UberGallery.php') UberGallery::init()->createGallery("path to dir","name of gallery"); ?>



来源:https://stackoverflow.com/questions/37826471/how-to-popup-image-over-the-screen-after-clicking-the-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!