Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sticky Sidebar

Posted on 2014-10-10
6
Medium Priority
?
173 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

886 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