简体   繁体   English

单击以在弹出框中显示图像

[英]onclick to show an image in pop-up box

I made a simple gallery, using this script to show the images: 我制作了一个简单的画廊,使用此脚本显示图像:

 $rs = mysql_query('SELECT * FROM images WHERE oferta_id=2');


 while($row = mysql_fetch_array($rs))
  {
        echo '<li onclick="Large()"><img src='.$row['location'].' alt="image"/></li>';

  }

and I want to use javascript in a way that when an user click on a certain picture a box pops-up and he sees the picture larger.I think I'm on a right way using onclick in the <li> tag, but have almost no idea how to make the Large() function.Any help on the topic? 我想以一种方式使用javascript,当用户单击某个图片时,会弹出一个框,并且他看到的图片更大。我认为使用<li>标记中的onclick是正确的方式,但是几乎不知道如何制作Large()函数。有关该主题的任何帮助吗?

Thanks 谢谢

Leron 勒伦

You could give each < img > an incrementing numeric id, and be passing that to the Large function, ie 您可以给每个<img>一个递增的数字id,并将其传递给Large函数,即

onclick="Large(0)" 

and each image would be 每个图像将是

<img id="image_0" .... />

so that you can get a unique reference to that particular image within the large function. 这样您就可以在大型函数中获得对该特定图像的唯一引用。

ie

function Large(index){
  var image = document.getElementById('image_' + index);
  //your code here
};

or as has already been suggested, you can use lightbox. 或已建议,您可以使用灯箱。

Also, another option is to just add the onclick handler the image and pass this, and then you have a direct reference to the image as well, and cut out the middle man, as it were. 另外,另一个选择是只添加onclick处理程序图像并传递该图像,然后您也可以直接引用该图像,并照原样切出中间人。 ie

<img onclick="Large(this) .... />

just some food for thought 只是一些思考

i'd just use a jquery plugin or another javascript framework's plugin. 我只是使用一个jQuery插件或另一个JavaScript框架的插件。 do a google search for "jquery lightbox" and see examples. 用谷歌搜索“ jquery lightbox”并查看示例。

there's definitely no point in writing your own unless you need some specific functionality not available out there or are looking to build your own as a learning exercise. 除非您需要一些尚不可用的特定功能或正在寻求构建自己的学习功能,否则绝对没有必要编写自己的功能。

I would suggest checking out the 100s of lightbox options for your larger image. 我建议您查看100多个灯箱选项,以获取更大的图像。 Just google lightbox. 只是谷歌灯箱。

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

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