943,104 Members | Top Members by Rank

Ad:
May 5th, 2009
0

Moving div Background Position on Click

Expand Post »
I have a product image that is 4 images 'sewn together'
http://www.flickr.com/photos/28033561@N03/3504394261/

I am going to set a div called ProductImage and then have the background of the div set to this image, but with only the top image showing.

I then want to have a button for 'more views' and when this is clicked the background position shift to show the next part of the image showing a different view.

What is a suitable way to do this, I am hoping it could be done with a combination of css and javascript.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
millsy007 is offline Offline
28 posts
since Feb 2009
May 5th, 2009
0

Re: Moving div Background Position on Click

Millsy,
You just reinvented something called a Sprite!

Put your adidas shoes image (shoes.jpg) in a folder named "images", then save the following code as eg. "myProduct.html", then browse it in your browser.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled</title>
  5. <style type="text/css">
  6. #productImg div {
  7. margin:6px 0;
  8. width : 241px;
  9. height : 125px;
  10. background : no-repeat;
  11. background-image : url('images/shoes.jpg');
  12. }
  13. #productImg .img0 { background-position : left 0; }
  14. #productImg .img1 { background-position : left -125px; }
  15. #productImg .img2 { background-position : left -250px; }
  16. #productImg .img3 { background-position : left -375px; }
  17. </style>
  18. <script type="text/javascript">
  19. function rotateImg(){
  20. var div = document.getElementById('productImg').firstChild;
  21. var number_of_images = 4;
  22. div.className = 'img' + (++rotateImg.seq % number_of_images);
  23. return false;
  24. }
  25. rotateImg.seq = 0;
  26. </script>
  27. </head>
  28. <body>
  29. <div id="productImg">
  30. <div class="img0">&nbsp;</div>
  31. <a href="" onclick="return rotateImg();">Next Image (as a link)</a><br />
  32. <input type="button" onclick="return rotateImg();" value="Next Image (as a button)">
  33. </div>
  34. </body>
  35. </html>
You can style the button/link as you wish in CSS.

Enjoy

Airshow
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,519 posts
since Apr 2009
May 5th, 2009
0

Re: Moving div Background Position on Click

Doh! Problems in Firefox. Maybe someone knows the workaround. I'll have another look at it tomorrow.

Airshow
Last edited by Airshow; May 5th, 2009 at 10:39 pm.
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,519 posts
since Apr 2009
May 6th, 2009
0

Re: Moving div Background Position on Click

OK, this version has been tested in IE6, Firefox 3.0.10, Opera 9.01 (all under Win2000).

We use a constructor to build the DOM elements within a nominated DIV. Thus the code can be reused to easily incorporate two or more sprites on the same page with very minimal additional HTML and javascript.

For flexibility, we also compute background position at each rotation rather than rely on predefined CSS rules.

Save image and html file as per version 1 above.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <title>Sprites</title>
  5. <style type="text/css">
  6. .spriteControl { font-size:10pt; color:#333333; text-decoration:none; }
  7. .spriteControl:hover { color:#339933;}
  8. </style>
  9. <script type="text/javascript">
  10. function Sprite(elementId, width, height, spriteURL, numImages, seq, controlTxt){
  11. /*
  12.   * Parameters
  13.   * elementId : The id of an empty HTML div into which the sprite will be inserted.
  14.   * width : the width of the sprite
  15.   * height : the height of one frame of the sprite (all frames must be of the same height)
  16.   * spriteURL : the URL of the sprite image (ie vertically abutting images in one jpg/gif/png)
  17.   * numImages : the number of individual images in the sprite. Note: this may be less than the total number of images in the sprite; any excess miages will not be shown.
  18.   * seq : the image at which the sprite will start (first image is at the top of the sprite and is numbered 0)
  19.   * controlTxt : The text which appears as a clickable control link below the sprite.
  20.   */
  21. seq = (!seq) ? 0 : (seq % numImages);
  22. controlTxt = (!controlTxt) ? 'Next' : controlTxt;
  23. var div = (document.getElementById) ? document.getElementById(elementId) : document.all[elementId];
  24. var innerDiv = document.createElement('div');
  25. var control = document.createElement('a');
  26. var txt = document.createTextNode(controlTxt);
  27. div.appendChild(innerDiv);
  28. control.className = 'spriteControl';
  29. control.setAttribute('href', '');
  30. control.appendChild(txt);
  31. div.appendChild(control);
  32. innerDiv.style.width = width + 'px';
  33. innerDiv.style.height = height + 'px';
  34. innerDiv.style.backgroundImage = ["url('" , spriteURL , "')"].join('');
  35. innerDiv.style.backgroundRepeat = 'no-repeat';
  36. var setBackground = function(){
  37. var posY = -height * (seq % numImages);
  38. innerDiv.style.backgroundPosition = ['0% ' , (posY+'px')].join('');
  39. };
  40. this.rotate = function(){
  41. seq++;
  42. setBackground();
  43. return false;
  44. };
  45. setBackground();
  46. control.onclick = this.rotate;
  47. }
  48. var sprites = [];
  49. onload = function(){
  50. sprites.push(new Sprite('productImg', 241, 125, 'images/shoes.jpg', 4, 0, 'Rotate'));
  51. sprites.push(new Sprite('productImg2', 241, 125, 'images/shoes.jpg', 3, 2, 'Rotate'));
  52. };
  53. </script>
  54. </head>
  55. <body>
  56.  
  57. <div id="productImg"></div>
  58. <div id="productImg2"></div>
  59.  
  60. </body>
  61. </html>

Airshow
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,519 posts
since Apr 2009
May 11th, 2009
0

Re: Moving div Background Position on Click

Thanks guys thats awesome
Reputation Points: 10
Solved Threads: 0
Light Poster
millsy007 is offline Offline
28 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: make div appear when you click a link
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: opinions please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC