Solved

Sticky Sidebar

Posted on 2014-10-10
6
160 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

777 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