views:

94

answers:

2

I have a link (index.html#services) and a <div id="services> that I'm trying to change the background color of when the link is clicked and then fade back. I'm using the latest jQuery and jQuery Color plugin, and:

$(document).ready(function(){
    if(window.location.hash === '#services') {
        var service = $('#services');
        var originalColor = service.css('background-color');

        service.css('background-color', '#FFEE9F').animate({
            'background-color': originalColor
        }, 3000);
    }
});

to do the highlighting, but it's not working. Anyone know why?

+3  A: 

That code is only run when the page is loaded, not when a link with a hash is clicked. Try following the link (index.html#services) directly from a new browser tab and it will probably work. What you need to do is run that code when the hash is changed. New browsers have an onhashchange event - but no such thing on older browsers. For old browsers you could poll the hash property every so often to see if it has changed.

If by chance you have a specific identifier (css class, id, name, etc.) on the links that trigger that animation, you could add a click listener to run that code. For example:

function animateBackground() {
    var service = $('#services');
    var originalColor = service.css('background-color');

    service.css('background-color', '#FFEE9F').animate({
        'background-color': originalColor
    }, 3000);
}

$(function () { // shortcut to $(document.ready)
   $('.fade-bg').live('click', animateBackground);
   animateBackground();
});
CD Sanchez
Ah you were right, I tried refreshing the page from that link before... but it actually took opening a new tab and copy/pasting it. Thanks! How could I make it work whenever the link is clicked (it's part of my navigation.)
Matt Untsaiyi
Opps, replied before seeing your edited answer. Could you show me how that would be done? I'm new to JS, or would that be too much to ask?
Matt Untsaiyi
Again... you beat me to my response ha. So if I were to, per say, do `<a href="index.html#services" id="listen">Link</a>` the above code would work?
Matt Untsaiyi
@Matt: If you just wanted to copy and paste that JS code, your link would look like: `<a href="index.html#services" id="listen" class='fade-bg'>Link</a>`. Or, if you want it to work with the anchor you just pasted in that comment, you would change `$('.fade-bg')` to `$('#listen')`. Also, needless to say the code would go inside the ready function.
CD Sanchez
It works, tyvm. I'm going to try and understand it better now :).
Matt Untsaiyi
I found a bug. It works when you're *on* `index.html`, but on any other page it doesn't. Is there an easy fix for this?
Matt Untsaiyi
Never mind! I just added what I had before as a script too. I'm sure that's a bad way (and overly redundant), but it works. Now when a click is made, it works, and when the page is first loaded (from another page) it works too :).
Matt Untsaiyi
@Matt: Yeah, I should have mentioned that the other code needs to stay there. What you could do to reduce redundancy is put all that code in a function, and then run that function in the ready event, and assign that function for the click handler as well. I'll edit it into my answer.
CD Sanchez
A: 

Or use

window.onhashchange = function(){
    if(window.location.hash === '#services') {
        var service = $('#services');
        var originalColor = service.css('background-color');

        service.css('background-color', '#FFEE9F').animate({
            'background-color': originalColor
        }, 3000);
    }
};

depending on which browsers you target.

Ms2ger