Solved

Sticky Sidebar

Posted on 2014-10-10
6
154 Views
Last Modified: 2014-10-10
Hello Experts,

Sticky sidebar.....

The sidebar is on the right hand side. I am using the offset.top in order to stop the sidebar from scrolling when it reaches 300 pixels from the top. It works fine and everything is sweet :-)

The problem starts when the user resize the window.

When the user resize the window and the window width is smaller than 990 pixels I need to removed the "fixed" class. Also when the user resize the window and the window width is greater than 990 pixels I need to add the "fixed" class back.

For some reason when I resize the window below 990 pixels and I start to scroll down ...the "fixed" class is being applied though the the first condition in the jQuery code below should execute only if the window is greater than 990 pixels.

Can someone please have a look? code below.....

if ( $(window).width() > 990) { // only if the window is over 990 pixels
		var offset = $('.sticky').offset();
		var sidebarWidth = $(".sidebar").width(); // setting the sidebar width when fixed class applied
		$(window).scroll(function() {
			if(window.scrollY >= offset.top + 300) {
				$('.sticky').addClass('fixed').width(sidebarWidth);					
			} else {		
				$('.sticky').removeClass('fixed');
			}
		});
}
			
$(window).resize(function() {
	if ( $(window).width() < 990) { // only if the window is less than 990 pixels
		$('.sticky').removeClass("fixed"); // removing the fixed class
	} else {
		$('.sticky').addClass('fixed').width(sidebarWidth);
	}
});

Open in new window


<div class="sidebar>
     <div class="sticky">
      Sidebar content
    </div>
</div>

Open in new window

0
Comment
Question by:Refael
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
You're making it rather complicated

jQuery
$(window).scroll(function() {
    console.log(window.scrollY)
	if(window.scrollY >= 300) {
		$('.sticky').addClass('fixed')
	} else {		
		$('.sticky').removeClass('fixed');
	}
});

Open in new window


CSS
.fixed {
position:fixed;
top:0
}

Open in new window

0
 

Author Comment

by:Refael
Comment Utility
Gary, Hi

Where is the window width conditional statement? The class "fixed" should apply only when the window size is greater than 990 pixels.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
if ( $(window).width() > 990) {
$(window).scroll(function() {
    console.log(window.scrollY)
	if(window.scrollY >= 300) {
		$('.sticky').addClass('fixed')
	} else {		
		$('.sticky').removeClass('fixed');
	}
});
});

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Refael
Comment Utility
Gary, Thanks again....

Did you read my post? It looks like you are missing the issue. What happened the the user resize the window smaller than 990 pixels and then resize it back to a grater than 990 pixels?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
$(window).scroll(function() {
if ( $(window).width() > 990) {
	if(window.scrollY >= 300) {
		$('.sticky').addClass('fixed')
	} else {		
		$('.sticky').removeClass('fixed');
	}
    }
    else
{
$('.sticky').removeClass('fixed');
}
});

Open in new window

0
 

Author Closing Comment

by:Refael
Comment Utility
Thank you Gary!!!

So i should have used the window-width conduction after the scroll function!
I only made the code complicated because it did not work right :-) Yet, thank you for simplifying the code as well.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now