【问题标题】:Styling responsive fixed navigation on scroll在滚动上设置响应式固定导航样式
【发布时间】:2016-03-27 23:31:20
【问题描述】:

我正在为网站使用 adtile 的固定响应式导航 (demo here)。它工作得很好,除了一件事:我希望标题背景颜色首先是透明的,然后在您向下滚动到“关于”部分时淡入。如果可能,当“关于”链接颜色更改时,标题背景应具有 100% 的不透明度。我真的不知道如何进行这项工作,所以任何帮助都将不胜感激!

我做了一个 jsfiddle here,但是有很多 Javascript,所以在 GitHub here 上查看 repo 可能会更容易。

HTML:

<header>
  <a href="#home" class="logo" data-scroll>Fixed Nav</a>
  <nav class="nav-collapse">
    <ul>
      <li class="menu-item active"><a href="#home" data-scroll>Home</a></li>
      <li class="menu-item"><a href="#about" data-scroll>About</a></li>
      <li class="menu-item"><a href="#projects" data-scroll>Projects</a></li>
      <li class="menu-item"><a href="#blog" data-scroll>Blog</a></li>
    </ul>
  </nav>
</header>

<section id="home">
  <h1>Fixed Nav</h1>
  <p>The code and examples are hosted on GitHub and can be <a href="https://github.com/adtile/fixed-nav">found from here</a>. Read more about the approach from&nbsp;<a href="http://blog.adtile.me/2014/03/03/responsive-fixed-one-page-navigation/">our&nbsp;blog</a>.</p>
</section>

<section id="about">
  <h1>About</h1>
</section>

<section id="projects">
  <h1>Projects</h1>
</section>

<section id="blog">
  <h1>Blog</h1>
</section>

CSS:

body, div,
h1, h2, h3, h4, h5, h6,
p, blockquote, pre, dl, dt, dd, ol, ul, li, hr,
fieldset, form, label, legend, th, td,
article, aside, figure, footer, header, hgroup, menu, nav, section,
summary, hgroup {
  margin: 0;
  padding: 0;
  border: 0;
}

a:active,
a:hover {
  outline: 0;
}

@-webkit-viewport { width: device-width; }
@-moz-viewport { width: device-width; }
@-ms-viewport { width: device-width; }
@-o-viewport { width: device-width; }
@viewport { width: device-width; }


/* ------------------------------------------
  RESPONSIVE NAV STYLES
--------------------------------------------- */

.nav-collapse ul {
  margin: 0;
  padding: 0;
  width: 100%;
  display: block;
  list-style: none;
}

.nav-collapse li {
  width: 100%;
  display: block;
}

.js .nav-collapse {
  clip: rect(0 0 0 0);
  max-height: 0;
  position: absolute;
  display: block;
  overflow: hidden;
  zoom: 1;
}

.nav-collapse.opened {
  max-height: 9999px;
}

.disable-pointer-events {
  pointer-events: none !important;
}

.nav-toggle {
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}

@media screen and (min-width: 40em) {
  .js .nav-collapse {
    position: relative;
  }
  .js .nav-collapse.closed {
    max-height: none;
  }
  .nav-toggle {
    display: none;
  }
}


/* ------------------------------------------
  DEMO STYLES
--------------------------------------------- */

body {
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  text-size-adjust: 100%;
  color: #37302a;
  background: #fff;
  font: normal 100%/1.4 sans-serif;
}

section {
  border-bottom: 1px solid #999;
  text-align: center;
  padding: 100px 0 0;
  height: 800px;
  width: 100%;
}

h1 {
  margin-bottom: .5em;
}

p {
  width: 90%;
  margin: 0 auto;
}


/* ------------------------------------------
  FIXED HEADER
--------------------------------------------- */

header {
  background: #f4421a;
  position: fixed;
  z-index: 3;
  width: 100%;
  left: 0;
  top: 0;
}

.logo {
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  text-decoration: none;
  font-weight: bold;
  line-height: 55px;
  padding: 0 20px;
  color: #fff;
  float: left;
}


/* ------------------------------------------
  MASK
--------------------------------------------- */

.mask {
  -webkit-transition: opacity 300ms;
  -moz-transition: opacity 300ms;
  transition: opacity 300ms;
  background: rgba(0,0,0, .5);
  visibility: hidden;
  position: fixed;
  opacity: 0;
  z-index: 2;
  bottom: 0;
  right: 0;
  left: 0;
  top: 0;
}

.android .mask {
  -webkit-transition: none;
  transition: none;
}

.js-nav-active .mask {
  visibility: visible;
  opacity: 1;
}

@media screen and (min-width: 40em) {
  .mask {
    display: none !important;
    opacity: 0 !important;
  }
}


/* ------------------------------------------
  NAVIGATION STYLES
--------------------------------------------- */

.fixed {
  position: fixed;
  width: 100%;
  left: 0;
  top: 0;
}

.nav-collapse,
.nav-collapse * {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.nav-collapse,
.nav-collapse ul {
  list-style: none;
  width: 100%;
  float: left;
}

@media screen and (min-width: 40em) {
  .nav-collapse {
    float: right;
    width: auto;
  }
}

.nav-collapse li {
  float: left;
  width: 100%;
}

@media screen and (min-width: 40em) {
  .nav-collapse li {
    width: auto;
  }
}

.nav-collapse a {
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  border-top: 1px solid white;
  text-decoration: none;
  background: #f4421a;
  padding: 0.7em 1em;
  color: #fff;
  width: 100%;
  float: left;
}

.nav-collapse a:active,
.nav-collapse .active a {
  background: #b73214;
}

@media screen and (min-width: 40em) {
  .nav-collapse a {
    border-left: 1px solid white;
    padding: 1.02em 2em;
    text-align: center;
    border-top: 0;
    float: left;
    margin: 0;
  }
}

.nav-collapse ul ul a {
  background: #ca3716;
  padding-left: 2em;
}

@media screen and (min-width: 40em) {
  .nav-collapse ul ul a {
    display: none;
  }
}


/* ------------------------------------------
  NAV TOGGLE STYLES
--------------------------------------------- */

@font-face {
  font-family: "responsivenav";
  src:url("../icons/responsivenav.eot");
  src:url("../icons/responsivenav.eot?#iefix") format("embedded-opentype"),
    url("../icons/responsivenav.ttf") format("truetype"),
    url("../icons/responsivenav.woff") format("woff"),
    url("../icons/responsivenav.svg#responsivenav") format("svg");
  font-weight: normal;
  font-style: normal;
}

.nav-toggle {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-decoration: none;
  text-indent: -300px;
  position: relative;
  overflow: hidden;
  width: 60px;
  height: 55px;
  float: right;
}

.nav-toggle:before {
  color: #fff; /* Edit this to change the icon color */
  font: normal 28px/55px "responsivenav"; /* Edit font-size (28px) to change the icon size */
  text-transform: none;
  text-align: center;
  position: absolute;
  content: "\2261"; /* Hamburger icon */
  text-indent: 0;
  speak: none;
  width: 100%;
  left: 0;
  top: 0;
}

.nav-toggle.active:before {
  font-size: 24px;
  content: "\78"; /* Close icon */
}

JAVASCRIPT:

Stackoverflow 上的代码行数过多。请参阅 GitHub here 上的 Javascript 文件。

【问题讨论】:

    标签: javascript html css responsive-design navigation


    【解决方案1】:

    您绝对可以使用 Skrollr 来实现这一点。但是,我希望其他人可以找到不依赖于此的解决方案。

    https://github.com/Prinzhorn/skrollr

    您基本上可以在设定的滚动高度/位置上减轻不透明度/颜色的变化。所以它会完美地做你想做的事。演示 https://prinzhorn.github.io/skrollr/ 有一个很好的例子,大约在页面的中间。

    【讨论】:

      【解决方案2】:

      尝试使标题背景最初透明并在滚动时在标题上添加类。

      $(window).scroll(function() {    
       var scroll = $(window).scrollTop();
      
       if (scroll >= 800) {
          $("header").addClass("darkHeader");
       } else {
          $("header").removeClass("darkHeader");
       }
      });
      

      根据您的要求设计新类。希望这是您正在寻找的。​​p>

      【讨论】:

        【解决方案3】:

        你可以通过 JQuery 做到这一点,只需首先将任何类添加到主页链接,即“主页”&lt;li class="menu-item home active"&gt;&lt;a href="#home" data-scroll&gt;Home&lt;/a&gt;&lt;/li&gt;,然后在滚动时检测它是否处于活动状态,将类添加到标题

        例子

        $(window).scroll(function(){
          if ($(".menu-item.home").hasClass("active")) {
              $("header").addClass("default-header")
          }else {
              $("header").removeClass("default-header")
          }
        })
        

        比做

        .default-header{
        // your css code here for default/home section header
        }
        

        【讨论】:

        • 我对你的问题有点困惑,但据我所知,当用户离开时,主页部分标题会改变
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-17
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        • 2023-02-04
        • 1970-01-01
        相关资源
        最近更新 更多