Solved

Sticky Sidebar

Posted on 2014-10-10
6
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

751 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