javascript - 按钮定位在完全错误的位置

标签 javascript html css button position

我有一个包含 2 个不同部分的网页。每个都是视口(viewport)的高度。一个在中央有一个“工作”按钮。单击此按钮后,它会消失,一些链接会出现在原来的位置。同样的事情适用于下一节。

我正在尝试添加重置功能以删除链接并重新添加按钮。我最初试图让一个按钮重置所有部分,但在这不起作用之后,我现在正在尝试为每个部分制作一个单独的按钮。

我已经这样做了,但我的问题是第二部分的重置按钮出现在与第一部分相同的位置。两者都应出现在各自部分的右下角。

function openSites(categoryType) {
  if (categoryType == "work") {
    var sites = document.getElementById("workSites");
    var button = document.getElementById("workButton");
  } else if (categoryType == "other") {
    var sites = document.getElementById("otherSites");
    var button = document.getElementById("otherButton");
  }
  sites.classList.add("show");
  sites.classList.remove("hide");
  button.classList.add("hide");
}

function reset(categoryType) {
  if (categoryType == "work") {
    var sites = document.getElementById("workSites");
    var button = document.getElementById("workButton");
  } else if (categoryType == "other") {
    var sites = document.getElementById("otherSites");
    var button = document.getElementById("otherButton");
  }
  sites.classList.remove("show");
  sites.classList.add("hide");
  button.classList.remove("hide");
}

function betterReset() {
  for (category in document.getElementsByClassName("category-container")) {
    document.write(category);
  }
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.page {
  display: inline-block;
  overflow: hidden;
  width: 100%;
  height: 100vh;
}

#page-1 {
  display: block;
  background-color: #3faae4;
}

#page-2 {
  display: block;
  background-color: #ffffff;
}

.pointer {
  cursor: pointer;
}

#work {
  height: 100%;
  width: 100%;
}

#other {
  height: 100%;
  width: 100%;
}

#workSites {
  height: 100%;
  width: 100%;
}

#otherSites {
  height: 100%;
  width: 100%;
}

.sites {
  list-style-type: none;
  height: 100%;
}

.site {
  padding: 50px 0px;
  flex-grow: 1;
  text-align: center;
}

.center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.category-container {
  height: 100%;
}

.category-button {
  border: solid 0.5px;
  padding: 60px;
}

.buttonClose {
  position: absolute;
  border: solid 0.5px;
  border-radius: 5px;
  right: 3px;
  bottom: 0px;
  width: 70px;
  height: 35px;
}

.show {
  display: block;
}

.hide {
  display: none;
}
<!DOCTYPE html>
<html>
<head>
  <title>Nick's site</title>
  <link rel="stylesheet" type="text/css" href="./styles3.css">
  <meta name="viewport" content="width= device-width, inital-scale=1">
</head>
<body>
  <div id="page-1" class="page">
    <div id="work">
      <div id="workButton" class="category-container center">
        <a class="category-button pointer" onclick="openSites('work')">Work</a>
      </div>
      <div id="workSites" class="hide">
        <ul class="sites center">
          <li class="site"><a class="pointer" href="#">Printfactory</a></li>
          <li class="site"><a class="pointer" href="#">Henry's Site</a></li>
        </ul>
        <button onclick="reset('work')" class="buttonClose pointer" style="z-index: 2;">
         Reset
       </button>
      </div>
    </div>
  </div>
  <div id="page-2" class="page">
    <div id="other">
      <div id="otherButton" class="category-container center">
        <a class="category-button pointer" onclick="openSites('other')">Other</a>
      </div>
      <div id="otherSites" class="hide">
        <ul class="sites center">
          <li class="site"><a class="pointer" href="#">#</a></li>
          <li class="site"><a class="pointer" href="#">#</a></li>
          <li class="site"><a class="pointer" href="#">#</a></li>
        </ul>
        <button onclick="reset('other')" class="buttonClose pointer" style="z-index: 2;">
          Reset2
        </button>
      </div>
    </div>
  </div>
</body>
</html>

最佳答案

你正在给你的重置按钮一个 position:absolute 。这使它们采用相对于具有 position:relative 的下一个父级的 rightbottom 的值。在这种情况下是 body 标签。

要解决此问题,请将 position:relative 添加到您的父 div。

#workSites,
#otherSites {
  position: relative;
}

希望这有帮助:>

function openSites(categoryType) {
                if (categoryType == "work") {
                    var sites = document.getElementById("workSites");
                    var button = document.getElementById("workButton");
                } else if (categoryType == "other") {
                    var sites = document.getElementById("otherSites");
                    var button = document.getElementById("otherButton");
                }
                sites.classList.add("show");
                sites.classList.remove("hide");
                button.classList.add("hide");

            }
            function reset(categoryType) {
                if (categoryType == "work") {
                    var sites = document.getElementById("workSites");
                    var button = document.getElementById("workButton");
                } else if (categoryType == "other") {
                    var sites = document.getElementById("otherSites");
                    var button = document.getElementById("otherButton");
                }
                sites.classList.remove("show");
                sites.classList.add("hide");
                button.classList.remove("hide");
            }
            function betterReset() {
                for (category in document.getElementsByClassName("category-container")){
                    document.write(category);
                }
            }
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.page {
    display: inline-block;
    overflow: hidden;
    width: 100%;
    height: 100vh;
}
#page-1 {
    display: block;
    background-color: #3faae4;
}
#page-2 {
    display: block;
    background-color: #ffffff;
}
.pointer {
    cursor: pointer;
}
#work {
    height: 100%;
    width: 100%;
}
#other {
    height: 100%;
    width: 100%;
}
#workSites {
    height: 100%;
    width: 100%;
}
#otherSites {
    height: 100%;
    width: 100%;
}
.sites {
    list-style-type: none;
    height: 100%;
}
.site {
    padding: 50px 0px;
    flex-grow: 1;
    text-align: center;
}
.center {
    display: flex;
    align-items: center;
    justify-content: center;
}
.category-container {
    height: 100%;
}
.category-button {
    border: solid 0.5px;
    padding: 60px;
}
.buttonClose {
    position: absolute;
    border: solid 0.5px;
    border-radius: 5px;
    right: 3px;
    bottom: 0px;
    width: 70px;
    height: 35px;
}
.show {
    display: block;
}
.hide {
    display: none;
}

#workSites,
#otherSites {
  position: relative;
}
<!DOCTYPE html>
<html>
    <head>
        <title>Nick's site</title>
        <link rel="stylesheet" type="text/css" href="./styles3.css">
        <meta name="viewport" content="width= device-width, inital-scale=1">
    </head>
    <body>
        <div id="page-1" class="page">
            <div id="work">
                <div id="workButton" class="category-container center">
                    <a class="category-button pointer" onclick="openSites('work')">Work</a>
                </div>
                <div id="workSites" class="hide">
                    <ul class="sites center">
                        <li class="site"><a class="pointer" href="#">Printfactory</a></li>
                        <li class="site"><a class="pointer" href="#">Henry's Site</a></li>
                    </ul>
                    <button onclick="reset('work')" class="buttonClose pointer" style="z-index: 2;">
                        Reset
                    </button>
                </div>
            </div>
        </div>
        <div id="page-2" class="page">
            <div id="other">
                <div id="otherButton" class="category-container center">
                    <a class="category-button pointer" onclick="openSites('other')">Other</a>
                </div>
                <div id="otherSites" class="hide">
                    <ul class="sites center">
                        <li class="site"><a class="pointer" href="#">#</a></li>
                        <li class="site"><a class="pointer" href="#">#</a></li>
                        <li class="site"><a class="pointer" href="#">#</a></li>
                    </ul>
                    <button onclick="reset('other')" class="buttonClose pointer" style="z-index: 2;">
                        Reset2
                    </button>
                </div>
            </div>
        </div>
    </body>
</html>

关于javascript - 按钮定位在完全错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51140195/

相关文章:

javascript - 为什么我的 "add row to table"函数不起作用?

javascript - 动画功能 : change div width by using radio buttons

javascript - 我想在同一张卡片中显示数据作为两列任何建议

javascript - 如何通过非立即的类获取父元素的ID值? jQuery

javascript - 是否可以在均衡器高度上添加额外的高度?

javascript - 当我使用 AngularJS $routeProvider 时,Node.js 页面转换不再起作用

javascript - 为什么我不能使用 JavaScript(而不是 HttpOnly)使 cookie 过期

javascript - 在一个 div 中创建相同类型的两个标签

javascript - 在 Chrome 上获取最终生成的 HTML

IBM worklight 不显示 css 背景图像