{"id":268,"date":"2021-03-22T19:20:22","date_gmt":"2021-03-22T19:20:22","guid":{"rendered":"https:\/\/wshop.fi\/eng\/?p=268"},"modified":"2021-03-22T19:27:41","modified_gmt":"2021-03-22T19:27:41","slug":"css-sticky-footer","status":"publish","type":"post","link":"https:\/\/wshop.fi\/eng\/css-sticky-footer\/","title":{"rendered":"\u201cSometimes\u201d Sticky CSS Element Using jQuery"},"content":{"rendered":"\n<p>There are several tutorials on how to make things \u201c<strong>sticky<\/strong>\u201d using CSS. We will make a \u201c<strong>sometimes sticky<\/strong>\u201d element using CSS and jQuery. Here is an article on&nbsp;<a href=\"http:\/\/www.surfingsuccess.com\/css\/css-sticky-footer\/\">CSS sticky footers<\/a>&nbsp;to help you understand before we move onto the \u201csometimes\u201d part using jQuery.<\/p>\n\n\n\n<p>Remember &#8211; &nbsp;<code>position: fixed;<\/code>&nbsp;in a CSS sticky element is in a fixed position&nbsp;<em>relative<\/em>&nbsp;to the element or \u201ccontainer\u201d it\u2019s in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CSS Sticky Class<\/h2>\n\n\n\n<p>The&nbsp;<strong>main points for this CSS sticky element<\/strong>&nbsp;are&nbsp;<code>position: fixed;<\/code>&nbsp;and&nbsp;<code>top: 75px;<\/code>&nbsp;Z-index helps keep it \u201con top\u201d when the user scrolls.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.sticky {\n    position: fixed;\n    top: 75px;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">HTML for Sometimes Sticky Element<\/h2>\n\n\n\n<p>This is just a general idea of the HTML needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;body&gt;\n   &lt;div id=\"wrapper\"&gt;\n      &lt;div id=\"leftSideBar\"&gt;\n         &lt;p&gt;lots of content&lt;\/p&gt; \n         &lt;div id=\"stickyChunk\"&gt;\n            &lt;p&gt;stuff&lt;\/p&gt;\n         &lt;\/div&gt;\n         &lt;p&gt;lots of content&lt;\/p&gt;\n      &lt;\/div&gt;  \n      &lt;div id=\"mainContent\"&gt; \n         &lt;p&gt;lots of content&lt;\/p&gt; \n      &lt;\/div&gt; \n   &lt;\/div&gt;\n&lt;\/body&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery for Sometimes Sticky Element<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$(document).ready(function() {\n \n    var stickyNavTop = $('#stickyChunk').offset().top;  \n \n    var stickyNav = function(){  \n        var scrollTop = $(window).scrollTop();\n \n        if (scrollTop &gt; stickyNavTop) \n            {\n                $('#stickyChunk').addClass('sticky');\n            }\n        else \n            {\n                $('#stickyChunk').removeClass('sticky');   \n            }  \n    };  \n \n    stickyNav();  \n \n    $(window).scroll(function() {  \n        stickyNav();  \n    });  \n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are several tutorials on how to make things \u201csticky\u201d using CSS. We will make a \u201csometimes sticky\u201d element using CSS and jQuery. Here is an article on&nbsp;CSS sticky footers&nbsp;to help you understand before we move onto the \u201csometimes\u201d part using jQuery. Remember &#8211; &nbsp;position: fixed;&nbsp;in a CSS sticky element is in a fixed position&nbsp;relative&nbsp;to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":269,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":{"0":"post-268","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-coding"},"_links":{"self":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":3,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/268\/revisions\/276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/media\/269"}],"wp:attachment":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}