向下滚动隐藏固定标题,向上滚动显示并悬停版本2

时间:2015-07-08 10:47:31

标签: javascript html css header

我知道之前已经在这里问过这个问题,但是我不喜欢它的编写方式,并且在运行时看起来不稳定。

我想要以下内容:

  • 使用淡出功能向下滚动隐藏标题
  • 使用fadein向上滚动显示标题
  • 在悬停时显示标题

我决定不从其他例子中展示一些js,因为人们打算继续研究这些js,但是它们没有那么好用。

希望有人可以为编码员提供帮助。

--> FIDDLE

HTML:

<header>
<nav>
    <div class="mob-nav">
        <div class="nav-toggle"><i class="nav-icon"></i></div>
    </div>
    <ul class="left-nav">   
        <li class="home"><a href="#">Pixelation</a></li>    
    </ul>
    <ul class="right-nav">  
        <li><a href="#">Work</a></li>
        <li><a href="#">About</a></li>
    </ul>
</nav>
</header>

3 个答案:

答案 0 :(得分:3)

在jQuery中使用scroll函数,如下所示 -

$(document).ready(function(){
	var iScrollPos = 0;
	$(window).scroll(function () {
	    var iCurScrollPos = $(this).scrollTop();
	    if (iCurScrollPos > iScrollPos) {
	        $('header').fadeOut(500);
	    } else {
	       $('header').fadeIn(500);
	    }
	    iScrollPos = iCurScrollPos;
	});
});
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box; /* width and height value includes content, padding and border not the margin */
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-tap-highlight-color: rgba(0,0,0,0); /* remove highlight on touch devices */
  -webkit-tap-highlight-color: transparent;
}

html {
  position: relative;
  min-height: 100%; /* sticky footer  */
  overflow-y: scroll;
  height: 2500px;
}

body {
  font-family:  "Helvetica Neue", helvetica, arial, sans-serif;
  font-weight: normal;
  font-size: 18px;
  line-height: 1.4;
  letter-spacing: 1px;
  color: #000;
}

body, input, textarea, select, button {
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  margin: 0;
  padding: 0;
  z-index: 9999;
  background-color: #eee;
}

.mob-nav {
  display: none; /* hide mobile nav */
}

nav ul.left-nav {
  float: left;
}

nav ul.right-nav {
  float: right;
}

nav {
  position: relative;
  padding: 0 5px;
}

nav ul {
  list-style: none;
  font-size: 0; /* remove default spacing */
}

nav li {
  display: inline-block;
}

nav a {
  display: block;
  font-size: 18px;
  font-weight: normal;
  text-transform: uppercase;
  text-align: center;
  text-decoration: none;
  background-color: transparent;
  padding: 0 10px;
  line-height: 44px;
}

nav a:link, 
nav a:visited {
  color: #111;
  text-decoration: none;
}

nav a:hover,
nav a:active {
  color: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
	<nav>
		<div class="mob-nav">
			<div class="nav-toggle"><i class="nav-icon"></i></div>
		</div>
		<ul class="left-nav">	
			<li class="home"><a href="#">Pixelation</a></li>	
		</ul>
		<ul class="right-nav">	
			<li><a href="#">Work</a></li>
			<li><a href="#">About</a></li>
		</ul>
	</nav>
 </header>

答案 1 :(得分:1)

试试这个..

 (function () {
      var previousScroll = 0;

   $(window).scroll(function () {
   var currentScroll = $(this).scrollTop();
   if (currentScroll > previousScroll){
      $('header').fadeOut(500);
   }
   else {
       $('header').fadeIn(500);
   }
   previousScroll = currentScroll;
  });
  }());

答案 2 :(得分:0)

如果想要隐藏,直到顶部滚动试试这个:

 (function () {
      var previousScroll = 0;

   $(window).scroll(function () {
   var currentScroll = $(this).scrollTop();
   if (currentScroll > previousScroll){
      $('#menu-bar').fadeOut(0);
   }
   else {
       $('#menu-bar').fadeIn(0);
   }
  });
  }());