为了账号安全,请及时绑定邮箱和手机立即绑定

如果滚动页面时移动导航栏处于打开状态,如何关闭?

如果滚动页面时移动导航栏处于打开状态,如何关闭?

FFIVE 2022-08-04 16:09:37
如果导航栏在网站的移动视图中打开,如何在页面滚动时关闭导航栏?最简单的方法(css或js)
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

好吧,对我来说,我更喜欢使用JS。这是我从w3schools得到的一个例子,它正在与导航栏的Web和移动视图一起使用


var prevScrollpos = window.pageYOffset;

window.onscroll = function() {

  var currentScrollPos = window.pageYOffset;

  if (prevScrollpos > currentScrollPos) {

    document.getElementById("navbar").style.top = "0";

  } else {

    document.getElementById("navbar").style.top = "-50px";

  }

  prevScrollpos = currentScrollPos;

}

body {

  margin: 0;

  background-color: #f1f1f1;

  font-family: Arial, Helvetica, sans-serif;

}


#navbar {

  background-color: #333;

  position: fixed;

  top: 0;

  width: 100%;

  display: block;

  transition: top 0.3s;

}


#navbar a {

  float: left;

  display: block;

  color: #f2f2f2;

  text-align: center;

  padding: 15px;

  text-decoration: none;

  font-size: 17px;

}


#navbar a:hover {

  background-color: #ddd;

  color: black;

}

<body>


<div id="navbar">

  <a href="#home">Home</a>

  <a href="#news">News</a>

  <a href="#contact">Contact</a>

</div>


<div style="padding:15px 15px 2500px;font-size:30px;margin-top:30px;">

  <p><b>This example demonstrates how to hide a navbar when the user starts to scroll the page.</b></p>

  <p>Scroll down this frame to see the effect!</p>

  <p>Scroll up to show the navbar.</p>

  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

</div>


</body>


查看完整回答
反对 回复 2022-08-04
  • 1 回答
  • 0 关注
  • 200 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号