首頁  >  問答  >  主體

處理不同覆蓋層ID的方法:使用相同的JavaScript函數

<p>我想要為不同的連結顯示覆蓋層。第一個覆蓋層包含圖片和文本,而如果我們點擊連結2,它必須顯示具有相同結構但不同內容的覆蓋層2。請幫助我獲得所需的輸出。 </p> <p> <pre class="brush:js;toolbar:false;">function on() { document.getElementById("overlay").style.display = "block"; } function on1() { document.getElementById("overlay1").style.display = "block"; } function off() { document.getElementById("overlay").style.display = "none"; }</pre> <pre class="brush:css;toolbar:false;">.img { transition: transform 5s ease-in-out; transform: scale(1); transform-origin: 0 0; } .img:hover { transform: scale(1.25) } #overlay, #overlay1 { position: fixed; display: none; width: 100%; height: 100%; top: 0; left: 0; right: 0; bottom: 0; background-color: white; z-index: 77777772; cursor: pointer; } #text, #text1 { position: fixed; top: 20%; left: 5%; //font-size: 50px; color: black; // transform: translate(-50%,-50%); // -ms-transform: translate(-50%,-50%); }</pre> <pre class="brush:html;toolbar:false;"><div id="overlay" onclick="off()"> <div id="text"> <div style="width: 48%; float:left"> <h2>XXX</h2> <h4>ZZZ</h4> <p style="font-size:14px;"> 幫助發掘居住在山谷中的人們的巨大才華。 </p> </div> <div style="width: 50%; float:right; margin-top:-220px; "> <img class="img" style="height:100%" src="http://cdn.dpmag.com/2016/06/boydB3_6881-683x1024.jpg"> </div> </div> <div style="padding:20px"> <h2></h2> <a onclick="on()"></a> </div> <div id="overlay1" onclick="on1()"> <div id="text1"> <div style="width: 48%; float:left"> <h2>AAA</h2> <h4>MMM</h4> <p style="font-size:14px;">幫助發掘居住在山谷中的人們的巨大才華。 </p> </div> <div style="width: 50%; float:right; margin-top:-220px; "> <img class="img" style="height:100%" src="http://cdn.dpmag.com/2016/06/002boydB37683-703x1024.jpg"> </div> </div> <div style="padding:20px"> <h2></h2> <a onclick="on1()"></a> </div> </div> </div> <a onclick="on(id)"style="font-size: 11pt;">讀更多 -></a> <a onclick="on(id)"style="font-size: 11pt;">讀更多 -></a> 1. 列表項目</pre> </p>
P粉511757848P粉511757848410 天前437

全部回覆(1)我來回復

  • P粉978742405

    P粉9787424052023-09-05 16:50:03

    有更好的方法來實現這個,但根據您的要求,您可以將不同的idsactions作為params傳遞給同一個函數,像這樣

    function on() {
      document.getElementById("overlay").style.display = "block";
    }
    
    function on1() {
      document.getElementById("overlay1").style.display = "block";
    }
    
    function off() {
      document.getElementById("overlay").style.display = "none";
    }
    
    function toggle(id, value) {
      document.getElementById(id).style.display = value;
    }
    .img {
      transition: transform 5s ease-in-out;
      transform: scale(1);
      transform-origin: 0 0;
    }
    
    .img:hover {
      transform: scale(1.25)
    }
    
    #overlay,
    #overlay1 {
      position: fixed;
      display: none;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: white;
      z-index: 77777772;
      cursor: pointer;
    }
    
    #text,
    #text1 {
      position: fixed;
      top: 20%;
      left: 5%;
      font-size: 50px;
      color: black;
      transform: translate(-50% -50%);
      -ms-transform: translate(-50% -50%);
    }
    <div id="overlay" onclick="toggle('overlay', 'none')">
      <div id="text">
        <div style="width: 48%; float:left">
          <h2>XXX</h2>
          <h4>ZZZ</h4>
          <p style="font-size:14px;">
            帮助发掘居住在山谷中的巨大才能。 </p>
        </div>
        <div style="width: 50%; float:right;   margin-top:-220px;
        ">
          <img class="img" style="height:100%" src="http://cdn.dpmag.com/2016/06/boydB3_6881-683x1024.jpg">
        </div>
      </div>
      <div style="padding:20px">
        <h2></h2>
        <a onclick="toggle('overlay', 'block')"></a>
      </div>
      <div id="overlay1" onclick="toggle('overlay1', 'block')">
        <div id="text1">
          <div style="width: 48%; float:left">
            <h2>AAA</h2>
            <h4>MMM</h4>
            <p style="font-size:14px;">帮助发掘居住在山谷中的巨大才能。 </p>
          </div>
          <div style="width: 50%; float:right;   margin-top:-220px;
        ">
            <img class="img" style="height:100%" src="http://cdn.dpmag.com/2016/06/002boydB37683-703x1024.jpg">
          </div>
        </div>
        <div style="padding:20px">
          <h2></h2>
          <a onclick="toggle('overlay1', 'block')"></a>
        </div>
      </div>
    </div>
    
    
    <a onclick="toggle('overlay', 'block')" style="font-size: 11pt;">阅读更多 -></a>
    <a onclick="toggle('overlay', 'block')" style="font-size: 11pt;">阅读更多 -></a> 1. 列表项

    回覆
    0
  • 取消回覆