按钮覆盖

时间:2020-04-01 08:06:47

标签: javascript html css overlay

问题是我想为我们出售的每顿饭都创建一个叠加层,我想通过“订购”按钮激活它,但是问题是我似乎不知道如何按顺序定位每个特定的按钮为每个按钮提供不同的覆盖物
这是HTML

    <div class="container">

        <div class="MenuItems">



            <img src="ribeye.jpg">
            <button onclick="on()">Order Now</button>
            <img src="Sirloin.jpg">
            <button onclick="on()">Order Now</button>
            <img src="Skirt.jpg">
            <button onclick="on()">Order Now</button>
            <img src="T-Bone.jpg">
            <button onclick="on()">Order Now</button>
            <img src="TomahawkSteak.jpg">
            <button onclick="on()">Order Now</button>
            <img src="Flank.jpg">
            <button onclick="on()">Order Now</button>
            <img src="Chuck.jpg">
            <button onclick="on()">Order Now</button>
            <img src="BBQChuck.jpg">
            <button onclick="on()">Order Now</button>
            <img src="ribeye.jpg">
            <button onclick="on()">Order Now</button>





        </div>

        <div class="left">
            <div class="SideMenu">
            <ul>
                <li><a href="Menu.html">Steak</a></li>
                <li><a href="Chicken.html">Chicken</a></li>
                <li><a href="Appietizers.html">Appietizers</a></li>
                <li><a href="beverages.html">Bevreges</a></li>
                <li><a href="Desserts.html">Desserts</a></li>
            </ul>

            </div>

        </div>
    </div>

这应该是叠加层之一,但是我需要创建多个叠加层并将其分配给我选择的任何按钮

    <div id="over">


        <img src="r.png">
        <Button class="AddBtn">-</Button>
        <Button class="SubtractBtn">+</Button>
        <Button class="CloseBtn" onclick="off()">X</Button>




    </div>
















    <script type="text/javascript" src="MenuJS.js"></script>

</body>

</html>

JavaScript:

function on() {
    document.getElementById("over").style.display = "block";

  }

  function off() {
    document.getElementById("over").style.display = "none";
  }

这是CSS:

#overlay {
    position: fixed;
    display: none;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 2;
    cursor: pointer;
  }

  #over{
    position: fixed;
    display: none;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 3;
    cursor: pointer;
  }


  #over img {
    position: relative;
    left: 35%;
    top: 25%;
    border: 2px crimson;
    width: fit-content;
    border-style: inset;
    height: 280px;
    width: 280px;
  }

  .CloseBtn {

  background-color: crimson;
  font-size: larger;
  color: white;
  border: solid 1px crimson;
  left: 1500px;
  border-radius: 5px;
  position: relative;
  bottom: 220px;


  }

  .AddBtn {
    background-color: crimson;
    font-size: larger;
  color: white;
  border: solid 1px crimson;
  border-radius: 5px;
  position: relative;
  left: 750px;
  top: 200px;
  width: 30px;
}


  .SubtractBtn {
    background-color: crimson;
    font-size: larger;
  color: white;
  border: solid 1px crimson;
  border-radius: 5px;
  position: relative;
  left: 760px;
  top: 200px;
  width: 30px;
}

1 个答案:

答案 0 :(得分:0)

调用函数时,可以传递this作为参数,并访问通过函数中的event.target单击的元素

HTML:

<button onclick="on(this)">Order Now</button>

JS:

function on() {
  console.log(event.target)
}

对于您要寻找的功能,您的方法将取决于您如何构造HTML。

对于最基本的方法,您可以执行以下操作:

HTML:

    <button onclick="on(this)">Order Now</button>
    <div class="over">
        <img src="r.png">
        <p>overlay 1</p>
        <Button class="AddBtn">-</Button>
        <Button class="SubtractBtn">+</Button>
        <Button class="CloseBtn" onclick="off()">X</Button>
    </div>

    <button onclick="on(this)">Order Now</button>
    <div class="over">
        <img src="r.png">
        <p>overlay 2</p>
        <Button class="AddBtn">-</Button>
        <Button class="SubtractBtn">+</Button>
        <Button class="CloseBtn" onclick="off()">X</Button>
    </div>

    <button onclick="on(this)">Order Now</button>
    <div class="over">
        <img src="r.png">
        <p>overlay 3</p>
        <Button class="AddBtn">-</Button>
        <Button class="SubtractBtn">+</Button>
        <Button class="CloseBtn" onclick="off()">X</Button>
    </div>

JS:

function on() {
  event.target.nextElementSibling.style.display = 'block'
}

function off() {
  event.target.parentElement.style.display = 'none'
}

这是利用nextElementSibling(因为按钮之后的下一个元素是叠加层)和parentElementcloseBtn的容器是叠加层)