Highlight Nav Menu on scroll with JavaScript

Поделиться
HTML-код
  • Опубликовано: 31 май 2024
  • Many of you guys wanted to see a video on Highlighted nav menu. So here it is as you wanted. This is a really popular stuff that you might saw it in many website. Here when you scroll on web page, based on your scroll position the menu active item change dynamically. Today we are going make it with HTML, CSS and JavaScript.
    Hope you will like it.
    🔔subscribe and turn on the notification bell.
    👉 source code: codepen.io/Web_Cifar/pen/LYRBbVE
    Timestamps -
    00:00 - Preview
    01:57 - Layout(HTML/CSS)
    10:42 - JavaScript
    23:17 - Tutorial Conclusion
    Follow Arfan 👇👇
    Instagram: / shaifarfan08
    Facebook: / shaifarfan08
    🐱‍🏍 Support The Channel
    Paypal : paypal.me/webcifar
    ▶ Also Watch :
    Make a full website with HTML & CSS: • One Page Full Website ...
    HTML & CSS Landing Page : • HTML & CSS Website Lan...
    Mini Project for HTML & CSS : • Responsive Website Wit...
    How To Make A WordPress website: • How To Make A Professi...
    ▶ Some useful Playlist :
    HTML & CSS: • HTML & CSS
    Vanilla JavaScript : • Vanilla JavaScript
    Frontend Mentor Challenges: • Frontend Mentor Challe...
    30days30submits Challenge: • 30days30submits
    🌐 Join The Community :
    Our website : webcifar.com/
    Facebook Page : / webcifar
    Facebook group : bit.ly/fb-group-webcifar
    Instagram : / web_cifar
    twitter : / webcifar
    Github: github.com/WebCifar
    #webcifar #highlightedMenu #javascript
  • НаукаНаука

Комментарии • 136

  • @AjSarco
    @AjSarco 2 года назад +1

    life saver tutorial , thanks man, I've been struggling how to highlight section on navbar, I'm almost done with my project and this is the only thing I'm stuck with.

  • @ichigolei4637
    @ichigolei4637 2 года назад

    Just what I needed! I made minimalist navigation side bar, just lines to redirect my five sections, and was wondering how to change the background color when passing each section. thank you so much

  • @skullnecroze
    @skullnecroze Год назад

    The best tutorial about this on the web. Thanks very much for share this with us!

  • @freddymoreno9300
    @freddymoreno9300 Год назад

    Great Great Great TUTORIAL! simple, concise, and intereting. Thank you! keep doing great stuff

  • @gabrielfono844
    @gabrielfono844 2 года назад

    Thanks so much sir
    I brought the same logic into the startup I am currently building with React.
    I used useRef instead and everything is smooth.
    thanks again
    have a wonderful day and your family
    love from Seattle Washington

  • @simond.7448
    @simond.7448 Год назад

    Thank you very much for this quick guide! It's awesome.

  • @tusharkadian
    @tusharkadian 3 года назад

    It is better to add class on tag instead of ?

  • @tinghuang3218
    @tinghuang3218 2 года назад

    Great tutorial! This is perfect and helps me a lot. Thank you so much👍

  • @JoshPalmeroc
    @JoshPalmeroc 3 года назад +11

    Great tutorial man! What I did take make it a little easier to maintain is instead of selecting all the li, I selected every "a" tag and got its href attribute then did a substring to remove the "#". Then i checked that against the "current" variable. that way you don't have to add classes to each li.
    const navLi = document.querySelectorAll('.tabs__item a');
    navLi.forEach(li => {
    li.classList.remove('active');
    const href = li.getAttribute('href').substring(1);
    if (href === current) {
    li.classList.add('active');
    }
    });

  • @photoinshot1355
    @photoinshot1355 Год назад

    Excellent video, brilliant tutor, he explains things really well, clear well presented video, thanks!

  • @alexkelso1649
    @alexkelso1649 2 года назад

    thanks so much legend. Learned so much watching this.

  • @photoinshot1355
    @photoinshot1355 6 месяцев назад

    Brilliant tutorial, very helpful and very well explained and demonstrated, great code, great teacher, thanks!

  • @mwmersal
    @mwmersal 2 года назад

    Wooooow! this was unbelievably helpful mate. Thanks ^^

    • @webcifar
      @webcifar  2 года назад

      Glad to hear it!

  • @Matin1999_unique
    @Matin1999_unique 2 года назад +5

    this tutorial is just perfect and what programmers are looking for. 👍

  • @valeriyadolya8412
    @valeriyadolya8412 2 года назад

    Thank you very much, it helped me a lot!

  • @guilhermehammes1516
    @guilhermehammes1516 3 года назад +1

    simple and functional! thanks for the great video

  • @chaipiyo
    @chaipiyo 2 года назад

    thank you so much brother .. very well explained

  • @ryukagai
    @ryukagai 3 года назад

    Exactly what I needed! Thank you :)

  • @tszpang9917
    @tszpang9917 3 года назад

    Thanks for the tutorial! Worked for my project :D

  • @nimi_nomo
    @nimi_nomo 11 месяцев назад

    Thanks so much. Helped a lot

  • @user-gw3yd8ev6p
    @user-gw3yd8ev6p 2 года назад

    Thanks for tutorial, it's very useful!

  • @kese1135
    @kese1135 3 года назад +2

    Thank you so much! You're the best!

    • @webcifar
      @webcifar  3 года назад +1

      You are welcome ❤️

  • @alphamx13
    @alphamx13 7 месяцев назад

    Great tutorial, thank you!

  • @paalaleksander
    @paalaleksander 2 года назад

    Thanks for this! Helped me a lot!

  • @kunalpatil1250
    @kunalpatil1250 2 года назад +1

    thanks man i implemented this in react

  • @__-qp9dl
    @__-qp9dl 2 года назад

    당신은 천재입니다.
    감사합니다 !

  • @shaaa00
    @shaaa00 3 года назад

    Thank you for sharing this tutorial, it really helped me

    • @webcifar
      @webcifar  3 года назад

      Glad to hear that!

  • @istiakahammad7074
    @istiakahammad7074 3 года назад +1

    Finally i got it.
    Thanks

  • @jmcoxD
    @jmcoxD Год назад

    Excellent tutorial!

  • @esmailkhalifa3984
    @esmailkhalifa3984 2 года назад +1

    I have been looking for it for three days😶
    Finally ❤️🙂

  • @guxsta3217
    @guxsta3217 2 года назад

    Thank you so much!! Amazing video

  • @selah_2215
    @selah_2215 2 года назад

    Thank you very much! Your tutorial helped me a lot. At first I was kinda struggling troubleshooting, but it was the most fun part. It was worth it. ;> Really really thank u 🙏

  • @_4zad
    @_4zad 2 года назад

    Great vid, learned a lot!

  • @thanhannguyen2565
    @thanhannguyen2565 2 года назад

    It is extremely easy to understand.

  • @abdulraffy5398
    @abdulraffy5398 Год назад

    Nailed It!

  • @selsen9011
    @selsen9011 Год назад

    i have the problem that my sectiontop does not show, when i type "console.log".
    my Sections are all diffrent in height.
    the console.log on sections worked, so it knows where the sections are, but cant figure the offsetTop out.. Do you know why that is?

  • @havefun5519
    @havefun5519 8 месяцев назад

    Thanks, this is what I want

  • @MrTomequ
    @MrTomequ 3 года назад +3

    Cool video, thanks. Small suggestion. If you set the value of current on 'home' (current = 'home') instead an empty string, than you'll have button-background on Home at the beginning and when you click again Home as well. Gr.

  • @richardshaju
    @richardshaju Год назад

    Thank you so much 💝🥰

  • @alamineuropeanit583
    @alamineuropeanit583 2 года назад

    thanks. I like this tips

  • @rosehope5376
    @rosehope5376 2 года назад +1

    Thank you really it was a good one ..

    • @webcifar
      @webcifar  2 года назад

      Glad you liked it!

  • @kennyakins3536
    @kennyakins3536 3 года назад +3

    This really helped a lot 👏

    • @webcifar
      @webcifar  3 года назад +1

      Glad it was helpful.

  • @LordzChilight
    @LordzChilight 2 года назад

    thank you, learn a lot :D 🙏

  • @dustinbest1326
    @dustinbest1326 2 года назад

    Nice video! Thank you!

  • @sandeepsv3514
    @sandeepsv3514 3 года назад +1

    thanks thank you so much for the video it means a lot

    • @webcifar
      @webcifar  3 года назад +1

      Glad it helped

  • @patricioscida3949
    @patricioscida3949 2 года назад +1

    Great video, thank you so much.

    • @webcifar
      @webcifar  2 года назад

      You're very welcome!

  • @raomudasir4509
    @raomudasir4509 2 года назад

    really awesome bro

  • @liluo7790
    @liluo7790 2 года назад +1

    thank you so much ! thank you so much man

  • @duvvadasaibhargav9879
    @duvvadasaibhargav9879 2 года назад

    Thanks for your tutorial learned a lot

    • @webcifar
      @webcifar  2 года назад

      Glad to hear that!

  • @eddi3ms
    @eddi3ms 3 года назад

    very helpfull, thanks alot !!

  • @thangphan6047
    @thangphan6047 2 года назад

    Thank you so much :)

  • @marekgacek2034
    @marekgacek2034 Год назад

    awesome, thx man

  • @ujjalzaman2722
    @ujjalzaman2722 2 года назад +1

    thank you so much for this one.

    • @webcifar
      @webcifar  2 года назад

      You're very welcome!

  • @a.j9157
    @a.j9157 3 года назад

    Great video man keep up !

    • @webcifar
      @webcifar  3 года назад

      Thanks, you too!

  • @osman1110
    @osman1110 2 года назад

    hi how would you make it diff colours for each section?

  • @sairaghava6111
    @sairaghava6111 2 года назад

    This implementation is breaking when I change the screen resolution, can you help here

  • @ankitkumarravidas6062
    @ankitkumarravidas6062 2 года назад

    yu are my life save bro thanks

  • @dibanpitchaimuthu9336
    @dibanpitchaimuthu9336 3 года назад

    Great vid, just one error I noticed you forgotten 's' at 10:04 for transition

  • @giovannimichel5506
    @giovannimichel5506 Год назад

    Thank you for the intel

  • @harshilparmar9076
    @harshilparmar9076 2 года назад

    Thanks bro!!

  • @omarfarouk2080
    @omarfarouk2080 3 года назад

    Thank you bro!

  • @MrWebDesignerAnas
    @MrWebDesignerAnas 2 года назад +1

    thank you sir it was really helpful

  • @romuloalves9349
    @romuloalves9349 3 года назад +3

    Ótimo vídeo parabéns 👏.

  • @ankitraj5ar
    @ankitraj5ar 2 года назад +1

    very useful !!!!!!

  • @r18570
    @r18570 Год назад

    thank you bro

  • @siawonderswhy
    @siawonderswhy 3 года назад +1

    very helpful thank you

    • @webcifar
      @webcifar  3 года назад

      Glad it was helpful!

  • @MohamedMedhat-ic5ng
    @MohamedMedhat-ic5ng 2 года назад

    thank you

  • @mariyanesvyetaylo6264
    @mariyanesvyetaylo6264 2 года назад

    Hello, thank you so much for this video - very helpful!
    I wonder if you can help me : I want to add .scrollIntoView({ behavior: 'smooth', inline: "start" }) when clicked on nav item(so the correspondant section scrolls smoothly into view), but can't figure out how to get the "section" id to match li class..
    navLi.forEach((li) => {
    li.classList.remove("active");
    if (li.classList.contains(current)) {
    li.classList.add("active");
    }
    li.addEventListener("click", () => {
    let liClass = li.getAttribute("class")
    ????????????
    })
    });
    sorry if this is confusing >_>

  • @tiendang5149
    @tiendang5149 2 года назад

    Very good , thank you!

  • @Rystaal
    @Rystaal 2 года назад

    Don't work for me if section's height is in %... any advice to fix this issue?

  • @shas3626
    @shas3626 2 года назад

    Great video..had a question i am getting my section.OffsetTop as undefined.Can you help me with this?

  • @SamFourieOfficial
    @SamFourieOfficial 2 года назад

    My sections variable just has an empty list, so it does not pick up the sections.

  • @ogunkayodeoluwaseun9348
    @ogunkayodeoluwaseun9348 2 года назад

    What would happen if the nav li's had multiple sub-links ???
    @WEB CIFAR

  • @nadirmohamed8274
    @nadirmohamed8274 3 года назад +1

    You are the best bro

  • @ThisIsAhn
    @ThisIsAhn 2 года назад

    ধন্যবাদ ভাই

  • @samarjamil3053
    @samarjamil3053 3 года назад

    Awesome Bro! ❤️

  • @webnirbhar785
    @webnirbhar785 2 года назад

    How to open collapse data when scroll down in js Please help

  • @ranadebnath5421
    @ranadebnath5421 3 года назад +1

    thanku so much bhai ...

    • @webcifar
      @webcifar  3 года назад

      Welcome Bhai ♥

  • @lanhphalla1623
    @lanhphalla1623 Год назад

    Very good video.
    Can you create the video of this in small screen and move navbar by base on scroll?

  • @noahdwhitney
    @noahdwhitney 8 месяцев назад

    Be careful. You are adding a scroll listener, which fires a ton even on a short distance scroll. Every time the scroll event is fired you are iterating through a list of elements. This method could be extremely expensive. You should include logic in event listener callback to throttle how many times the iteration is done.

  • @ahmadyamout6867
    @ahmadyamout6867 2 года назад

    Hello dear .Your code is not working because Pageyoffset is deprecated what is it's alternative please ?

  • @vishnuk9628
    @vishnuk9628 2 года назад

    how to keep the selected nav color the same even after refreshing the page

  • @senju31
    @senju31 2 года назад

    I'm not seeing the footer in console.log. I see the home and about section but not the footer.

  • @omarakki705
    @omarakki705 3 года назад

    Thank you my brother

    • @webcifar
      @webcifar  3 года назад +1

      You are welcome

    • @omarakki705
      @omarakki705 3 года назад

      @@webcifar do you have a tutorial in javascript

  • @BarahaJewellery
    @BarahaJewellery 2 года назад

    helpful

  • @nadirmemmedov6071
    @nadirmemmedov6071 3 года назад +1

    good

  • @imranhossainbpp
    @imranhossainbpp 2 года назад

    What if i want to add class in the nav while scrolling in a particular section ?
    Love from @Bangladesh

  • @MuhammadRizky-ub3oy
    @MuhammadRizky-ub3oy Год назад

    why is my home offsetTop returning 0?

  • @tomo_3883
    @tomo_3883 2 года назад

    Vsc theme?

  • @TommyTips
    @TommyTips 3 года назад

    It's cool ;)

  • @hugoshaco8149
    @hugoshaco8149 3 года назад

    When you are on home and click to footer: the animation ("active" so the red bg) is visible on all other nav button during the autoscroll idk if someone know what i mean :D also thx for this video

  • @AzulCieloBlue
    @AzulCieloBlue 2 года назад

    When I scroll to the bottom the last section is not beign detected. help please! Cool and really helpful video btw

  • @zayednation3692
    @zayednation3692 Год назад

    bro, can u do it for react js?

  • @victory_lucky
    @victory_lucky 3 года назад +1

    nice, but please zoom in a little bit when recording, watching on mobile is quite difficult.

  • @aminzorriyeh2782
    @aminzorriyeh2782 2 года назад

    how can i get this cod

  • @gabrielfono844
    @gabrielfono844 2 года назад +1

    sir
    I would love to start a youtube channel
    could you tell with software you are using to record a course and which software you use to edit.
    also which microphone and camera you are using
    I really love creating content that is why I am building an application to share my content

    • @webcifar
      @webcifar  2 года назад

      That's awesome. I use OBS to record, DaVinci Resolve to edit and for the microphone I use BM800. For now, everything is working fine for me with this setup.

    • @gabrielfono844
      @gabrielfono844 2 года назад

      @@webcifar thanks a lot 🙏

  • @senju31
    @senju31 2 года назад +2

    There's a RUclipsr by the name of Bedimcode that implemented this feature in this video ruclips.net/video/6cidbUHNZRQ/видео.html but he did it differently from yours. I think yours was quicker to grasp because you explained what you were doing but he doesn't speak in his videos. I actually was able to break it down by watching your video and further reading and testing on my part. I may make a RUclips video on it , explaining how Bedimcode made his own work

  • @Sebeklis
    @Sebeklis 2 года назад

    There are tons of problems with routing etc.

  • @entertainmenthut8896
    @entertainmenthut8896 2 года назад

    Your tutorial does not work for me. I am using sublime 3.

  • @harshjha271
    @harshjha271 2 года назад

    Please explain properly i did'nt understand the logic in (if condtion) if anyone understand please explain this logic to me