Make sticky header for Magento 2 problem












0















I'm following the answer in this topic to keep header sticky. So far I've created vendor/theme/web/js/sticky.js with the following content:



require([ "jquery" ], function($) {
$(window).scroll(function() {
if ($(window).scrollTop() > $('#header').offset().top && !($('#header').hasClass('sticky'))) {
$('#header-2').addClass('sticky');
} else if ($(window).scrollTop() == 0) {
$('#header').removeClass('sticky');
}
});
});


and called this file inside vendor/theme/Magento_Theme/layout/default_head_blocks.xml



<link src="js/sticky.js"/>


Second part of this answer says:



"add some css to this sticky class like this:"



#header.sticky {
background: #fff none repeat scroll 0 0;
border-bottom: 4px solid #325052;
margin: 0 auto;
max-width: 100%;
position: fixed;
top: 0;
width: 100%;
z-index: 99;
}


How can I implement this part? I've tried creating a css/sticky.css file with the code above and link it on default_head_blocks.xml ( <css src="css/sticky.css"/> ), but it's not working.










share|improve this question





























    0















    I'm following the answer in this topic to keep header sticky. So far I've created vendor/theme/web/js/sticky.js with the following content:



    require([ "jquery" ], function($) {
    $(window).scroll(function() {
    if ($(window).scrollTop() > $('#header').offset().top && !($('#header').hasClass('sticky'))) {
    $('#header-2').addClass('sticky');
    } else if ($(window).scrollTop() == 0) {
    $('#header').removeClass('sticky');
    }
    });
    });


    and called this file inside vendor/theme/Magento_Theme/layout/default_head_blocks.xml



    <link src="js/sticky.js"/>


    Second part of this answer says:



    "add some css to this sticky class like this:"



    #header.sticky {
    background: #fff none repeat scroll 0 0;
    border-bottom: 4px solid #325052;
    margin: 0 auto;
    max-width: 100%;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 99;
    }


    How can I implement this part? I've tried creating a css/sticky.css file with the code above and link it on default_head_blocks.xml ( <css src="css/sticky.css"/> ), but it's not working.










    share|improve this question



























      0












      0








      0


      0






      I'm following the answer in this topic to keep header sticky. So far I've created vendor/theme/web/js/sticky.js with the following content:



      require([ "jquery" ], function($) {
      $(window).scroll(function() {
      if ($(window).scrollTop() > $('#header').offset().top && !($('#header').hasClass('sticky'))) {
      $('#header-2').addClass('sticky');
      } else if ($(window).scrollTop() == 0) {
      $('#header').removeClass('sticky');
      }
      });
      });


      and called this file inside vendor/theme/Magento_Theme/layout/default_head_blocks.xml



      <link src="js/sticky.js"/>


      Second part of this answer says:



      "add some css to this sticky class like this:"



      #header.sticky {
      background: #fff none repeat scroll 0 0;
      border-bottom: 4px solid #325052;
      margin: 0 auto;
      max-width: 100%;
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 99;
      }


      How can I implement this part? I've tried creating a css/sticky.css file with the code above and link it on default_head_blocks.xml ( <css src="css/sticky.css"/> ), but it's not working.










      share|improve this question
















      I'm following the answer in this topic to keep header sticky. So far I've created vendor/theme/web/js/sticky.js with the following content:



      require([ "jquery" ], function($) {
      $(window).scroll(function() {
      if ($(window).scrollTop() > $('#header').offset().top && !($('#header').hasClass('sticky'))) {
      $('#header-2').addClass('sticky');
      } else if ($(window).scrollTop() == 0) {
      $('#header').removeClass('sticky');
      }
      });
      });


      and called this file inside vendor/theme/Magento_Theme/layout/default_head_blocks.xml



      <link src="js/sticky.js"/>


      Second part of this answer says:



      "add some css to this sticky class like this:"



      #header.sticky {
      background: #fff none repeat scroll 0 0;
      border-bottom: 4px solid #325052;
      margin: 0 auto;
      max-width: 100%;
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 99;
      }


      How can I implement this part? I've tried creating a css/sticky.css file with the code above and link it on default_head_blocks.xml ( <css src="css/sticky.css"/> ), but it's not working.







      magento2 javascript css magento2.2.3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 8 '18 at 11:56







      Madn3ss795

















      asked May 8 '18 at 11:47









      Madn3ss795Madn3ss795

      457




      457






















          2 Answers
          2






          active

          oldest

          votes


















          2














          You can use default M2 Sticky



          define([
          'jquery',
          'mage/mage',
          'domReady!'
          ], function ($) {
          $('#header').mage('sticky', {
          container: '#maincontent',
          spacingTop: 0
          });
          });


          And CSS



          #header {
          position: relative;
          }





          share|improve this answer


























          • This is the better answer

            – Leland
            5 hours ago



















          1














          So I forgot to replace some #header placeholders, correct code below.



          Update: I've added padding ( to prevent pixel skipping ) and mobile support ( to pad correct element on mobile interface )



          vendor/theme/web/js/sticky.js :



          require([ "jquery" ], function($) {
          if( $(window).width() >= 768 ) {
          $(window).scroll(function() {
          if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.sections.nav-sections').hasClass('padding'))) {
          $('.page-header').addClass('sticky');
          $('.sections.nav-sections').addClass('padding');
          } else if ($(window).scrollTop() == 0) {
          $('.page-header').removeClass('sticky');
          $('.sections.nav-sections').removeClass('padding');
          }
          });
          }
          else
          {
          $(window).scroll(function() {
          if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.page-main').hasClass('paddingm'))) {
          $('.page-header').addClass('sticky');
          $('.page-main').addClass('paddingm');
          } else if ($(window).scrollTop() == 0) {
          $('.page-header').removeClass('sticky');
          $('.page-main').removeClass('paddingm');
          }
          });
          }
          });


          vendor/theme/web/css/sticky.css :



          .sticky {
          background: #fff none repeat scroll 0 0;
          border-bottom: 2px solid #131313;
          margin: 0 auto;
          max-width: 100%;
          max-height: 109px;
          position: fixed;
          top: 0;
          width: 100%;
          z-index: 99;
          }

          .padding {
          padding-top: 126px;
          }

          .paddingm {
          padding-top: 107px;
          }


          ( max-height and padding-top values depend on header's height )






          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f225143%2fmake-sticky-header-for-magento-2-problem%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You can use default M2 Sticky



            define([
            'jquery',
            'mage/mage',
            'domReady!'
            ], function ($) {
            $('#header').mage('sticky', {
            container: '#maincontent',
            spacingTop: 0
            });
            });


            And CSS



            #header {
            position: relative;
            }





            share|improve this answer


























            • This is the better answer

              – Leland
              5 hours ago
















            2














            You can use default M2 Sticky



            define([
            'jquery',
            'mage/mage',
            'domReady!'
            ], function ($) {
            $('#header').mage('sticky', {
            container: '#maincontent',
            spacingTop: 0
            });
            });


            And CSS



            #header {
            position: relative;
            }





            share|improve this answer


























            • This is the better answer

              – Leland
              5 hours ago














            2












            2








            2







            You can use default M2 Sticky



            define([
            'jquery',
            'mage/mage',
            'domReady!'
            ], function ($) {
            $('#header').mage('sticky', {
            container: '#maincontent',
            spacingTop: 0
            });
            });


            And CSS



            #header {
            position: relative;
            }





            share|improve this answer















            You can use default M2 Sticky



            define([
            'jquery',
            'mage/mage',
            'domReady!'
            ], function ($) {
            $('#header').mage('sticky', {
            container: '#maincontent',
            spacingTop: 0
            });
            });


            And CSS



            #header {
            position: relative;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 4 mins ago









            Leland

            1094




            1094










            answered May 8 '18 at 12:46









            Sanju AntalaSanju Antala

            28810




            28810













            • This is the better answer

              – Leland
              5 hours ago



















            • This is the better answer

              – Leland
              5 hours ago

















            This is the better answer

            – Leland
            5 hours ago





            This is the better answer

            – Leland
            5 hours ago













            1














            So I forgot to replace some #header placeholders, correct code below.



            Update: I've added padding ( to prevent pixel skipping ) and mobile support ( to pad correct element on mobile interface )



            vendor/theme/web/js/sticky.js :



            require([ "jquery" ], function($) {
            if( $(window).width() >= 768 ) {
            $(window).scroll(function() {
            if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.sections.nav-sections').hasClass('padding'))) {
            $('.page-header').addClass('sticky');
            $('.sections.nav-sections').addClass('padding');
            } else if ($(window).scrollTop() == 0) {
            $('.page-header').removeClass('sticky');
            $('.sections.nav-sections').removeClass('padding');
            }
            });
            }
            else
            {
            $(window).scroll(function() {
            if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.page-main').hasClass('paddingm'))) {
            $('.page-header').addClass('sticky');
            $('.page-main').addClass('paddingm');
            } else if ($(window).scrollTop() == 0) {
            $('.page-header').removeClass('sticky');
            $('.page-main').removeClass('paddingm');
            }
            });
            }
            });


            vendor/theme/web/css/sticky.css :



            .sticky {
            background: #fff none repeat scroll 0 0;
            border-bottom: 2px solid #131313;
            margin: 0 auto;
            max-width: 100%;
            max-height: 109px;
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 99;
            }

            .padding {
            padding-top: 126px;
            }

            .paddingm {
            padding-top: 107px;
            }


            ( max-height and padding-top values depend on header's height )






            share|improve this answer






























              1














              So I forgot to replace some #header placeholders, correct code below.



              Update: I've added padding ( to prevent pixel skipping ) and mobile support ( to pad correct element on mobile interface )



              vendor/theme/web/js/sticky.js :



              require([ "jquery" ], function($) {
              if( $(window).width() >= 768 ) {
              $(window).scroll(function() {
              if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.sections.nav-sections').hasClass('padding'))) {
              $('.page-header').addClass('sticky');
              $('.sections.nav-sections').addClass('padding');
              } else if ($(window).scrollTop() == 0) {
              $('.page-header').removeClass('sticky');
              $('.sections.nav-sections').removeClass('padding');
              }
              });
              }
              else
              {
              $(window).scroll(function() {
              if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.page-main').hasClass('paddingm'))) {
              $('.page-header').addClass('sticky');
              $('.page-main').addClass('paddingm');
              } else if ($(window).scrollTop() == 0) {
              $('.page-header').removeClass('sticky');
              $('.page-main').removeClass('paddingm');
              }
              });
              }
              });


              vendor/theme/web/css/sticky.css :



              .sticky {
              background: #fff none repeat scroll 0 0;
              border-bottom: 2px solid #131313;
              margin: 0 auto;
              max-width: 100%;
              max-height: 109px;
              position: fixed;
              top: 0;
              width: 100%;
              z-index: 99;
              }

              .padding {
              padding-top: 126px;
              }

              .paddingm {
              padding-top: 107px;
              }


              ( max-height and padding-top values depend on header's height )






              share|improve this answer




























                1












                1








                1







                So I forgot to replace some #header placeholders, correct code below.



                Update: I've added padding ( to prevent pixel skipping ) and mobile support ( to pad correct element on mobile interface )



                vendor/theme/web/js/sticky.js :



                require([ "jquery" ], function($) {
                if( $(window).width() >= 768 ) {
                $(window).scroll(function() {
                if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.sections.nav-sections').hasClass('padding'))) {
                $('.page-header').addClass('sticky');
                $('.sections.nav-sections').addClass('padding');
                } else if ($(window).scrollTop() == 0) {
                $('.page-header').removeClass('sticky');
                $('.sections.nav-sections').removeClass('padding');
                }
                });
                }
                else
                {
                $(window).scroll(function() {
                if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.page-main').hasClass('paddingm'))) {
                $('.page-header').addClass('sticky');
                $('.page-main').addClass('paddingm');
                } else if ($(window).scrollTop() == 0) {
                $('.page-header').removeClass('sticky');
                $('.page-main').removeClass('paddingm');
                }
                });
                }
                });


                vendor/theme/web/css/sticky.css :



                .sticky {
                background: #fff none repeat scroll 0 0;
                border-bottom: 2px solid #131313;
                margin: 0 auto;
                max-width: 100%;
                max-height: 109px;
                position: fixed;
                top: 0;
                width: 100%;
                z-index: 99;
                }

                .padding {
                padding-top: 126px;
                }

                .paddingm {
                padding-top: 107px;
                }


                ( max-height and padding-top values depend on header's height )






                share|improve this answer















                So I forgot to replace some #header placeholders, correct code below.



                Update: I've added padding ( to prevent pixel skipping ) and mobile support ( to pad correct element on mobile interface )



                vendor/theme/web/js/sticky.js :



                require([ "jquery" ], function($) {
                if( $(window).width() >= 768 ) {
                $(window).scroll(function() {
                if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.sections.nav-sections').hasClass('padding'))) {
                $('.page-header').addClass('sticky');
                $('.sections.nav-sections').addClass('padding');
                } else if ($(window).scrollTop() == 0) {
                $('.page-header').removeClass('sticky');
                $('.sections.nav-sections').removeClass('padding');
                }
                });
                }
                else
                {
                $(window).scroll(function() {
                if ($(window).scrollTop() > $('.page-header').offset().top && !($('.page-header').hasClass('sticky')) && !($('.page-main').hasClass('paddingm'))) {
                $('.page-header').addClass('sticky');
                $('.page-main').addClass('paddingm');
                } else if ($(window).scrollTop() == 0) {
                $('.page-header').removeClass('sticky');
                $('.page-main').removeClass('paddingm');
                }
                });
                }
                });


                vendor/theme/web/css/sticky.css :



                .sticky {
                background: #fff none repeat scroll 0 0;
                border-bottom: 2px solid #131313;
                margin: 0 auto;
                max-width: 100%;
                max-height: 109px;
                position: fixed;
                top: 0;
                width: 100%;
                z-index: 99;
                }

                .padding {
                padding-top: 126px;
                }

                .paddingm {
                padding-top: 107px;
                }


                ( max-height and padding-top values depend on header's height )







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 8 '18 at 17:21

























                answered May 8 '18 at 12:17









                Madn3ss795Madn3ss795

                457




                457






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f225143%2fmake-sticky-header-for-magento-2-problem%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Alcázar de San Juan

                    Griza ansero

                    Heinkel He 51