Block Themes: How to create a sticky header in WordPress 2024

Поділитися
Вставка
  • Опубліковано 20 сер 2024

КОМЕНТАРІ • 17

  • @nexuz6
    @nexuz6 20 днів тому +2

    Thanks for this!
    I made some small changes to the js.
    1) Using the Math.max function to make it work on Safari when scrolled back to the top.
    2) Getting the site-header height in case you have a smaller header on mobile
    3) Removing handling the class "hidden". It does nothing right?
    // Sticky header
    document.addEventListener("DOMContentLoaded", function () {
    var prevScrollPos = Math.max(0, window.scrollY);
    var header = document.getElementById("site-header");
    window.onscroll = function () {
    var currentScrollPos = Math.max(0, window.scrollY);
    if (prevScrollPos >= currentScrollPos) {
    header.style.top = "0";
    } else {
    header.style.top = "-" + header.clientHeight + "px";
    }
    prevScrollPos = currentScrollPos;
    };
    });

    • @wpjakson
      @wpjakson  19 днів тому

      And thanks for this @nexuz6 ! Love it when folk get involved, take an idea and make it better 🙏 Will pin this comment to that others see and will update the code on the freebies download! thanks again! 🤩

    • @brunodoesit
      @brunodoesit 2 дні тому

      awesome!

  • @brunodoesit
    @brunodoesit 2 дні тому

    VERY handy tutorial Jakson thanks a lot!

  • @user-rs2em3ju7j
    @user-rs2em3ju7j 15 годин тому

    Hi jakson, how can i edit my child theme with vs code? i cant figure it out and you dont explain it in any of your videos? any other videos you can recomend? i hope for a quick reply :) thanks!

  • @JimKernix
    @JimKernix 4 місяці тому

    I had to use !important to change the background color for the header, and the admin bar is blocking the header

    • @wpjakson
      @wpjakson  4 місяці тому

      👍

    • @NazmusLabs
      @NazmusLabs 2 місяці тому

      ​@@wpjakson How do I fix the admin bar issue, please. Thanks in advance. *Edit*: found this solution that worked for me: body.admin-bar #site-header {
      margin-top: 32px }
      # F r e e P a l e s t i n e

  • @mergeconflictsdotcom
    @mergeconflictsdotcom 19 днів тому +1

    thanks you made my day!

    • @wpjakson
      @wpjakson  18 днів тому

      And you made my day with your comment Tommy! thanks for watching !

  • @kacper.ludwiczak
    @kacper.ludwiczak 4 місяці тому +1

    Hello Jakson,
    thanks for this tutorial. As always, great job.
    I encountered one problem and despite my best efforts, I cannot understand its cause:
    The header on my website is 120px high. I adjusted this value in style.css and js file (in the places you indicated in the comments), but for some reason the header displayed on the page is cut off by about 20px from the top.
    Update:
    The problem turned out to be trivial - the admin bar covers the upper part of the header. When admin bar is disabled everything is ok. :)
    Unfortunately, a second problem appeared:
    I worked on changing header behavior in a local environment. Wanting to transfer the changes to the live site, I did Export site to the file (using All-in-One Migration plugin) and then Import it to the hosting server. The site works ok, but the new header effect does not appear.
    The style.css and js files loaded on the hosting server correctly, so I don't know what the problem is. Do you have any ideas what I could have done wrong?
    All the best, Kacper

    • @wpjakson
      @wpjakson  4 місяці тому +1

      You're welcome Kacper :)
      Send me over the link for your site and I'll take a look - you can get my email here: jakson.co/subscribe/

    • @kacper.ludwiczak
      @kacper.ludwiczak 4 місяці тому

      @@wpjakson Thank you, I sent the link by email.

  • @JimKernix
    @JimKernix 4 місяці тому

    And you don't need the .style in the js file since you have the CSS defined for .hidden:
    document.addEventListener('DOMContentLoaded', function () {
    let prevScrollPos = window.scrollY;
    const header = document.getElementById('sticky-header');
    window.addEventListener('scroll', e => {
    let currentScrollPos = window.scrollY;
    if (prevScrollPos > currentScrollPos) {
    header.classList.remove('hidden');
    } else {
    header.classList.add('hidden');
    }
    prevScrollPos = currentScrollPos;
    });
    });

  • @sipsu145
    @sipsu145 3 місяці тому

    Thanks a lot for making this video! Very helpful :)
    I encountered the same problem where the admin bar is blocking the header. When it's disabled, it's all good.
    I'm facing an issue where the top margin is not applied in the mobile view causing the hero images on pages to be blocked by the header. Would you have any suggestions on how to fix this?

    • @wpjakson
      @wpjakson  19 днів тому

      Sorry I missed this @sipsu145 - have a look at @nexuz6 comment pinned above - poss that will help?