Suppose you want to create a digital photo album to display a group of pictures, but you don't want the page to be too large. One solution would be to display only one full size picture, and the other pictures as small thumbnail images.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Easy JavaScript Picture Selector Code

Suppose you want to create a digital photo album to display a group of pictures, but you don't want the page to be too large. One solution would be to display only one full size picture, and the other pictures as small thumbnail images. When the user clicks on one of the thumbnail images, the full size picture is replaced by a full size version of the thumbnail that they clicked on.

Click to see scene 1 Click to see scene 2
Click to see scene 3 Click to see scene 4

(In case you're wondering - it's southern Utah)

To make this happen you need the incredibly small JavaScript function shown below. Just paste this code in the HEAD section of your Web page.

<script language="JavaScript">
function selectPicture(name)
{
   document.images.bigPicture.src = name;
}
</script>

The argument "name" passed to the function is the name attribute you give to the html img tag for the full size picture, as shown below.

<img name="bigPicture" src="scene1.jpg">

In the onClick event of each thumbnail image tag, call the selectPicture function, passing it the filename of the respective fullsize picture, as shown below.

<img onClick="selectPicture('scene2.jpg')"
src="thumb2.jpg">

That's all there is to it! But let's consider four minor items.

Image Size - When ever you use image replacement, you should keep all the images the same size. If you don't, your Web page may grow and shrink as each picture is replaced, messing up the layout of your page.

You can always make the images DISPLAY all the same size by adding height and width attributes to the image tag. But then if an image is smaller, the browser will automatically expand the image, making it appear blurry.

If an image is larger than what is specified in the height and width attributes, the browser will automatically shrink the image. In fact, you could let the browser shrink the full size image files to create the thumbnail images. But the Web page will take longer to load the large image files.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269