html 顶部导航栏隐藏,js和css3智能隐藏和显示的顶部导航菜单

这是一款使用js和css3制作的智能隐藏和显示的顶部导航菜单。该顶部导航菜单在页面向下滚动时可以帧动画隐藏,在向上滚动到一定距离时优惠显示出来。

使用方法

HTML结构

使用元素作为该导航菜单的HTML结构:

首页

关于我们

联系我们

CSS样式

为导航菜单添加样式,使它固定在页面的顶部:

.header-navigation {

position: fixed;

top: 0;

width: 100%;

height: 60px;

line-height: 60px;

background-color: #333;

text-align: center;

box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);

}

导航菜单的隐藏和显示需要两个预定义的class类:

/*导航菜单向上滑动*/

.slideUp {

-webkit-transform: translateY(-100px);

transform: translateY(-100px);

transition: transform .5s ease-out;

}

/*导航菜单向下滑动*/

.slideDown {

-webkit-transform: translateY(0);

transform: translateY(0);

transition: transform .5s ease-out;

}

JavaScript

通过下面的JavaScript代码,在页面滚动到合适的位置时,隐藏或显示导航菜单。

var new_scroll_position = 0;

var last_scroll_position;

var header = document.getElementById("header");

window.addEventListener('scroll', function(e) {

last_scroll_position = window.scrollY;

// 向下滚动

if (new_scroll_position < last_scroll_position && last_scroll_position > 80) {

header.classList.remove("slideDown");

header.classList.add("slideUp");

// 向上滚动

} else if (new_scroll_position > last_scroll_position) {

header.classList.remove("slideUp");

header.classList.add("slideDown");

}

new_scroll_position = last_scroll_position;

});

201603282312454295.jpg

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个纵向滚动导航栏,可以使用 HTML、CSS 和 JavaScript 来完成。下面是一些步骤和示例代码,可以帮助你实现这个功能。 1. 首先,我们需要创建一个 HTML 结构来定义导航栏和它的菜单项。可以使用 `<ul>` 和 `<li>` 标签来创建一个有序列表,然后在列表项中添加链接。 ```html <nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> <li><a href="#section4">Section 4</a></li> </ul> </nav> ``` 2. 接下来,我们需要使用 CSS 来设置导航栏的样式。可以给 `<nav>` 元素设置一个固定的高度和宽度,并设置其 `overflow-y` 属性为 `scroll`,以便在内容溢出时显示滚动条。然后,可以设置 `<li>` 元素的样式,例如背景颜色、字体大小和间距。 ```css nav { height: 400px; width: 150px; overflow-y: scroll; } li { background-color: #f6f6f6; font-size: 16px; margin-bottom: 10px; padding: 10px; } ``` 3. 现在,我们需要使用 JavaScript 来添加一些交互性。我们可以使用 `window.addEventListener()` 方法来监听滚动事件,并在导航栏中高亮显示当前所在的节。 ```javascript window.addEventListener('scroll', function() { var sections = document.querySelectorAll('section'); var navHeight = document.querySelector('nav').offsetHeight; var currentSectionIndex = 0; for (var i = 0; i < sections.length; i++) { if (sections[i].offsetTop - navHeight <= window.scrollY) { currentSectionIndex = i; } } var activeLink = document.querySelector('nav li.active'); if (activeLink) { activeLink.classList.remove('active'); } var newActiveLink = document.querySelectorAll('nav li')[currentSectionIndex]; newActiveLink.classList.add('active'); }); ``` 在这段代码中,我们首先获取所有的节,并获取导航栏的高度。然后,我们遍历所有的节,如果某个节的顶部位置小于或等于滚动条的位置加上导航栏的高度,就将当前节的索引设置为 `currentSectionIndex`。最后,我们移除当前激活的菜单项的 `active` 类,然后将新的激活菜单项添加 `active` 类。 4. 最后,我们可以使用 CSS 来设置渐变效果。我们可以给 `.active` 类添加一个渐变背景色,使其从浅色逐渐变成深色。这样,用户就可以很容易地看出当前所在的节。 ```css li.active { background: linear-gradient(to bottom, #f6f6f6, #ccc); } ``` 这样,我们就完成了一个纵向滚动导航栏,并添加了滚动渐变效果。完整的 HTML、CSS 和 JavaScript 代码如下所示: ```html <!DOCTYPE html> <html> <head> <style> nav { height: 400px; width: 150px; overflow-y: scroll; } li { background-color: #f6f6f6; font-size: 16px; margin-bottom: 10px; padding: 10px; } li.active { background: linear-gradient(to bottom, #f6f6f6, #ccc); } </style> </head> <body> <nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> <li><a href="#section4">Section 4</a></li> </ul> </nav> <section id="section1">Section 1</section> <section id="section2">Section 2</section> <section id="section3">Section 3</section> <section id="section4">Section 4</section> <script> window.addEventListener('scroll', function() { var sections = document.querySelectorAll('section'); var navHeight = document.querySelector('nav').offsetHeight; var currentSectionIndex = 0; for (var i = 0; i < sections.length; i++) { if (sections[i].offsetTop - navHeight <= window.scrollY) { currentSectionIndex = i; } } var activeLink = document.querySelector('nav li.active'); if (activeLink) { activeLink.classList.remove('active'); } var newActiveLink = document.querySelectorAll('nav li')[currentSectionIndex]; newActiveLink.classList.add('active'); }); </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值