设置指定元素随网页滚动而隐藏或显示

Kevin 杂记 950 次浏览 37条评论

导航栏随网页滚动而隐藏或显示

这个效果经常用在网站导航菜单里,当用户向下滑动网页时自动隐藏菜单栏,向上滑动则显示。这样即可以充分利用屏幕高度,也能让用户方便地访问菜单。效果可以查看这个DEMO,实现方法如下。

HTML 结构

<header class="fixed-top">
    <nav>
        <a href="#">link</a>
        <a href="#">link</a>
        <a href="#">link</a>
    </nav>
</header>

.fixed-top是绑定事件的 CSS 类,使用过程中替换成自己的。

CSS 样式

.fixed-top {
    position:fixed;
    top:0;
    right:0;
    left:0;
    z-index:1030
}
.fixed-top {
    will-change:transform;
    transition:transform 300ms linear
}
.is-hidden {
    transform:translateY(-100%)
}
.is-show {
    transform:translateY(0%)
}

为相关类选择器添加样式,以显示触发事件的视觉效果。

jQuery 代码

$(function() {
    var vertical_scroll = $(document).scrollTop();
    $(window).scroll(function() {
    var rolling_action = $(document).scrollTop();
        if (rolling_action > 200) {
            $('.fixed-top').addClass('is-hidden')
        }
        if (rolling_action > vertical_scroll) {
            $('.fixed-top').removeClass('is-show')
        } else {
            $('.fixed-top').removeClass('is-hidden').addClass('is-show')
        }
        if (rolling_action < 200) {
            $('.fixed-top').removeClass('is-show')
        }
    vertical_scroll = $(document).scrollTop();
    })
});

在添加代码前先链接好 jQuery.js 文件。上面代码默认滑动 200px 后开始触发事件。

其它补充

上面原始代码来自这篇外文,在原基础上做了一些精简修改。原代码根据所设置的元素高度开始触发事件,这里改成了按指定高度。另外随滚动状态删除多余的 CSS 类。

这个代码相对简单,只实现了基本功能。如果网页内使用了其它特效,可能要按需修改下。例如有的元素设置了随页面滚动而固定显示,由于菜单栏不是固定显示的,为避免菜单栏滑出时遮挡住其它元素,需要调整受影响元素的边距。案例可以查看文中 DEMO 的侧边栏固定元素,代码查看网页源码。

最后分享几个类似功能的 jQuery 代码项目。

https://github.com/WickyNilliams/headroom.js
https://github.com/istvan-ujjmeszaros/bootstrap-autohidingnavbar
https://github.com/filippo-quacquarelli/scroll-hide
https://codyhouse.co/gem/auto-hiding-navigation

评论列表

  1. #1

    I’d always want to be update on new blog posts on this site, saved to bookmarks! https://local-auto-locksmith.co.uk/renault/

  2. #2

    I enjoy you because of all of the effort on this blog. Debby delights in carrying out internet research and it’s easy to see why. A number of us hear all about the compelling form you convey both useful and interesting guidelines by means of the blog and boost contribution from other individuals about this area of interest and my child has been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a dazzling job. https://endepamitriptyline.com/

  3. #3

    Bardzo interesujący temat, dziękuję za wysłanie wiadomości spirometr kulkowy. https://spirometrn.pl/

  4. #4

    tadalafil liquid – liquid tadalafil citrate dosage tadalafil 5 mg

  5. #5

    Cqsmfg – cheap stromectol Tktxfa absqlx

  6. #6

    Rjfxun – cialis dosage Mjrqww mdhxcf

  7. #7

    Jkuglg – furosemida Yylqjp cyojsh

  8. #8

    Lgxwmw – buy prozac Usfgzh pogmxd

  9. #9

    Lefbvo – brand cialis buy Daizyf ouqpym

  10. #10

    Wnzooy – sildenafil citrate pfizer Gyxrks gtbckt

  11. #11

    Hnjmmk – research paper juvenile delinquency Tamxjh khroxr

  12. #12

    Plwzgv – buy generic viagra canada online Akrkrg ptgwvb

  13. #13

    Uizypu – viagra no prescription online Rdzsol xhiaup

  14. #14

    Xilbez – electoral college pros and cons essay Cuxosf ujgjjx

  15. #15

    Pemzbn – buy proscar finasteride 5mg Hmcbud xfoajv

  16. #16

    Rrdjlp – placebo viagra Betowu ybnrxj

  17. #17

    Gmxigo – where to buy finasteride philippines Pcbsqx mxgofq

  18. #18

    Qkwsgy – best college application essay Yibxpw ymekvm

  19. #19

    Hwrygk – sildenafil 100mg walmart Gwjpvc xzgkis

  20. #20

    Zsnwev – college pressures essay Ewcpql banrsn

  21. #21

    Lrcsco – lasix side effects Lcspxn sihhcm

  22. #22

    Dfkjui – vardenafil rezeptfrei Cfopwt ooeeqm

  23. #23

    Yetbao – generic silagra Nscvck pnfzla

  24. #24

    Axbtro – tadacip 20 uk Ujfvux uztloy

  25. #25

    Aklwda – spedra 100mg Kaymen nbakud

  26. #26

    buy tadacip 10 mg tablet – buy tadacip online buy tadacip canada

  27. #27

    celebrex medicine generic – celecoxib 200mg generic buy celebrex online

  28. #28

    ivermectin 1% – stromectol cream stromectol tab price

  29. #29

    zofran – 2018 nexium coupon can i buy yasmin over the counter

  30. #30

    prednisone 20mg tablets where to buy – buy prednisone tablets online cost of prednisone prescription

  31. #31

    viagra best buy india – viagra professional paypal sildenafil 100mg tab

  32. #32

    viagra buy online canada – comprar viagra por internet black viagra

  33. #33

    lyrica cost uk – how much is lyrica in mexico lyrica canada

  34. #34

    where to buy nexium tablets – cheap nexium online generic nexium otc

  35. #35

    buy ivermectin cream – ivermectin pills human stromectol online pharmacy

  36. #36

    ivermectin 3mg price – oral ivermectin cost ivermectin syrup

  37. #37

    zofran 12mg – ondansetron cost zofran coupon

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。