Set Div Position to Fixed When Scrolled to Position - jQuery
Solution 1
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: ''});
}
}
});
Solution 2
replace:
.css({position: fixed, right: 490px});
with
.css({position: 'fixed', right: '490px'});
strings should be quoted!
![Daniel Harris](https://i.stack.imgur.com/u4g5T.jpg?s=256&g=1)
Daniel Harris
I'm currently a full stack developer with over 7 years of solid experience in full-stack web development. I have a good foundation in front-end and back-end development using the LAMP stack including relational databases, client-side and server-side scripting, and mobile-first development. My specialties consist of system administration to the development, optimization, and deployment of professional websites. I also convert designs into SEO-optimized and mobile-friendly websites. Since 2009, I've worked on many PHP/MySQL projects which included an event calendar system, custom content management systems, e-commerce shopping carts, customer data systems, order management systems, custom database management systems, and product/service management systems. I've also developed on over 20 websites. I develop websites for businesses, non-profits, and individuals. I have skills to make websites show on the first page of Google. For example, my website SpaceCoastSites.com has achieved 2,645 relevant long-tail keywords that ranked #1 on Google. I’m also skilled in using social media platforms to advertise and bring interest to businesses. Additionally, I can convert web designs from Photoshop into perfect fully-coded front-end pages. I'm also experienced in software documentation and can easily adapt to new software development environments and languages when needed.
Updated on July 09, 2022Comments
-
Daniel Harris almost 2 years
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?