5

I am working with a right sidebar that contains three sliders aligned vertically. I would like to have the sidebar's position to be fixed when I scroll down to 200 pixels. Here's my code so far:

$(document).ready(function() { window.onscroll = function() { if (window.pageYOffset >= 200){ $('.col-right').css({position: 'fixed', right: '490px'}); } } } 

Nothing happens when I use this code. It detects that I am scrolling but it doesn't set the CSS properties to the "col-right" class, which is the sidebar. Am I doing this right?

2
  • 1
    Use quotes, "fixed", fixed is Commented Apr 9, 2013 at 17:49
  • Added and nothing happens. Commented Apr 9, 2013 at 18:14

2 Answers 2

6

OK, I figured it out. I changed $ to jQuery and everything works. Here is my working solution:

jQuery(document).ready(function(){ window.onscroll = function() { if (window.pageYOffset >= 200){ jQuery('.col-right').css({position: 'fixed', right: '490px', top: '40px'}); } else { jQuery('.col-right').css({position: '', right: '', top: ''}); } } }); 
Sign up to request clarification or add additional context in comments.

Comments

5

replace:

.css({position: fixed, right: 490px}); 

with

.css({position: 'fixed', right: '490px'}); 

strings should be quoted!

6 Comments

Did this, nothing happens.
Works just fine for me -> FIDDLE, did you remember document.ready, as you're using jQuery to get a DOM element.
Yes, I added the document.ready function to the code and still it does nothing.
And you're sure window.pageYOffset is incrementing when you scroll, and that you have an element with the class col-right ?
You could always see if the jQuery version works -> $(window).on('scroll', function() { ... });, but that's doubtful. Somehow the scroll event is'nt triggered.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.