0

I have this piece of code and I have a simple question.

$(window).scroll(function() {
var y_scroll_pos = window.pageYOffset;
var scroll_pos_test = 150;             // set to whatever you want it to be

if(y_scroll_pos > scroll_pos_test) {
    $( "#cssmenu" ).addClass("extend");
} else if(y_scroll_pos < scroll_pos_test) {
    $( "#cssmenu" ).addClass("contract");
}
});

I have linked a CSS file ( via href="css/style.css") and I would like to use .extend and .contract from such file.

Is this possible?

Thank you.

2
  • Yes, it is. Have you tried it? What happens? What do those classes do? Commented Sep 20, 2013 at 22:59
  • jsfiddle.net/5uv5U/1 I am trying this. I figured it out. Yes, it works. Thank you. Commented Sep 20, 2013 at 23:02

3 Answers 3

3

Yes. Just include the CSS file in your HTML.

<head>
   <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

You will also need to call .removeClass() to remove the extend and contract class when adding the other one.

See this jsFiddle

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

Comments

2

A little correction

$(window).scroll(function () {
    var y_scroll_pos = window.pageYOffset;
    var scroll_pos_test = 150;             // set to whatever you want it to be

    if (y_scroll_pos > scroll_pos_test) {
        $("#cssmenu").addClass("extend");
        $("#cssmenu").removeClass("contract");
    } else if (y_scroll_pos < scroll_pos_test) {
        $("#cssmenu").addClass("contract");
        $("#cssmenu").removeClass("extend");
    }
});

Comments

1

Yes, of course it is. addClass is just a jquery method to add a class='' to any dom element. It doesnt create the class itself.

Comments

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.