JavaScript中的响应式导航菜单

时间:2018-05-20 18:45:42

标签: javascript html css

我正在尝试为我的网站构建一个响应式导航菜单。但是,当您向下滚动页面时,它也应该粘在顶部。这适用于普通的桌面尺寸'菜单,但是当我在响应式导航菜单中展开列表项时,整个菜单会将自己重新定位到顶部。

console.log('bot in orders');
var Discord = require(‘discord.js’);
var bot = new Discord.Client();

bot.on('menssage', (menssage) => {
    if (message.contenet == 'hi') {
        message.channel.sendMessage('Hi! :D');
    }
});
bot.login('TOKEN');

我的JSFiddle:https://jsfiddle.net/MihkelPajunen/t37g6hsc/

问题:有没有办法让自适应导航菜单变得粘稠,以便在页面顶部可以访问菜单,即使您向下滚动页面也是如此?

2 个答案:

答案 0 :(得分:0)

删除以下规则:

nav.responsive {
    position: relative;
}

因为这会覆盖position: fixed规则。

另外,更改崩溃功能如下:

if (navbar.className.indexOf("responsive") >=0 ) {

因为班级名称实际上类似于"响应粘性"

Here's a fork of that fiddle

答案 1 :(得分:0)

尝试使用此而不是你的

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
</style>
</head>
<body>

<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
</div>

<div style="padding-left:16px">
  <h2>Responsive Topnav Example</h2>
  <p>Resize the browser window to see how it works.</p>
</div>

<script>
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>

</body>
</html>