How show and hide an image using Javascript ?
-
I have 2 little divs with 2 little images, the idea is to click over one of this images in order to see the "normal version" of the same image in a third div (to see the image bigger). How in the name of hell and heaven I can do that ? Any idea ? Greatings and thanks
-
Answer:
You could try using a lightbox for example, they are quite popular with the web 2.0 crowd due to their ease of use and user friendlyness. http://www.huddletogether.com/projects/lightbox2/ If not, you could look into using jQuery for your own custom needs (look up the .focus() function here: http://api.jquery.com/focus/ for a starter)
renzocj at Yahoo! Answers Visit the source
Other answers
Let's make your life easier on writing javascript. We will use jquery library on this matter. First download the jquery library, its about 8kb and include it on your page. Here's how to include <script type="text/javascript" src="jquerylocation"></script> below this code we will create javascript to manipulate images. <script type="text/javascript"> $(document).ready(function(){ //#img1 is the id of your image $("#img1").click(function(){ $("this").attr("src", "srcofbiggerimage.jpg"); //this code replace the image into bigger image, note that srcofbiggerimage.jpg is the source of your image }); //to return to original image on mouse out $("#img1").mouseout(function(){ $("this").attr("src", "srcofsmallimage.jpg"); //this code replace the image into bigger image, note that srcofbiggerimage.jpg is the source of your image }); }); </script> There's a lot of stuff including clean animations a replacement technique you can use in Jquery. Just contact me, I can offer free training. I also offer web development services.
document.getElementById("your div's id").style.display = none; to hide it. Change none to inline to show it.
OK if I am correct you have something like the following HTML code on your page: <div><img src="largeImg1.jpg" id="largeImg"></div> <div><img src="thumbnail1.jpg" id="thumb1Img"></div> <div><img src="thumbnail2.jpg" id="thumb2.img"></div> There maybe more or less coding but that is the rudements of what you need a simple way to do this is to change it to: <div><img src="largeImg1.jpg" id="largeImg" /></div> <div><img src="thumbnail1.jpg" id="thumb1Img" onmouseover="document.getElementById( 'largeImg').src ='largeImg1.jpg'" /></div> <div><img src="thumbnail2.jpg" id="thumb2.img" onmouseover="document.getElementById( 'largeImg').src='largeImg2.jpg'" /></div> This will make the large image change when aa users mouse enters the smaller thumbnail. Also if the user doesn't have javascript enabled they will at least be able to see the large image of the first thumbnail. I would also advise to have an onload event preloading the images this can be added to the body tag as follows: <body onload="img1=new Image(); image1.src='largeImg1.jpg'; img2=new Image(); image2.src='largeImg2.jpg';"> The advantage of preloading is that one the mouseover the image will change instantly rather than have to download the image the first time it happens.
Related Q & A:
- How to uninstall Firefox add-on using javascript?Best solution by Stack Overflow
- How do I remove a site from IIS7 using JavaScript?Best solution by Server Fault
- How to connect to a Pervasive Database using javascript?Best solution by Stack Overflow
- How to upload image using Ajax request?Best solution by Stack Overflow
- How to get current YouTube video url using JavaScript?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.