views:

275

answers:

4

Hi,

I have 3 images that I want to rotate when a button is clicked.

image1, image2, image3.

If the image is at image1, then when clicked it should show image2 (and so on, in order of image1, .., image3).

When I am at image3, it should then hide the image, i.e. don't display it.

I need some help with the javascript function to do this, I already have the code for the button click event.

I am passing the toggle() function the jquery object $('myImageID');

$(document).ready(
    function() 
    {

        $('#button1').click( function() { toggleSector( $('#sector1') ) }  ;            

    } 
);

function toggleSector(o)
    {     
         // help!
    }

<div id="sector1"></div>
<input type="button" id="button1" value="Sector 1" />

Update

I have to somehow find the name of the current background image set to the <div> where my image is.

Is there a background property to get the image name currently being displayed?

+2  A: 

You can get a background-image by accessing it from the .css(name) method:

$("#sector1").css("background-image");

Without managing your list of images in an array or some other fashion, you're going to have to check each background-image to know when it's time to hide your element. This isn't a great way of working, as it doesn't allow you to easily add a new image in the future if you like.

Perhaps something like the following:

function toggle(el) {
  var whenToHide = "background3.jpg";
  var currBackground = $(el).css("background-image");
  /* ...code... */
  if (currBackground == whenToHide) {
    $(el).remove();
  }
}
Jonathan Sampson
+2  A: 

Do you have to use the background image?
If not, here's a little code sample for what I would do.

<html>
<head>
<style type="text/css">
    #imageRotater { list-style-type:none; }
    #imageRotater, .imageRotater li { margin:0px auto; padding: 0px; }
    #imageRotater img { display:none; }
</style>

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
(function($) {
    $.fn.rotate = function() {
     return this.each(function() {
      var list = $(this).is('ul') ? $(this) : $('ul', this);
      list.find('img:eq(0)').show();

      $('img', list).click(function() {
       $(this).hide().closest('li').next().find('img').show();
      });
     });
    };
})(jQuery);


$(document).ready(function() {
    $("#imageRotater").rotate();
});

</script>

</head>
<body>
    <div id="sector1">
     <ul id="imageRotater">
      <li><img src="image1.png" alt="" /></li>
      <li><img src="image2.png" alt="" /></li>
      <li><img src="image3.png" alt="" /></li>
     </ul>
    </div>
</body>
</html>
bendewey
A: 

Here's a thing that works.
Each overlay is initially hidden with CSS. Each time your button is clicked, all the overlays are hidden, then one is revealed based on some data stored on the button. If the data reaches the max number overlays + 1, none are shown and the data is reset to 0.

Markup

<div id="container" style="background: yellow">
    <div class="overlay" style="background: red"></div>
    <div class="overlay" style="background: green"></div>
    <div class="overlay" style="background: blue"></div>
</div>

Style

div{
 width: 100px;
 height: 100px;

}
.overlay{
 position: absolute;
 top: 0;
 left: 0;
 display: none;
}
#container{
 position: relative;
}

Script

$(function() {
    var b = $('#button1');
    b.data('next', 0);
    b.data('max', $('.overlay').size()+1 );
    b.click( function( e ) {
        var next = $(this).data('next');
     var o = $('.overlay');
        o.hide();
        o.eq(next).show();
        next = (next+1) % $(this).data('max');
        $(this).data('next', next);
    });
});
meouw
A: 

In response to Bendeway's answer above, you'll need to insert before

list.find('img:eq(0)').show();

the following line:

list.find('img').hide();

This will hide all the images before it starts rotating through them.

KyokoHunter