1

I've found this code

$(window).scroll(function(){
    $('#header').css({
        //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to  remain at 15px left
        'left': $(this).scrollLeft() + 15  
    });
});

And it does exactly what I need, but I have no idea how to implement it in my HTML or CSS. Can anyone help me out? I'd truly appreciate it.

4
  • 2
    If it does what you need, where are you stuck? Commented Jul 5, 2012 at 15:36
  • Take a look at any page that includes Javascript to see how it's done? Commented Jul 5, 2012 at 15:36
  • 1
    This code requires jQuery, so you will need to include jQuery as well. Commented Jul 5, 2012 at 15:38
  • How do you know it does exactly what you need if you don't know how to run it? Commented Jul 5, 2012 at 15:39

1 Answer 1

3

That code uses jQuery, so you need to have included jQuery in the page.

Because it's JavaScript, you need to use a <script /> tag. You can add the code inline (which I'll do, as it's easier to explain), but you should look into adding it in a separate JS file and including it.

Add this in your HTML anywhere;

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <!-- Include jQuery -->
<script type="text/javascript">
    $(window).scroll(function(){
        $('#header').css({
            //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to  remain at 15px left
            'left': $(this).scrollLeft() + 15  
        });
    });
</script>

Normally you'd have to add a ready handler, but you don't if you're attaching to window, as it always exists.

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

1 Comment

Thank you so much, Perfect! I really appreciate your help. I didn't know how to activate jQuery... Everyone starts somewhere :)

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.