Solved

Sticky Sidebar

Posted on 2014-10-10
6
162 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
ID: 40374138
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
ID: 40374150
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
ID: 40374160
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Refael
ID: 40374163
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
ID: 40374170
$(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
ID: 40374195
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
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)

820 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