简体   繁体   中英

show image onclick javascript

How can i load the original image when the tumbnail version of the image has been clicked? Im using ASP.NET in combinaton with javascript.

The original images are big, so they have been scaled on server side. This makes the site load faster. But somehow, both versions (original and tumbnail) of the images are being downloaded.

I'm trying to download only the tumbnail version of the image. And when the user clicks on the image, i want to show the original image.

How can i get this done?

Html such as below for each thumbnail image should do the trick

<a href="[url to original image]" target="_blank" id="thumbnail_link">
  <img src="[url to thumbnail image]" alt="Click to see the full image" />
</a>

Edit: Modified to illustrate use of FancyBox.

Use above markup along with below java-script:

$(document).ready(function() {
   $("a#thumbnail_link").fancybox();
})'

Don't forget to include jquery and fancybox js files.

HTML code:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title> - jsFiddle demo</title> 
  <script type='text/javascript' src='http://identify.site88.net/showimage.js'></script>
  <style type='text/css'>
       #test{
        display:none
        }
       #blackout {
       width:50%;
       position:absolute;
       background-color: rgba(0, 0, 0, .5);
       display: none;
       z-index: 20;
       }
       .modal {
       margin: auto;
       }
       #close {
       color: white;
       font-weight: bold;
       cursor: pointer;
       }   
  </style>
    <script type='text/javascript'>
    $(window).load(function(){
    $('img').click(function () {
        var img = $(this).clone().addClass('modal').appendTo($('#blackout'));
        $('#blackout > #close').click(function () {
            $('#blackout').fadeOut(function () {
                img.remove();
            });
        });
        $('#blackout').fadeIn();
    });
    });  

    $(window).load(function(){
    $('#close2').hide();
    $('span').click(function () {
        $('#test').show();
        $('#close2').show();
        $('#txtsp').hide();
        $('#blackout2 > #close2').click(function () {
            $('#blackout2').fadeOut(function () {
                $('#test').hide();
                $('#txtsp').show();
                $(this).css({
                    "text-decoration": ''
                });
            });
        });
        $('#blackout2').fadeIn();
    });
    });  
    </script>
</head>
<body>
    <div id="blackout2"><div id="close2" >Close</div></div><img id="test" src="http://data.vietinfo.eu/News//2012/10/16/179281/1350402084.7404.jpg"/> <span id="txtsp">Click here to show image</span>
    <br /><br />
    <div id="blackout"><div id="close">Close</div></div><div style="width: 50px; height: 50px;"><img width="100%" src="http://dantri.vcmedia.vn/Uploaded/2009/06/02/hh02066.jpg" /></div>
</body>
</html>

You can replace tag span by your image have been scaled on server side.

I think you have to show thumbnails first and on click you need to open the original images in a new pop up window. You can do this using code as given below -

<SCRIPT LANGUAGE="JavaScript">
function openImage(imageFile){
windowOpen=window.open("",'Open','toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=1,width=420,height=420');
windowOpen.document.writeln("<head><title>Image</title></head><body>");
windowOpen.document.writeln('<img src=http://www.mysite.com/' + imageFile + 'border=1>');
windowOpen.document.writeln("</body></html>");
}
</SCRIPT>

Then call this openImage() method during onClick of the thumbnail image. You can pass imageFile as parameter to the function.

It sounds like you have both images referenced in your HTML, even though one is hidden from view, so the browser requests both. What you'd need to do is use JavaScript to create the full size <img> tag from scratch and then add it to the relevant place in the HTML. The browser will then load the full size image once it's added to the DOM.

For fancy box, all you need to do is

<a id="single_image" href="image_big.jpg"><img src="image_small.jpg" alt=""/></a>

Regards, Andy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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