如何制作缩略图在点击时显示完整尺寸的图像?

时间:2022-11-04 22:34:20

Normally you have an image tage with the src as the source of the image and the alt as the alternative text:

通常你有一个图像tage,src作为图像的来源,alt作为替代文本:

<img src="image1.gif" alt="Image 1" />

Can you have something like is?:

你能拥有类似的东西吗?:

<img src="image1.gif" alt="image2.gif" />

So that means the src has an image source and the alt also has an image source.

这意味着src有一个图像源,alt也有一个图像源。

What I want to do is pull 2 images from flickr using the flickr api (a thumb image and a normal sized image) and when the user clicks on the thumb, the normal sized image is shown.

我想要做的是使用flickr api(拇指图像和正常大小的图像)从flickr中提取2个图像,当用户点击拇指时,会显示正常大小的图像。

I have tried something like this:

我尝试过这样的事情:

<a href="image2.gif" ><img src="image1.gif"/></a>

...but I get the default 'not found' image as the thumb (even though the image does really exist).

...但我得到默认的“未找到”图像作为拇指(即使图像确实存在)。

I am using JQuery/ Javascript.

我正在使用JQuery / Javascript。

Any help, suggestions or even alternatives are all welcome and appreciated.

任何帮助,建议甚至替代品都欢迎和赞赏。

Thanks


Thanks for the reply Sam,

谢谢Sam的回复,

I don't like lightbox because I find it too slow and fancy.

我不喜欢灯箱,因为我觉得它太慢而且很花哨。

but I like geowa4's idea.

但我喜欢geowa4的想法。

but please, keep your answers coming...

但是,请保持你的答案......

thanks

9 个解决方案

#1


15  

The

<a href="image2.gif" ><img src="image1.gif"/></a>

technique has always worked for me. I used it to good effect in my Super Bowl diary, but I see that the scripts I used are broken. Once I get them fixed I will edit in the URL.

技术一直对我有用。我在超级碗日记中使用它效果很好,但是我看到我使用的脚本坏了。一旦我修复它们,我将在URL中进行编辑。

#2


10  

alt is text that is displayed when the image can't be loaded or the user's browser doesn't support images (e.g. readers for blind people).

alt是无法加载图像或用户浏览器不支持图像时显示的文本(例如盲人阅读器)。

Try using something like lightbox:

尝试使用灯箱之类的东西:

http://www.lokeshdhakar.com/projects/lightbox2/

update: This library maybe better as its based on jQuery which you have said your using http://leandrovieira.com/projects/jquery/lightbox/

更新:这个库可能更好,因为它基于jQuery,你已经说过你使用http://leandrovieira.com/projects/jquery/lightbox/

#3


8  

<img src='thumb.gif' onclick='this.src="full_size.gif"' />

Of course you can change the onclick event to load the image wherever you want.

当然,您可以更改onclick事件以在任何位置加载图像。

#4


1  

This won't do what you are expecting:

这不符合您的期望:

<img src="image1.gif" alt="image2.gif" />

The ALT attribute is text-only--it won't do anything special if you give it an image URL.

ALT属性是纯文本的 - 如果您为其提供图像URL,它将不会执行任何特殊操作。

If you want to initially display a low res image, then replace it with a high res image, you could do some javascript coding to swap out the images. Or, perhaps load the image into a div which has a background pattern filled with the low res image. Then, when the high res image loads, it'll load overtop the background.

如果你想最初显示低分辨率图像,然后用高分辨率图像替换它,你可以做一些javascript编码来换掉图像。或者,也许将图像加载到具有填充低分辨率图像的背景图案的div中。然后,当高分辨率图像加载时,它将加载到背景之外。

Unfortunately, there's no direct way to do this.

不幸的是,没有直接的方法来做到这一点。

Your second attempt will create a link to image2, but actually display image1.

您的第二次尝试将创建指向image2的链接,但实际上会显示image1。

<a href="image2.gif" ><img src="image1.gif"/></a>

If you want to popup a higher res version, @Sam's suggestion is a good idea.

如果你想弹出一个更高的res版本,@ Sam的建议是个好主意。


This CSS might work for you (it works for me in Firefox 3):

这个CSS可能适合你(它在Firefox 3中适用于我):

<html>
<head>
    <style>
        .lowres { background-image: url('low-res.png');}
    </style>
</head>
<body>
    <div class="lowres" style="height:500px; width:500px">
        <img src="hi-res.png" />
    </div>
</body>
</html>

In that example, you have to set the div height/width to that of the image. It will actually load both images simultaneously, but presuming the low-res one loads quick, you might see it first while the hi-res image downloads.

在该示例中,您必须将div高度/宽度设置为图像的高度/宽度。它实际上会同时加载两个图像,但假设低分辨率加载快速,你可能会在高分辨率图像下载时首先看到它。

#5


0  

I wouldn't do that - the alt tag is there specifically for when your image is not displayed. However, and this applies to a lot of jQuery stuff, the "rel" attribute is very useful.

我不这样做 - alt标签专门用于何时不显示图像。但是,这适用于很多jQuery的东西,“rel”属性非常有用。

#6


0  

That sort of functionality is going to require some Javascript, but it is probably possible just to use CSS (in browsers other than IE6&7).

这种功能需要一些Javascript,但可能只使用CSS(在IE6和7以外的浏览器中)。

#7


0  

A scriptless, CSS-only experimental approach with image pre-loadingBONUS! and which only works in Firefox:

一个无脚本,仅限CSS的实验方法,具有图像预加载功能!并且只适用于Firefox:

<style>
a.zoom .full { display: none; }
a.zoom:active .thumb { display: none; }
a.zoom:active .full { display: inline; }
</style>

<a class="zoom" href="#">
<img class="thumb" src="thumbnail.png"/>
<img class="full" src="fullsize.png"/>
</a>

Shows the thumbnail by default. Shows the full size image while the mouse button is clicked and held down. Goes back to the thumbnail as soon as the button is released. I'm in no way suggesting that this method be used; it's just a demo of a CSS-only approach that [very] partially solves the problem. With some z-index + relative position tweaks, it could work in other browsers too.

默认显示缩略图。单击并按住鼠标按钮时显示完整尺寸的图像。释放按钮后立即返回缩略图。我绝不建议使用这种方法;它只是一个仅使用CSS的方法的演示,[非常]部分地解决了这个问题。通过一些z-index +相对位置调整,它也可以在其他浏览器中使用。

#8


0  

Here is the Angular version of LightBox. Just Awesome :)

这是LightBox的Angular版本。真棒:)

Note : I have put this answer hence No Js library has been mentioned under the Tags.

注意:我已经提出了这个答案,因此在标签下提到了没有Js库。

angular-bootstrap-lightbox

<ul ng-controller="GalleryCtrl">
  <li ng-repeat="image in images">
    <a ng-click="openLightboxModal($index)">
      <img ng-src="{{image.thumbUrl}}" class="img-thumbnail">
    </a>
  </li>
</ul>

#9


0  

Use this as an example

以此为例

https://www.w3schools.com/jquerymobile/tryit.asp?filename=tryjqmob_popup_image

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
    <h1>Welcome To My Homepage</h1>
  </div>

  <div id="pageone" data-role="main" class="ui-content">
    <p>Click on the image to enlarge it.</p>
    <p>Notice that we have added a "back button" in the top right corner.</p>
    <a href="#myPopup" data-rel="popup" data-position-to="window">
    <img src="https://lh3.googleusercontent.com/8XQPMmZImaDsSyEPbAptXOHQYZKP_rggu_tr9EPl3jpPnuXOd5gGP5kQKqFT0ZLXQ36-=s136" alt="Skaret View" style="width:200px;"></a>

    <div data-role="popup" id="myPopup">
      <p>This is my picture!</p> 
      <a href="#pageone" data-rel="back" class="ui-btn ui-corner-all ui-shadow ui-btn-a ui-icon-delete ui-btn-icon-notext ui-btn-right">Close</a><img src="https://lh3.googleusercontent.com/8XQPMmZImaDsSyEPbAptXOHQYZKP_rggu_tr9EPl3jpPnuXOd5gGP5kQKqFT0ZLXQ36-=s136" style="width:800px;height:400px;" alt="Skaret View">
    </div>
  </div>

  <div data-role="footer">
    <h1>Footer Text</h1>
  </div>
</div> 

</body>
</html>

#1


15  

The

<a href="image2.gif" ><img src="image1.gif"/></a>

technique has always worked for me. I used it to good effect in my Super Bowl diary, but I see that the scripts I used are broken. Once I get them fixed I will edit in the URL.

技术一直对我有用。我在超级碗日记中使用它效果很好,但是我看到我使用的脚本坏了。一旦我修复它们,我将在URL中进行编辑。

#2


10  

alt is text that is displayed when the image can't be loaded or the user's browser doesn't support images (e.g. readers for blind people).

alt是无法加载图像或用户浏览器不支持图像时显示的文本(例如盲人阅读器)。

Try using something like lightbox:

尝试使用灯箱之类的东西:

http://www.lokeshdhakar.com/projects/lightbox2/

update: This library maybe better as its based on jQuery which you have said your using http://leandrovieira.com/projects/jquery/lightbox/

更新:这个库可能更好,因为它基于jQuery,你已经说过你使用http://leandrovieira.com/projects/jquery/lightbox/

#3


8  

<img src='thumb.gif' onclick='this.src="full_size.gif"' />

Of course you can change the onclick event to load the image wherever you want.

当然,您可以更改onclick事件以在任何位置加载图像。

#4


1  

This won't do what you are expecting:

这不符合您的期望:

<img src="image1.gif" alt="image2.gif" />

The ALT attribute is text-only--it won't do anything special if you give it an image URL.

ALT属性是纯文本的 - 如果您为其提供图像URL,它将不会执行任何特殊操作。

If you want to initially display a low res image, then replace it with a high res image, you could do some javascript coding to swap out the images. Or, perhaps load the image into a div which has a background pattern filled with the low res image. Then, when the high res image loads, it'll load overtop the background.

如果你想最初显示低分辨率图像,然后用高分辨率图像替换它,你可以做一些javascript编码来换掉图像。或者,也许将图像加载到具有填充低分辨率图像的背景图案的div中。然后,当高分辨率图像加载时,它将加载到背景之外。

Unfortunately, there's no direct way to do this.

不幸的是,没有直接的方法来做到这一点。

Your second attempt will create a link to image2, but actually display image1.

您的第二次尝试将创建指向image2的链接,但实际上会显示image1。

<a href="image2.gif" ><img src="image1.gif"/></a>

If you want to popup a higher res version, @Sam's suggestion is a good idea.

如果你想弹出一个更高的res版本,@ Sam的建议是个好主意。


This CSS might work for you (it works for me in Firefox 3):

这个CSS可能适合你(它在Firefox 3中适用于我):

<html>
<head>
    <style>
        .lowres { background-image: url('low-res.png');}
    </style>
</head>
<body>
    <div class="lowres" style="height:500px; width:500px">
        <img src="hi-res.png" />
    </div>
</body>
</html>

In that example, you have to set the div height/width to that of the image. It will actually load both images simultaneously, but presuming the low-res one loads quick, you might see it first while the hi-res image downloads.

在该示例中,您必须将div高度/宽度设置为图像的高度/宽度。它实际上会同时加载两个图像,但假设低分辨率加载快速,你可能会在高分辨率图像下载时首先看到它。

#5


0  

I wouldn't do that - the alt tag is there specifically for when your image is not displayed. However, and this applies to a lot of jQuery stuff, the "rel" attribute is very useful.

我不这样做 - alt标签专门用于何时不显示图像。但是,这适用于很多jQuery的东西,“rel”属性非常有用。

#6


0  

That sort of functionality is going to require some Javascript, but it is probably possible just to use CSS (in browsers other than IE6&7).

这种功能需要一些Javascript,但可能只使用CSS(在IE6和7以外的浏览器中)。

#7


0  

A scriptless, CSS-only experimental approach with image pre-loadingBONUS! and which only works in Firefox:

一个无脚本,仅限CSS的实验方法,具有图像预加载功能!并且只适用于Firefox:

<style>
a.zoom .full { display: none; }
a.zoom:active .thumb { display: none; }
a.zoom:active .full { display: inline; }
</style>

<a class="zoom" href="#">
<img class="thumb" src="thumbnail.png"/>
<img class="full" src="fullsize.png"/>
</a>

Shows the thumbnail by default. Shows the full size image while the mouse button is clicked and held down. Goes back to the thumbnail as soon as the button is released. I'm in no way suggesting that this method be used; it's just a demo of a CSS-only approach that [very] partially solves the problem. With some z-index + relative position tweaks, it could work in other browsers too.

默认显示缩略图。单击并按住鼠标按钮时显示完整尺寸的图像。释放按钮后立即返回缩略图。我绝不建议使用这种方法;它只是一个仅使用CSS的方法的演示,[非常]部分地解决了这个问题。通过一些z-index +相对位置调整,它也可以在其他浏览器中使用。

#8


0  

Here is the Angular version of LightBox. Just Awesome :)

这是LightBox的Angular版本。真棒:)

Note : I have put this answer hence No Js library has been mentioned under the Tags.

注意:我已经提出了这个答案,因此在标签下提到了没有Js库。

angular-bootstrap-lightbox

<ul ng-controller="GalleryCtrl">
  <li ng-repeat="image in images">
    <a ng-click="openLightboxModal($index)">
      <img ng-src="{{image.thumbUrl}}" class="img-thumbnail">
    </a>
  </li>
</ul>

#9


0  

Use this as an example

以此为例

https://www.w3schools.com/jquerymobile/tryit.asp?filename=tryjqmob_popup_image

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
    <h1>Welcome To My Homepage</h1>
  </div>

  <div id="pageone" data-role="main" class="ui-content">
    <p>Click on the image to enlarge it.</p>
    <p>Notice that we have added a "back button" in the top right corner.</p>
    <a href="#myPopup" data-rel="popup" data-position-to="window">
    <img src="https://lh3.googleusercontent.com/8XQPMmZImaDsSyEPbAptXOHQYZKP_rggu_tr9EPl3jpPnuXOd5gGP5kQKqFT0ZLXQ36-=s136" alt="Skaret View" style="width:200px;"></a>

    <div data-role="popup" id="myPopup">
      <p>This is my picture!</p> 
      <a href="#pageone" data-rel="back" class="ui-btn ui-corner-all ui-shadow ui-btn-a ui-icon-delete ui-btn-icon-notext ui-btn-right">Close</a><img src="https://lh3.googleusercontent.com/8XQPMmZImaDsSyEPbAptXOHQYZKP_rggu_tr9EPl3jpPnuXOd5gGP5kQKqFT0ZLXQ36-=s136" style="width:800px;height:400px;" alt="Skaret View">
    </div>
  </div>

  <div data-role="footer">
    <h1>Footer Text</h1>
  </div>
</div> 

</body>
</html>