2

Silly question 4 most, I guess, I'm trying to get my last li, which has an image for the background (a href) to look as if its a blinking image, changing from one colour to the next, its really just moving the background image from one position to the next, this is already working on hover using css, but id like to use jquery to animate this button to emulate the blinking effect...

I have my HTML code as follows..

<ul>
   <li id="view"><a href="#">One</a></li>
   <li id="contact"><a href="#">Two</a></li>
   <li id="checkit"><a href="#">Three</a></li>
</ul>

Ive created 2 extra classes in the css called on and off, which i though I could use in jquery to animate the a href,

#header ul li#checkit a.off{background-position: -352px 0;}
#header ul li#checkit a.on{background-position: -352px -73px;}

whats the best or easyiest way to either change the background every 0.5 sec or change the css from on to off to replicate that effect...

$(document).ready(function() {
       //$('#header ul li#checkit').addClass('on');
       //$('#header ul li#checkit').addClass('off');

       $('#header ul li#checkit').animate({ ? }, 150);
});

thanks in advance Marty.

3
  • Why don't you just create an animated gif image and put it as background? Would spare you alot of code :)
    – easwee
    Commented Sep 17, 2010 at 10:04
  • Why don't you go back and accept some awnsers. Then i could help you :)
    – meo
    Commented Sep 17, 2010 at 10:06
  • Hi guys thanks for all the responses so far, I only posted the question about 15mins ago, made a cuppa and came back to check, didnt realise there would be this many answers, I did try and make the animated gif, although the background remains white for the background even though transparency has been set so didnt work for that, im given a few of these a try and ill report back in 10 mins or so, thanks again guys ;)
    – Marty
    Commented Sep 17, 2010 at 10:20

4 Answers 4

3

Use the toggleClass() method which allows for multiple classes to be toggled

setInterval(
    function(){
         $('#header ul li#checkit').toggleClass('on off');
    },500
   );

You will need to first set one of the two classes on the li.. (or you can add it with jquery as you have in comments in your code $('#header ul li#checkit').addClass('on');)

3
  • This almost works, it doesn't have a class initially though, so that'll need to be applied otherwise he'll get the .on styling, since it's defined last in the CSS. Commented Sep 17, 2010 at 10:14
  • @Nick, well.. i did mention that he would nee to set one of the two classes on the element first.. i will add a line there to do it through jquery as well.. Commented Sep 17, 2010 at 10:20
  • @nick, i am pretty sure i had the mention in, the first time (i only added the parenthesis at edit..).. but then again it would not be the first time my memory fails me .. :) Commented Sep 17, 2010 at 11:00
1

Use setInterval:

setInterval(function()
{
    // Get the elements and toggle classes
    $('#header ul li#checkit a').toggleClass('on');
}, 500);

You wouldn't need an 'off' class, you could just style the 'on' differently.

I did notice that you were trying to change the class of the li, but setting it on the anchor in CSS. Add or remove that a depending on which element you want

0

Use toggle function - http://api.jquery.com/toggle/

$('#target').toggle(function() {
  $('#header ul li#checkit').addClass('on').removeClass('off');
}, function() {
  $('#header ul li#checkit').removeClass('on').addClass('off');
});
1
  • This did look almost what I was looking for, although there dosnt seem to be any animation when trying it out, do you think it would need a trigger of some sort before the toggle is fired off?
    – Marty
    Commented Sep 17, 2010 at 10:28
0

Thanks to Gaby

$(document).ready(function() {

   setInterval(
    function(){
        $('#checkit a').toggleClass('on');
    },500
   );

});

Not the answer you're looking for? Browse other questions tagged or ask your own question.