猿问

滚动页面后更改导航栏

我想知道如何在滚动页面后更改导航栏。

开始的情况是,一旦用户到达网站,他就会找到一个导航栏,一旦他滚动页面,另一个导航栏就会立即出现,就像这个主题中发生的那样: https://kendall.qodeinteractive.com/manicure/



慕斯王
浏览 107回答 3
3回答

梦里花落0921

有多种方法可以做到这一点,但最简单的是使用两个导航栏。<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Navbar</title><style>*,*:before,*:after {&nbsp; margin: 0;&nbsp; font-family: sans-serif;}section {&nbsp; width: 100%;&nbsp; height: 2500px;}.header {&nbsp; box-sizing: border-box;&nbsp; position: absolute;&nbsp; top: 0;&nbsp; width: 100%;&nbsp; height: 400px;&nbsp; text-align: center;&nbsp; background-color: #33f;&nbsp; color: #fff;}.navigation {&nbsp; box-sizing: border-box;&nbsp; position: fixed;&nbsp; width: 100%;&nbsp; height: 60px;&nbsp; background-color: #ff4d4d;&nbsp; color: #fff;&nbsp; padding: 20px 50px;&nbsp; transition: all 0.5s ease;&nbsp; transform: translateY(-100%);&nbsp; z-index: 9999;}.navigation--relative {&nbsp; position: relative;&nbsp; top: 0px;&nbsp; transform: translateY(0%);}.navigation.is-fixed {&nbsp; position: fixed;&nbsp; width: 100%;&nbsp; transform: translateY(0%);}</style></head><body><section><nav class='navigation'><div class='navigation__title'><h1>Navbar 1</h1></div></nav><header class='header header-content'><nav class='navigation navigation--relative'><div class='navigation__title'><h1>Navbar 2</h1></div></nav><h1>Content</h1></header></section><script>function stickyElement(e) {&nbsp; var header = document.querySelector('.header');&nbsp; var headerHeight = getComputedStyle(header).height.split('px')[0];&nbsp; var navbar = document.querySelector('.navigation');&nbsp; var scrollValue = window.scrollY;&nbsp; if (scrollValue > headerHeight) {&nbsp; &nbsp; navbar.classList.add('is-fixed');&nbsp; } else if (scrollValue < headerHeight) {&nbsp; &nbsp; navbar.classList.remove('is-fixed');&nbsp; }}window.addEventListener('scroll', stickyElement);&nbsp; &nbsp; </script></body></html>JSFiddle 演示

大话西游666

这是我认为您正在尝试实现的目标的一个有效示例。除了一些样式和对 JS 的一些小调整之外,您几乎已经完成了。JSFiddle 工作示例<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Navbar</title><style>*,*:before,*:after {&nbsp; margin: 0;&nbsp; font-family: sans-serif;}body {&nbsp; background-color: brown;&nbsp; top: 0;&nbsp; min-height: 100vh;&nbsp; width: 100vw;}section {&nbsp; width: 100%;&nbsp; height: 2500px;}.header {&nbsp; box-sizing: border-box;&nbsp; position: absolute;&nbsp; top: 20px;&nbsp; width: 80%;&nbsp; left: 10%;&nbsp; height: auto;&nbsp; text-align: center;&nbsp; background-color: #33f;&nbsp; color: #fff;}.navigation {&nbsp; box-sizing: border-box;&nbsp; position: fixed;&nbsp; width: 100%;&nbsp; height: 60px;&nbsp; background-color: #ff4d4d;&nbsp; color: #fff;&nbsp; padding: 20px 50px;&nbsp; transition: all 0.5s ease;&nbsp; transform: translateY(-100%);&nbsp; z-index: 9999;}.navigation--relative {&nbsp; position: relative;&nbsp; top: 0px;&nbsp; transform: translateY(0%);}.content {&nbsp; position: relative;&nbsp; top: 100px;&nbsp; &nbsp;width: 80%;&nbsp; &nbsp;left: 10%;&nbsp; height: 100%;&nbsp;&nbsp; &nbsp; background-color: purple;}.navigation.is-scrolled {&nbsp; position: fixed;&nbsp; background-color: green;&nbsp; width: 100%;&nbsp; transform: translateY(0%);}</style></head><body><section><nav class='navigation'><div class='navigation__title'><h1>Navbar 2</h1></div></nav><header class='header header-content'><nav class='navigation navigation--relative'><div class='navigation__title'><h1>Navbar 1</h1></div></nav></header><div class="content"><h1>Content</h1></div></section><script>function stickyElement(e) {&nbsp; var header = document.querySelector('.header');&nbsp; var headerHeight = getComputedStyle(header).height.split('px')[0];&nbsp; var navbar = document.querySelector('.navigation');&nbsp; var scrollValue = window.scrollY;&nbsp; if (scrollValue > 101) {&nbsp; &nbsp; navbar.classList.add('is-scrolled');&nbsp; } else if (scrollValue < 100) {&nbsp; &nbsp; navbar.classList.remove('is-scrolled');&nbsp; }}window.addEventListener('scroll', stickyElement);</script></body></html>

慕姐8265434

实际上这个主题有2个导航栏,在Jquery中使用ScrollTop来捕获事件滚动,然后使用隐藏第一个导航栏并显示2个固定导航栏。
随时随地看视频慕课网APP

相关分类

Html5
我要回答