0

I'm just beginning to learn jQuery and I'm trying to change the colour of a piece of text on my webpage when the user scrolls. the piece of text is contained within a div id. The name of the div I am using in my CSS sheet is #headerTitle.

Here is the code I have so far:

$(document).ready(function(){

    $(window).scroll(function(){

        $("#headerTitle").backgroundColor({
            transition:"background-color": "green"
         });
    }   
}
4
  • If you search the official jQuery documentation for backgroundColor, you will see that no such method exists. It's good that you tried something, but reading the documentation beforehand saves you from going in the wrong direction. You can change CSS via api.jquery.com/css and the property that controls the text color is color. Learn how to debug JavaScript. Commented May 26, 2014 at 22:56
  • Your questions title and description are talking about two different things. Title refers to text color, but your description is talking about the background color of a div. Can you clarify what you are needing? Commented May 26, 2014 at 22:57
  • @MattGreen Sorry about that I am trying to change the text colour, not the background colour. Commented May 26, 2014 at 23:00
  • Ah figured that was it, I updated my answer Commented May 26, 2014 at 23:06

1 Answer 1

2

This will do it:

   $(document).ready(function () {
    $(window).scroll(function () {
        alert('The Window Scrolled');
        // Set the color attribute on the div
        $('#headerTitle').css('color', 'red');
    });
   });

When the window scrolls you are 'alerted' (This makes sure it works, you can delete), then it will set the text color within your div using the jQuery CSS method.

Sign up to request clarification or add additional context in comments.

1 Comment

What does this refer to here? How exactly should the OP use it with the code he has?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.