{"id":1629,"date":"2019-07-11T00:37:55","date_gmt":"2019-07-10T16:37:55","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1629"},"modified":"2019-07-13T15:05:20","modified_gmt":"2019-07-13T07:05:20","slug":"how-to-make-a-portion-of-your-sidebar-sticky-as-you-scroll-till-the-footer-of-your-web-page-with-jquery-and-some-css-styles","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-make-a-portion-of-your-sidebar-sticky-as-you-scroll-till-the-footer-of-your-web-page-with-jquery-and-some-css-styles\/","title":{"rendered":"How to make a portion of your sidebar sticky as you scroll till the footer of your web page with jQuery and some CSS styles"},"content":{"rendered":"<p>You want a portion of your sidebar to stick as you scroll down your web page.<\/p>\n<p>When you reach the footer of your web page, you want that portion to stop just above the footer of your web page.<\/p>\n<p>In this case, you can use jQuery to help you achieve this behaviour.<\/p>\n<p>Given that, this post shows how to make a portion of your sidebar sticky as you scroll till the footer of your web page with jQuery and some CSS styles.<\/p>\n<h2>Setting up the HTML part of the equation<\/h2>\n<p>In order for jQuery to locate the sticky part of the content, define a HTML element that should contain the sticky content:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;div id=&quot;sticky-sidebar&quot;&gt;\r\n&lt;p&gt;The sticky content&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>In addition to that, define a HTML element just before the sticky portion. Given that, you should have HTML elements that look like the following:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;div id=&quot;before-sticky-sidebar&quot;&gt;\r\n&lt;p&gt;The non-sticky content&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n&lt;div id=&quot;sticky-sidebar&quot;&gt;\r\n&lt;p&gt;The sticky content&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Finally, define a HTML element that marks the section that the sticky portion stops being sticky. For example, we can define a footer section for such a purpose:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;div id=&quot;main-footer&quot;&gt;\r\n&lt;!-- Footer content --&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<h2>Setting up the CSS part of the equation<\/h2>\n<p>After setting up the HTML elements, define a style class to make the sticky portion stick.<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n#sticky-sidebar.stick {\r\n\tposition: fixed;\r\n\tmax-width: 350px;\r\n}\r\n<\/pre>\n<p>In the above code, we define a style class that will keep the sticky portion in place. In addition to that, we also apply the <code>max-width<\/code> property to keep children elements of the sticky portion to a maximum width. <\/p>\n<h2>The JavaScript \/ jQuery code to make a portion of your sidebar sticky as you scroll till the footer of your web page<\/h2>\n<p>Once we have defined the CSS style class, we are ready to include the jQuery codes within our web page. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction sticky_relocate() {\r\n\tvar window_top = jQuery(window).scrollTop();\r\n\tvar footer_top = jQuery(&quot;#main-footer&quot;).offset().top;\r\n\tvar div_top = jQuery('#before-sticky-sidebar').offset().top + jQuery('#before-sticky-sidebar').height();\r\n\tvar div_height = jQuery(&quot;#sticky-sidebar&quot;).height();\r\n\r\n\tvar padding = 20;  \r\n\r\n\tif (window_top + div_height &gt; footer_top - padding) {\r\n\t\tjQuery('#sticky-sidebar').css({top: (window_top + div_height - footer_top + padding) * -1});\r\n\t}\r\n\telse if (window_top &gt; div_top) {\r\n\t\tjQuery('#sticky-sidebar').addClass('stick');\r\n\t\tjQuery('#sticky-sidebar').css({top: 80});\r\n\t} else {\r\n\t\tjQuery('#sticky-sidebar').removeClass('stick');\r\n\t}\r\n}\r\n\r\njQuery(document).ready(function() {\r\n\tjQuery(window).scroll(sticky_relocate);\r\n\tsticky_relocate();\r\n});\r\n<\/pre>\n<p>So what does the jQuery \/ JavaScript code do?<\/p>\n<p>First, we define a function, <code>sticky_relocate<\/code>, that will be called whenever the user scrolls the web page.<\/p>\n<p>At the start of <code>sticky_relocate<\/code>, we first get some values for deciding the location of the sticky portion of the sidebar:<\/p>\n<ul>\n<li><code>window_top<\/code> that denotes which part of the web page that the current view is at.<\/li>\n<li><code>footer_top<\/code> that denotes the top of the footer.<\/li>\n<li><code>div_top<\/code> that denotes the bottom of the non sticky content. We take the bottom of the non sticky content so that we can put in content with varying height later.<\/li>\n<li><code>div_height<\/code> that denotes that height of the sticky content.<\/li>\n<\/ul>\n<p>Once we have these values, we define three condition blocks to tweak the position of the sticky content.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nif (window_top + div_height &gt; footer_top - padding) {\r\n\tjQuery('#sticky-sidebar').css({top: (window_top + div_height - footer_top + padding) * -1});\r\n}\r\n<\/pre>\n<p>checks whether the sticky content had gone past the footer. When the sticky content had gone past the footer, we position it just above the footer.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nelse if (window_top &gt; div_top) {\r\n\tjQuery('#sticky-sidebar').addClass('stick');\r\n\tjQuery('#sticky-sidebar').css({top: 80});\r\n} \r\n<\/pre>\n<p>checks whether the user had scrolled past the bottom of the non sticky content. When the condition holds true, we apply the style class to <code>#sticky-sidebar<\/code> to make it stick. In addition, we keep a space of 80 pixels at the top of the sticky content.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nelse {\r\n\tjQuery('#sticky-sidebar').removeClass('stick');\r\n}\r\n<\/pre>\n<p>runs when the user had not scrolled past the bottom of the non sticky content. In this case, the code attempts to remove the style class that make the sticky portion stick.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-qh\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-qh&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-qh&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1629&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>You want a portion of your sidebar to stick as you scroll down your web page.<\/p>\n<p>When you reach the footer of your web page, you want that portion to stop just above the footer of your web page.<\/p>\n<p>In this case, you can use jQuery to help you achieve this behaviour.<\/p>\n<p>Given that, this post shows how to make a portion of your sidebar sticky as you scroll till the footer of your web page with jQuery and some CSS styles.<\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[17,16,128,89],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-qh","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1629"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1629"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}