Panoramic Photoviewer in Javascript
As a webdesigner you might have had this problem: “I’ve got a nice looking wide image, but I don’t want my visitors to scroll horizontally.”
A colleague showed me a new project he was working on. A large image appeared and I had the ability to drag and move the image around in a container. Not super efficient if you ask me, I still had to grab the image and move it around holding my mouse button. Can’t there be an easier way?
This article will show you my solution to the problem and maybe even a different approach on navigation.
Download
PhotoNav zip-archive
Example
Open the PhotoNav examples
The example page is located here:
http://www.gayadesign.com/scripts/photonav/
And the archive is downloadable here:
http://www.gayadesign.com/scripts/photonav/photonav.zip
What is it?
This photo container has been adjusted to “move” with the cursor. To achieve this I used Javascript and a library called Prototype. Prototype is also needed in order to run script.aculo.us. I also applied script.aculo.us to achieve the smooth moving picture.
What do we have and what do we need to do?
- Create a HTML layout for the picture to get in.
- Adjust the CSS.
- Make a call to PhotoNav in Javascript.
1. Create a HTML layout
This is the easiest step. It might just as well be a copy paste of the code below. But be aware of three things.
First give the overall container an id (so Javascript can grab the object).
Then make sure the div with classname fixed gets a second classname to correspond with the CSS (I used opt1 and opt2).
Determine if you want to have links inside of the container, if not; you can leave the container empty.
Take a look at the code below:
<div class='photo' style='display: none;' id='navigate'>
<div class='fixed opt1'>
<!-- this is optional --!>
<a href='http://gayadesign.com/post/' class='button1'>
</a>
<a href='http://gayadesign.com/portfolio/' class='button2'>
</a>
<a href='http://gayadesign.com/about/' class='button3'>
</a>
<a href='http://gayadesign.com/partners/' class='button4'>
</a>
<a href='http://gayadesign.com/contact/' class='button5'>
</a>
</div>
</div>
The overall container “navigate” will always be hidden prior to the actual Javascript call. I also gave the fixed div a second class. This classname will point to the options of the container; the height and the background image (the actual image that needs scrolling).
You can add a tags to the fixed container. I gave them all classnames so they can be positioned in the CSS. This part is optional.
Remember to include the Prototype library and the PhotoNav Javascript files in the header (and script.aculo.us if you want to enable smooth scrolling). Also include the CSS file photonav.css.
<script src="js/prototype.js" type="text/javascript"></script> <script src="js/scriptaculous.js" type="text/javascript"></script> <script src="js/photonav.js" type="text/javascript"></script> <link href='css/photonav.css' rel='stylesheet' type='text/css' />
2. Adjust the CSS
The top part of the CSS has to stay the way it is, do not adjust it if you don’t know what you are doing. I put a comment at the spot where you can start to edit the CSS.
I’ll describe what to do at each part of the CSS.
.photonav .photo:
.photonav .photo {
width: 400px;
margin: 10px;
border: 1px solid gray;
}
Adjust the width of the total container. This has to be a fixed width in order for it to work in IE6. I have no idea why, but if the width was set to 100%, the height was 0px. You can do 100% in other browser with the !important trick.
The margin and border are optional, just make it as you’d like.
.photonav .photo .opt1:
.photonav .photo .opt1 {
height: 100px;
background-image: url();
}
This is the height of the photo container, at least, the fixed container. But notice the .opt1, it is the second classname we added to the fixed container in the HTML part.
The background image is the image that has to be viewed inside the container.
The next part is optional and only if you want buttons inside the container.
a.button*:
a.button1 {
margin-left: 20px;
margin-top: 30px;
width: 100px;
height: 90px;
background-image: url();
}
a.button1:hover {
background-image: url();
}
In order to create a menu navigation like I did on the preview page, you need to add a tags with different classnames. Define the pixels from the left side and the top of the container in the margin values. State the width and height of the button and the background image (if needed).
To create an onhover effect you can add an other image in the :hover part.
If you want a second a tag next to the other, subtract the height of the previous button from the margin-top.
3. Make a call to PhotoNav in Javascript
Now that you’ve got all the necessary HTML and CSS set, you can call the PhotoNav functionality. The following code can be put in a js file or in a strict tag.
document.getElementById('navigate').style.display = 'block';
PhotoNav.init('navigate', 758, 1412, false, 0, false);
The specification is:
function init(String id_of_container, int width_of_container, int width_of_panorama_picture, bool smoothScrolling, float smoothLevel, bool debugMode);
String id_of_container:
The id of the container.
int width_of_container:
Give the width in pixels of the fixed container. For calculating reasons.
int width_of_panorama_picture:
Give the width in pixels of the panoramic picture. For calculating reasons.
bool smoothScrolling:
Enable script.aculo.us powered smooth scrolling (default: false)
float smoothLevel:
Level of smoothing, recommended to keep the default 0.1 (default: 0.1)
bool debugMode:
Enable debug mode. Outputting value to the HTML element with id ’status’. (default: false)
If you want to enable debug mode, you have to insert the following code somewhere in your HTML:
<div id='status'>
</div>
Now that everything is set, you can fire the thing up! Enable the navigation on loading a page or by triggering an event in Javascript. If you have implementations of this script, post your examples in the comment section, I’d like to see them.
Happy developing! And have fun.
Articles like this one
31 Comments
-
Looks pretty sleek! Although the “menu” example is pretty hard and not really user friendly, the “panorama viewer” on the other hand is.
Well done!
-
I kinda created the menu example for other purposes. Like scrolling through a picture and making parts clickable.
Thnx for the compliment
-
This is not really a panaromic viewer. This is quite good I must say, but its not what a panaromic view. Will be nice if we can look up or down as in a 3-D view.
-
Wow – Well done Gaya, this article has ben feautured on NETTUTS:
http://nettuts.com/articles/web-roundups/best-of-the-web-january/Sweet :D!
-
@Abhijit:
Thank you. I was thinking about using the same technique but also enabling an up-and-done view. With this you can create a map viewer without scrolling, or even a point and click game. Will post if I have something new.@Marco:
Damn sweet! Let’s see what this brings =D
-
Wow, nice affect. I like the look of this, I don’t know if I would ever use it, but it is definately a nice feature, bookmarked.
-
This looks great!
Do you think that it would be difficult to “autopan” the image so that a user does not necessarily have to move the mouse to pan the image?
-
@Jiggy:
No not really… you’d have to to change the offset over time… that could work. (If you look in the source of the script, you can see I calculate percentages of movement)
-
hey do you think that I could use this to create a photo gallery ? Use the mouse to navigate through a series of thumbs from left to right?
Each picture would link to a separate html page?
-
@fiona:
Yeah, check out the menu example. but instead of menu items, you could use thumbs :)
-
It’s pretty cool!
I’m a japanese panorama creator.
Photonav is so convenient to entry the weblog with too long cylinderical panoramic image.
I have translated japasese in this way on my weblog.
I hope that a lot of Japanese use for this program.
-
Hey Akila,
Be sure to point back to this post =)
Thank you for the complement!
Oh, if you could mail the address of you blog to me, that would be nice =D. (Oops, already found it =P nvm)
-
When I try to download the zip file, it continues to tell me that it is corrupted. Can you email me the zip file please.
-
Hey Scott,
I tried to e-mail you, but it didn’t work (it bounced back).
You should try and open the file in WinRAR, should work just fine.
Good luck
-
nice piece of code
-
Wow very nice.. I love your blog design as well. Good work!
-
Thanks for the nice comments =)
Be sure to subscibe to the RSS for more
-
Thanks! Nice work.
Note that the html example layout doesn’t work as is. It misses a toplevel div with class/id ‘photonav’. Nothing is happening without it.
-
@Sjaak
Ahh, thanks. Haven’t noticed that yet! Thanks for pointing that out.
Cheers!
-
Hey Gaya,
I tried to add the PhotoNav effect to more than one element of a page but that doesn’t work. The efffect works only for the last element.I think I understand why this happens, but I don’t know how solve that and “multiply” Photonav for several elements without blowing up the code like that: var PhotoNav1 = { ……. } var PhotoNav2 = { …… }
Do you know what I mean?
Thank you for help!
Christian
-
I understand what you mean. The thing is that once you load the Javascript file it creates only one instance of PhotoNav.
You could copy it, but that would be weird. That’s why I think converting it to a class would be better. Try this page: http://www.prototypejs.org/learn/class-inheritance
-
Thanks mate for a nother brilliant article. Btw: nice blog design, unique and kinky :)
-
interesting post, will come back here, bookmarked your site
-
As a total noob I managed to create a panorama viewer with your photoviewer. Works pretty cool 8)
Thank you!
-
I’d love to use this. I’m a real beginner and your instructions were so complete till the very end – the java script part. When you wrote that document.getElementById(‘navigate’).style.display = ‘block’;
PhotoNav.init(‘navigate’, 758, 1412, false, 0, false);
needs to go in a java script document, do you mean a new one? Because I added it to photonav.js and nothing is running and I don’t know if I have a mistake in doing that or somewhere else.
-
i need to move the background image both top and bottom.
But it moves only left and right… Please give me the script document for how to work the background image moves top and down.
-
Thank you for the Script, I used it here: http://www.laafi.at/projekte/kleinspitaeler/beun/
It would have saved me lots of time though if you had corrected the error (missing div id=photonav class=photonav), as Sjaak has pointed out last year.
-
Hello, I would like to add this to an interactive floorplan that I create in css.
http://www.realestateshooters.com/floorplans/205eastsaddleriver/
I would like to have it work like the still images were when you roll over them they open and then scroll with your code.
Thanks.
-
I have been trying all night to get this to work. Like Meryl, I guess I am getting stuck on Step 3. Where do we place the script to call photonav and where do we put the missing div id and div class? Please Help
-
document.getElementById(‘navigate’).style.display = ‘block’;
PhotoNav.init(‘navigate’, 400, 2313, false, 0, false);
-
Can’t seem to paste the code here.
document.getElementById(‘navigate’).style.display = ‘block’;
PhotoNav.init(‘navigate’, 400, 2313, false, 0, false);


Recent comments