Way to handle different overlay IDs: use the same JavaScript function
P粉511757848
P粉511757848 2023-09-04 22:36:35
0
1
456
<p>I want to show overlays for different links. The first overlay contains image and text, while if we click on link 2, it must show overlay 2 with the same structure but different content. Please help me to get the desired output. </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;"> Help discover the immense talents of the people who live in the valley. </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;">Help discover the immense talents of the people who live in the valley. </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;">Read more -></a> <a onclick="on(id)"style="font-size: 11pt;">Read more -></a> 1. List items</pre> </p>
P粉511757848
P粉511757848

reply all(1)
P粉978742405

There are better ways to achieve this, but depending on your requirements you can pass different ids and actions as params to the same one Function, like this

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. 列表项
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!