【问题标题】:How do I make my navbar go from no shadow to shadow with fade transition on scroll?如何使我的导航栏从无阴影变为阴影并在滚动时使用淡入淡出过渡?
【发布时间】:2020-10-30 04:31:29
【问题描述】:

我希望我的导航栏在滚动时从无阴影变为有阴影 - 完成(请阅读下面的答案)。现在我想为滚动的阴影添加一个淡入淡出的过渡效果。阅读下面的“更新”。

$(document).ready(function() {
        // Transition effect for navbar
        $(window).scroll(function() {
          // checks if window is scrolled more than 500px, adds/removes solid class
          if($(this).scrollTop() > 500) {
              $('.topnav').addClass('shadow');
          } else {
              $('.topnav').removeClass('shadow');
          }
        });
});
.logo {
    float: left;
  padding-left: 20px;
    padding-right: 15px;
    padding-bottom: 30px;
    padding-top: 20px;
    width: 210px;
    height: auto;
}

.topnav {
  overflow: hidden;
  background-color: white;
    top: 0;
  left: 0;
    margin: 0;
  padding: 0;
    z-index: 1000;
  width: 100%; !important;
    position: fixed;
}

.topnav.shadow {
  -webkit-box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
  -moz-box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
  box-shadow: 0px 0px 9px 3px rgba(41,41,41,.25);
}

.topnav a {
  float: left;
    display: block;
    color: grey;
    text-align: center;
    padding-top: 30px;
    padding-bottom: 0px;
    padding-left: 16px;
    padding-right: 16px;
    text-decoration: none;
    font-size: 18px;
    transition: color 0.3s ease-out;
}

.active, .nav-btn {
  color: #1a1a1a;
}

@media (max-width: 800px) {
    .resp-t-n {
        padding-top: 60px;
    }
    .resp-b-n {
        padding-bottom: 30px;
    }
    .logo {
        width: 130px;
        height: auto;
    }
}


.topnav .icon {
  display: none;
}

.dropdown {
    float: left;
    overflow: hidden;
}

.dropdown .dropbtn {
    font-size: 17px;
    border: none;
  outline: none;
    color: grey;
    padding: 34px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
    transition: color 0.3s ease-out;
}

.dropdown-content {
    display: none;
    position: fixed;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1001;
}

.dropdown-content a {
    float: none;
    color: grey;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover:not(.logo), .dropdown:hover .dropbtn {
    color: #1a1a1a;
    transition: color 0.5s ease;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 800px) {
  .topnav a:not(:first-child), .dropdown .dropbtn .resp-t-n .resp-b-n {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 800px) {
  .topnav.responsive {
      position: fixed;
  }
  .topnav.responsive .icon {
    position: fixed;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

#active {
  color: #1a1a1a;
}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 
 <div class="topnav" id="myTopnav">
    <img class="logo" src="[img path]">
        <a href="/" id="active"><div class="resp-t-n">Home</div></a>
        <a href="elements.html">Elements</a>
        <a href="about.html">About</a>
        <a href="contact.html"><div class="resp-b-n">Contact</div></a>
    <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()"><i class="fal fa-bars"></i></a>
  </div>

我正在尝试学习如何从头开始编码以及任何我不知道的东西,我通常只是搜索,但我真的找不到有效的解决方案,因此我的第一篇文章。

非常感谢任何帮助:)

在我的控制台中发现错误:

未捕获的引用错误:$ 未定义 在 main.js:14

第 14 行的代码是 $(document).ready(function() { — 我解决了这个问题

更新

我现在想为滚动的阴影添加一个淡入淡出过渡。是用 CSS 实现还是必须用 JS 备份?

【问题讨论】:

    标签: javascript html css navigation


    【解决方案1】:

    问题是&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"&gt;&lt;/script&gt; 在我的主 .js 文件下。我只是把它移到上面。这么简单又愚蠢的错误!

    对于我的问题的第二部分——过渡——我将transition: 1s; 添加到.topnav

    非常感谢大家的宝贵时间:)

    【讨论】:

      【解决方案2】:

      $(document).ready(function() {
        // Transition effect for navbar
        $(window).scroll(function() {
          // checks if window is scrolled more than 500px, adds/removes solid class
          if ($(this).scrollTop() > 50) {
            $('.topnav').addClass('shadow');
          } else {
            $('.topnav').removeClass('shadow');
          }
        });
      });
      body {
        height: 200vh/* just for checking purpose */
      }
      
      .logo {
        float: left;
        padding-left: 20px;
        padding-right: 15px;
        padding-bottom: 30px;
        padding-top: 20px;
        width: 210px;
        height: auto;
      }
      
      .topnav {
        overflow: hidden;
        background-color: white;
        top: 0;
        left: 0;
        margin: 0;
        padding: 0;
        z-index: 1000;
        width: 100%;
        position: fixed;
      }
      
      .topnav.shadow {
        -webkit-box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
        -moz-box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
        box-shadow: 0px 0px 9px 3px rgba(41, 41, 41, .25);
      }
      
      .topnav a {
        float: left;
        display: block;
        color: grey;
        text-align: center;
        padding-top: 30px;
        padding-bottom: 0px;
        padding-left: 16px;
        padding-right: 16px;
        text-decoration: none;
        font-size: 18px;
        transition: color 0.3s ease-out;
      }
      
      .active,
      .nav-btn {
        color: #1a1a1a;
      }
      
      @media (max-width: 800px) {
        .resp-t-n {
          padding-top: 60px;
        }
        .resp-b-n {
          padding-bottom: 30px;
        }
        .logo {
          width: 130px;
          height: auto;
        }
      }
      
      .topnav .icon {
        display: none;
      }
      
      .dropdown {
        float: left;
        overflow: hidden;
      }
      
      .dropdown .dropbtn {
        font-size: 17px;
        border: none;
        outline: none;
        color: grey;
        padding: 34px 16px;
        background-color: inherit;
        font-family: inherit;
        margin: 0;
        transition: color 0.3s ease-out;
      }
      
      .dropdown-content {
        display: none;
        position: fixed;
        background-color: #f9f9f9;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        z-index: 1001;
      }
      
      .dropdown-content a {
        float: none;
        color: grey;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
        text-align: left;
      }
      
      .topnav a:hover:not(.logo),
      .dropdown:hover .dropbtn {
        color: #1a1a1a;
        transition: color 0.5s ease;
      }
      
      .dropdown-content a:hover {
        background-color: #ddd;
        color: black;
      }
      
      .dropdown:hover .dropdown-content {
        display: block;
      }
      
      @media screen and (max-width: 800px) {
        .topnav a:not(:first-child),
        .dropdown .dropbtn .resp-t-n .resp-b-n {
          display: none;
        }
        .topnav a.icon {
          float: right;
          display: block;
        }
      }
      
      @media screen and (max-width: 800px) {
        .topnav.responsive {
          position: fixed;
        }
        .topnav.responsive .icon {
          position: fixed;
          right: 0;
          top: 0;
        }
        .topnav.responsive a {
          float: none;
          display: block;
          text-align: left;
        }
        .topnav.responsive .dropdown {
          float: none;
        }
        .topnav.responsive .dropdown-content {
          position: relative;
        }
        .topnav.responsive .dropdown .dropbtn {
          display: block;
          width: 100%;
          text-align: left;
        }
      }
      
      #active {
        color: #1a1a1a;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      
      <div class="topnav" id="myTopnav">
        <img class="logo" src="[img path]">
        <a href="/" id="active">
          <div class="resp-t-n">Home</div>
        </a>
        <a href="elements.html">Elements</a>
        <a href="about.html">About</a>
        <a href="contact.html">
          <div class="resp-b-n">Contact</div>
        </a>
        <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()"><i class="fal fa-bars"></i></a>
      </div>

      您的页面窗口内容较少。如果您在 js 中提到的内容超过 500px,它将起作用。

      【讨论】:

      • 我在 JS 中将 solid 改为 shadow 而不是这个,因为我尝试了这个,但它什么也没做
      • 编辑了我的答案。
      • 我把if($(this).scrollTop() &gt; 500)改成if($(this).scrollTop() &gt; 50)还是不行
      • 添加了 sn-p。请您现在检查。
      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多