将div转换成手机屏幕尺寸上的链接

时间:2018-08-20 20:30:30

标签: html css

我有一个带有某些链接的导航栏。当我打开屏幕尺寸较小的页面时,移动菜单会替换默认的导航栏。

当我打开此移动菜单时,我也想将链接的父div也转换为链接。意味着当我单击此div时,我要调用链接路线。

我提供了移动菜单的完整示例

$(document).ready(() => {
  $("#btnMenu").click(() => {
    toggleMenu();
  });

  $(".navbarLink").click(() => {
    if ($("#navbarItems").hasClass("activeNavbar")) {
      toggleMenu();
    }
  });
});

function toggleMenu() {
  $("#navbarItems").toggleClass("activeNavbar");
  toggleMenuBtn();
}

function toggleMenuBtn() {
  $("#btnMenu").toggleClass("activeMenuBtn");
}
.link {
  text-decoration: none;
}

body {
  margin: 0;
}

#navbar {
  height: 60px;
  top: 0;
  padding-left: 200px;
  padding-right: 200px;
  position: sticky;
  background: #1e222a;
}

#navbarItems {
  height: 100%;
  display: flex;
  align-items: center;
}

#logoLink {
  display: flex;
  align-items: center;
}

#navbarItems .navbarItemContainer:not(:first-child) {
  margin-left: 30px;
}

.navbarItemContainer {
  background: #1e222a;
}

.navbarLink {
  font-weight: bold;
  color: #ffffff;
}

.navbarLink:hover {
  color: #3abcf3;
}

#btnMenuContainer {
  height: 100%;
  display: none;
}

#btnMenu {
  cursor: pointer;
}

.menuBtnBar {
  width: 35px;
  height: 5px;
  margin: 6px 0;
  background-color: #ffffff;
  transition: 0.4s;
}

.activeMenuBtn #barTop {
  transform: rotate(-45deg) translate(-9px, 6px);
}

.activeMenuBtn #barCenter {
  opacity: 0;
}

.activeMenuBtn #barBottom {
  transform: rotate(45deg) translate(-8px, -8px);
}

@media(max-width: 1200px) {
  #navbar {
    padding-left: 150px;
    padding-right: 150px;
  }
}

@media(max-width: 1100px) {
  #navbar {
    padding-left: 0;
    padding-right: 0;
  }
  #navbarItems .navbarItemContainer:not(:first-child) {
    margin-left: 0;
  }
  #navbarItems .navbarItemContainer:not(:last-child) {
    border-bottom: 1px solid #676767;
  }
  #btnMenuContainer {
    display: flex;
    align-items: center;
  }
  #btnMenu {
    margin-left: 20px;
  }
  #navbarItems {
    margin-left: 0;
    display: none;
  }
  #logoLink {
    display: inline-block;
  }
  .navbarItem {
    width: 100%;
    text-align: center;
    padding: 30px 0;
  }
  #navbarItems.activeNavbar {
    display: block;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar">
  <div id="btnMenuContainer">
    <div id="btnMenu">
      <div id="barTop" class="menuBtnBar"></div>
      <div id="barCenter" class="menuBtnBar"></div>
      <div id="barBottom" class="menuBtnBar"></div>
    </div>
  </div>

  <div id="navbarItems">
    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a id="logoLink" class="link navbarLink" href="#">
          <img class="img" src="http://icons.iconarchive.com/icons/ph03nyx/super-mario/128/Hat-Mario-icon.png">
        </a>
      </div>
    </div>

    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a class="link navbarLink" href="#sectionTwo">
                    Link 2
                </a>
      </div>
    </div>

    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a class="link navbarLink" href="#sectionThree">
                    Link 3
                </a>
      </div>
    </div>
  </div>
</div>

打开移动菜单时,如何在整个父div的中心展开链接?

3 个答案:

答案 0 :(得分:1)

进入@media(max-width: 1100px),然后从其中删除填充

  .navbarItem {
    width: 100%;
    text-align: center;
    padding: 30px 0;
  }

接下来将其添加到您的CSS(小于1101px)

  .navbarLink {
    width: 100%;
    height: 100%;
    display: inline-block;
  }

  .navbarLink:hover {
    background: red;
  }

并将删除的填充添加到navbarLink padding: 30px 0;

  .navbarLink {
    width: 100%;
    height: 100%;
    display: inline-block;
    padding: 30px 0;
  }

工作示例:

$(document).ready(() => {
  $("#btnMenu").click(() => {
    toggleMenu();
  });

  $(".navbarLink").click(() => {
    if ($("#navbarItems").hasClass("activeNavbar")) {
      toggleMenu();
    }
  });
});

function toggleMenu() {
  $("#navbarItems").toggleClass("activeNavbar");
  toggleMenuBtn();
}

function toggleMenuBtn() {
  $("#btnMenu").toggleClass("activeMenuBtn");
}
.link {
  text-decoration: none;
}

body {
  margin: 0;
}

#navbar {
  height: 60px;
  top: 0;
  padding-left: 200px;
  padding-right: 200px;
  position: sticky;
  background: #1e222a;
}

#navbarItems {
  height: 100%;
  display: flex;
  align-items: center;
}

#logoLink {
  display: flex;
  align-items: center;
}

#navbarItems .navbarItemContainer:not(:first-child) {
  margin-left: 30px;
}

.navbarItemContainer {
  background: #1e222a;
}

.navbarLink {
  font-weight: bold;
  color: #ffffff;
}

.navbarLink:hover {
  color: #3abcf3;
}

#btnMenuContainer {
  height: 100%;
  display: none;
}

#btnMenu {
  cursor: pointer;
}

.menuBtnBar {
  width: 35px;
  height: 5px;
  margin: 6px 0;
  background-color: #ffffff;
  transition: 0.4s;
}

.activeMenuBtn #barTop {
  transform: rotate(-45deg) translate(-9px, 6px);
}

.activeMenuBtn #barCenter {
  opacity: 0;
}

.activeMenuBtn #barBottom {
  transform: rotate(45deg) translate(-8px, -8px);
}

@media(max-width: 1200px) {
  #navbar {
    padding-left: 150px;
    padding-right: 150px;
  }
}

@media(max-width: 1100px) {
  #navbar {
    padding-left: 0;
    padding-right: 0;
  }
  #navbarItems .navbarItemContainer:not(:first-child) {
    margin-left: 0;
  }
  #navbarItems .navbarItemContainer:not(:last-child) {
    border-bottom: 1px solid #676767;
  }
  #btnMenuContainer {
    display: flex;
    align-items: center;
  }
  #btnMenu {
    margin-left: 20px;
  }
  #navbarItems {
    margin-left: 0;
    display: none;
  }
  #logoLink {
    display: inline-block;
  }
  .navbarItem {
    width: 100%;
    text-align: center;
  }
  #navbarItems.activeNavbar {
    display: block;
  }
  .navbarLink {
    width: 100%;
    height: 100%;
    display: inline-block;
    padding: 30px 0;
  }
  .navbarLink:hover {
    background: red;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar">
  <div id="btnMenuContainer">
    <div id="btnMenu">
      <div id="barTop" class="menuBtnBar"></div>
      <div id="barCenter" class="menuBtnBar"></div>
      <div id="barBottom" class="menuBtnBar"></div>
    </div>
  </div>

  <div id="navbarItems">
    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a id="logoLink" class="link navbarLink" href="#">
          <img class="img" src="http://icons.iconarchive.com/icons/ph03nyx/super-mario/128/Hat-Mario-icon.png">
        </a>
      </div>
    </div>

    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a class="link navbarLink" href="#sectionTwo">
                    Link 2
                </a>
      </div>
    </div>

    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a class="link navbarLink" href="#sectionThree">
                    Link 3
                </a>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:-2)

尝试将您的移动样式替换为

.navbarItem {
  width: 100%;
  text-align: center;
  padding: 30px 0;
}

使用

#navbarItems:first-child .navbarItem, .navbarLink {
  width: 100%;
  text-align: center;
  padding: 30px 0;
}

这应该像现在一样将这些样式应用于徽标项目,然后将填充和宽度应用于实际导航栏链接的链接本身。

答案 2 :(得分:-2)