【问题标题】:How would I add search bar to off canvas menu?如何将搜索栏添加到画布外菜单?
【发布时间】:2021-02-03 10:20:30
【问题描述】:

您好,我怎样才能将搜索栏添加到单击搜索栏时不会关闭菜单的画布外菜单?

【问题讨论】:

  • 您好,您可以提供一些代码吗?这确实有助于其他人了解您想要做什么/允许他们提供建设性的答案。谢谢:)

标签: javascript html css menu searchbar


【解决方案1】:

(下次您可以向我们展示您的代码,以便我们编辑您的代码)

我有一个来自@W3Schools 的例子。也许对你有帮助。

@W3Schools Click to open their website

您可以将<a href="#">Example</a> 替换为<a href="#"><input type="text"></a>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: "Lato", sans-serif;
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

#main {
  transition: margin-left .5s;
  padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
</head>
<body>

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#"><input type="search" placeholder="Search"></a>
</div>

<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
</div>

<script>
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft= "0";
}
</script>
   
</body>
</html> 

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2016-08-28
    • 2016-05-28
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多