javascript 在Scroll上显示Navbar

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 在Scroll上显示Navbar相关的知识,希望对你有一定的参考价值。

section.navbar {
  @extend .flex;
  background: transparent;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 10;
  border: none;
  border-radius: 0;
  &#navbar-scroll {
    position: fixed;
    z-index: 10;
    background: $primary-color;
    border-bottom: solid 5px white;
    .border-black {
      width: 100%;
      height: 9px;
      background: black;
      position: absolute;
      z-index: 2;
      bottom: -14px;
    }
  }
  .wrapper {
    @extend .flex;
    @extend .flex--wrap;
    @extend .flex--center;
    @extend .page-wrapper; // container width [_base.scss]
    width: 100%;
    padding: 2rem 3rem;
    position: relative;
    .item {
      @extend .flex;
      &--1 {
        flex: 0 1 40%;
      }
      &--2 {
        flex: 0 1 60%;
        color: white;
        .box {
          margin: auto 0 auto auto;
        }
      }
    }
  }

  .logo {
    max-width: 90px;
  }

  .ribbon {
    background: url('../img/ribbon/40years-strong.svg') no-repeat center center / contain;
    height: 156px;
    width: 102px;
    position: absolute;
    z-index: 5;
    top: 0;
    left: 14rem;
  }

  .contact {
    font-size: 1.7rem;
    font-weight: 700;
    position: relative;
    top: -1px;
  }

  .divider {
    background: white;
    width: 1px;
    height: 25px;
    padding: 0;
    position: relative;
    top: 5px;
  }

  .apply {
    font-size: 2.6rem;
    font-weight: 700;
  }

  a {
    color: white;
    &:hover,
    &:focus,
    &:target,
    &:active {
      text-decoration: underline;
    }
  }

}
<?php $x = 1; while ($x <= 2) { ?>

    <section class="navbar" <?php if($x == 2) { ?> id="navbar-scroll" <?php } ?>>
        <div class="wrapper">
            <div class="item item--1">
                <div class="box">

                    <a href="/">
                        <?php if ($x == 1) { ?>
                            <img class="logo" src="<?php echo get_template_directory_uri(); ?>/assets/build/img/logos/koch.png" alt="Koch" />
                        <?php } else { ?>
                            <img class="logo" src="<?php echo get_template_directory_uri(); ?>/assets/build/img/logos/koch_white.png" alt="Koch" />
                        <?php } ?>
                    </a>

                    <div class="ribbon"></div>
                </div>
            </div>
            <div class="item item--2">
                <div class="box">
                    <ul class="list-inline">
                        <li>
                            <p class="contact">Speak with a recruiter now 000-000-0000</p>
                        </li>
                        <li class="divider"></li>
                        <li>
                            <p class="apply"><a href="#">Apply Online</a></p>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="border-black"></div>
    </section>

<?php $x++; } ?>
(function ($) {
    $(document).ready(function(){
        
        // hide .navbar first
        $("#navbar-scroll").hide(); 

        // fade in .navbar after scroll
        $(function () { 
            $(window).scroll(function () {
                // set distance user needs to scroll before we fadeIn navbar
                if ($(this).scrollTop() > 600) {
                    $('#navbar-scroll').fadeIn();
                } else {
                    $('#navbar-scroll').fadeOut();
                }
            });
        });
    });
}(jQuery));

以上是关于javascript 在Scroll上显示Navbar的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript - window.scroll( behavior: 'smooth' ) 在 Safari 中不起作用

在 iPad 上滚动冻结 (javascript)

JavaScript库,使页面上的任何元素在滚动时都可见。

JAVASCRIPT 在用户 SCROLL DOWN 和 SCROLL TOP 时执行函数

iOS状态栏点击上的Javascript事件

javascript 将JavaScript处理程序附加到Scroll