如何创建响应式下拉菜单,如http://www.brotherbots.com/

时间:2017-01-24 06:08:16

标签: javascript html css

我想知道如何在移动和桌面视图中将下拉列表视为http://www.brotherbots.com/上的下拉列表。我结合了w3schools的一些例子。它适用于桌面视图,但不适用于移动设备。我不喜欢造型,但我想我可以自己解决它。这也是最好的方法吗?

我有一个jsfiddle here



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

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}
li {
  float: left;
}
li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
li.dropdown {
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}
.dropdown-content a:hover {
  background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
  display: block;
}
ul.topnav li a:hover {
  background-color: #555;
}
ul.topnav li.icon {
  display: none;
}
@media screen and (max-width: 460px) {
  ul.topnav li:not(:first-child) {
    display: none;
  }
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}
@media screen and (max-width: 460px) {
  ul.topnav.responsive {
    position: relative;
  }
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}

<ul class="topnav" id="myTopnav">
  <li><a class="active" href="#home">Home</a>
  </li>
  <li><a href="#news">News</a>
  </li>
  <li><a href="#contact">Contact</a>
  </li>
  <li><a href="#about">About</a>
  </li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
  <li class="icon">
    <a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">☰</a>
  </li>
</ul>

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

2 个答案:

答案 0 :(得分:0)

你可以使用meterialised drop down ....

例如 CSS

$grey: #9e9e9e;
$plum: #8e44ad;
$shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 8px 0 rgba(0, 0, 0, 0.12);

h1, a, li {
  font-family: Roboto, sans-serif;
}

h1 {
  font-size: 26px;
  background: lighten($plum, 5%);
  color: white;
  padding: 40px 0 40px 20%;
  margin-bottom: 50px;
}

a, li {
  color: darken($grey, 20%);
  text-decoration: none;
}

.nav {
  margin-left: 20%;
  > li {
    display: inline-block;
    padding: 1em 18px;
    cursor: pointer;
    &:hover {
      background: lighten($grey, 30%);
    }
  }
}

.drop {
  position: relative;
  ul {
    position: absolute;
    left: 0;
    top: 3em;
    transition: all 0.3s ease;
    transform: scale(0);
    transform-origin: 0 0;
    box-shadow: $shadow;
    li {
      display: block;
      width: 100%;
      a {
        width: 100%;
        padding: 1em 18px;
        display: inline-block;
        white-space: pre;
        box-sizing: border-box;
        &:hover {
          background: lighten($grey, 30%);
        }
      }
    }
  }
  &:hover {
    ul {
      transform: scale(1);
    }
  }
}

//---------------------------------------------

.follow {
  width: 42px;
  height: 42px;
  border-radius: 50px;
  background: #03A9F4;
  display: block;
  margin: 300px auto 0;
  white-space: nowrap;
  padding: 13px;
  box-sizing: border-box;
  color: white;
  transition: all 0.2s ease;
  font-family: Roboto, sans-serif;
  text-decoration: none;
  box-shadow: 0 5px 6px 0 rgba(0,0,0,0.2);
  i {
    margin-right: 20px;
    transition: margin-right 0.2s ease;
  }
  &:hover {
    width: 134px;
    i {
      margin-right: 10px;
    }
  }
}

@media screen and (max-width: 800px) {
  .follow {
    margin: 400px auto 0;
  }
}

HTML

<h1>Material Design CSS Only Dropdown Menu</h1>

<ul class="nav">
  <li><a href="http://www.g.com">Home</a></li>
  <li><a href="http://www.g.com">Portfolio</a></li>
  <li class="drop">Drop
    <ul>
      <li><a href="http://www.g.com">Art</a></li>
      <li><a href="http://www.g.com">Coding</a></li>
      <li><a href="http://www.g.com">Design</a></li>
      <li><a href="http://www.g.com">Web Development</a></li>
    </ul>
  </li>
  <li><a href="http://www.g.com">Contact</a></li>
</ul>

<a class="follow" href="https://twitter.com/mildrenben" target="_blank"><i class="fa fa-twitter"></i>Follow Me</a>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<link href='http://fonts.googleapis.com/css?family=Roboto:400,500,300,700' rel='stylesheet' type='text/css'>

答案 1 :(得分:0)

我建议您查看Bootstrap。如果您不是造型的粉丝并且他们的网站上有navbar section的良好文档,那将会有所帮助。不幸的是,他们没有“快速响应能力”。你正在寻找,但我找到了一个JSFiddle,有人为此添加了CSS。

&#13;
&#13;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"cell";

    customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    customCell.oneYear.tag = indexPath.row;

    customCell.threeYearButton.tag = indexPath.row + 50;
[customCell.oneYear addTarget:self
                    action:@selector(oneYearButtonAction)
          forControlEvents:UIControlEventTouchUpInside];

[customCell.threeYearButton addTarget:self
                       action:@selector(threeYearButtonAction)
             forControlEvents:UIControlEventTouchUpInside];

[customCell setSelectionStyle:UITableViewCellSelectionStyleNone];
&#13;
.sidebar-nav {
    padding: 9px 0;
}

.dropdown-menu .sub-menu {
    left: 100%;
    position: absolute;
    top: 0;
    visibility: hidden;
    margin-top: -1px;
}

.dropdown-menu li:hover .sub-menu {
    visibility: visible;
}

.dropdown:hover .dropdown-menu {
    display: block;
}

.nav-tabs .dropdown-menu, .nav-pills .dropdown-menu, .navbar .dropdown-menu {
    margin-top: 0;
}

.navbar .sub-menu:before {
    border-bottom: 7px solid transparent;
    border-left: none;
    border-right: 7px solid rgba(0, 0, 0, 0.2);
    border-top: 7px solid transparent;
    left: -7px;
    top: 10px;
}
.navbar .sub-menu:after {
    border-top: 6px solid transparent;
    border-left: none;
    border-right: 6px solid #fff;
    border-bottom: 6px solid transparent;
    left: 10px;
    top: 11px;
    left: -6px;
}
&#13;
&#13;
&#13;

希望这有帮助。