{"id":215,"date":"2013-05-10T21:39:27","date_gmt":"2013-05-11T03:39:27","guid":{"rendered":"http:\/\/www.evermoretechnologies.com\/blog\/?p=215"},"modified":"2013-05-10T21:39:27","modified_gmt":"2013-05-11T03:39:27","slug":"attaching-a-side-div-to-the-top-of-a-window","status":"publish","type":"post","link":"https:\/\/www.evermoretechnologies.com\/blog\/2013\/05\/attaching-a-side-div-to-the-top-of-a-window\/","title":{"rendered":"Attaching a side div to the top of a window."},"content":{"rendered":"<p>I was working on a site and needed to have a side nav stick to the top once the header was scrolled off the screen. \u00a0Positioning it is easy enough on page load, but I wanted to move it with window scroll.<\/p>\n<p>First I generated the div with theside nav items in it. \u00a0Positioned it fixed with a right of 0 and a top of my header height of 200px;<\/p>\n<p>Then came the window scroll javascript. \u00a0The concept is pretty simple, so I was able to get it to work with this code:<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n(function($) {<br \/>\n\t$(document).ready( function() {<br \/>\n\t\tif ($(this).scrollTop() &lt; 200) {<br \/>\n            $(&quot;.side-nav&quot;).css({ &quot;position&quot;: &quot;fixed&quot;, &quot;top&quot;: 200 &#8211; $(this).scrollTop() + &quot;px&quot; });<br \/>\n        } else {<br \/>\n            $(&quot;.side-nav&quot;).css({ &quot;position&quot;: &quot;fixed&quot;, &quot;top&quot;: &quot;0px&quot; });<br \/>\n        }<\/p>\n<p>\t\t$(window).scroll(function() {<br \/>\n\t\t\tif ($(this).scrollTop() &lt; 200) {<br \/>\n\t\t\t\t$(&quot;.side-nav&quot;).css({ &quot;position&quot;: &quot;fixed&quot;, &quot;top&quot;: 200 &#8211; $(this).scrollTop() + &quot;px&quot; });<br \/>\n\t\t\t} else {<br \/>\n\t\t\t\t$(&quot;.side-nav&quot;).css({ &quot;position&quot;: &quot;fixed&quot;, &quot;top&quot;: &quot;0px&quot; });<br \/>\n\t\t\t}<br \/>\n\t\t});<br \/>\n\t});<br \/>\n})(jQuery);<br \/>\n[\/code]<\/p>\n<p>A flushed out example of this can be seen at:<br \/>\n<a href=\"http:\/\/jsfiddle.net\/evermoretech\/wChGd\/\" target=\"_blank\"> http:\/\/jsfiddle.net\/evermoretech\/wChGd\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a site and needed to have a side nav stick to the top once the header was scrolled off the screen. \u00a0Positioning it is easy enough&#8230; <a href=\"https:\/\/www.evermoretechnologies.com\/blog\/2013\/05\/attaching-a-side-div-to-the-top-of-a-window\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":0,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}