Which Correct Way to Apply Lightbox to Each Descriptive Link and Clear

Asked

Viewed 65 times

0

I’m on a little something. I need your help for a change.

I’m practicing on a personal project, the effect Light Box and if possible would like to hear the dear(a)s colleague. Whether pointing out errors or changes for didactic purposes, constructive criticism will always be welcome!

Code

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #e7e7e7;
    background-color: #f3f3f3;
}

li {
    float: left;
}

li a {
    display: block;
    color: #666;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover:not(.ativo) {
    background-color: #ddd;
}

li a.ativo {
    color: white;
    background-color: #4CAF50;
}

.escurecer{
    display: none;
    position: absolute;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 100%;
    background: black;
    z-index:1001;
    opacity:.80;
}

#sobre {
    display: none;
    position: absolute;
    top: 30%;
    left: 37%;
    width: 25%;
    height: 37%;
    padding: 16px;
    border: 3px solid gray;
    background-color: white;
    z-index:1002;
    overflow: auto;
    text-align: center;
}

#contato {
    display: none;
    position: absolute;
    top: 30%;
    left: 37%;
    width: 25%;
    height: 37%;
    padding: 16px;
    border: 3px solid gray;
    background-color: white;
    z-index:1002;
    overflow: auto;
	  text-align: center;
}
<ul class="topnav">
  <li><a class="ativo" href="#inicio">Inicio</a></li>
  <li><a href="#contato" onclick="document.getElementById('contato').style.display='block';document.getElementById('escurecer').style.display='block'">Contato</a></li>
  <li><a href="#sobre" onclick="document.getElementById('sobre').style.display='block';document.getElementById('escurecer').style.display='block'">Sobre</a></li>
</ul>

<div id="sobre" class="box"><h4>Sobre nós</h4><hr><br>

  <p>Lorem ipsum...</p>

</div>

<div id="contato" class="box"><h4>Fale conosco</h4><hr><br>

	<p>Lorem ipsum...</p>

</div>

        <div id="escurecer" class="escurecer" onclick="document.getElementById('escurecer').style.display='none';document.getElementById('sobre').style.display='none';document.getElementById('contato').style.display='none';"></div> 

Print

Print Screen - LightBox Sobre

Print Screen - LightBox Contato

Problem - I think it could improve the functional mode of the stretch:

<div id="escurecer" class="escurecer" onclick="document.getElementById('escurecer').style.display='none';document.getElementById('sobre').style.display='none';document.getElementById('contato').style.display='none';"></div> 

document.getElementById('sobre').style.display='none';     

document.getElementById('contato').style.display='none';"

This is for cleaning/hiding divs floating.

I was wondering if someone would propose something to me.. Let’s say more beautiful about this repetition of lines.

Reminder - All the source code presented here, works round. I only came to reap improvements or alternatives in the given section. In which I find this very amateur.

1 answer

1


There are several ways to do this, a basic example to make your code cleaner is

let classe = document.getElementsByClassName('link')
let escurecer = document.getElementById('escurecer')

escurecer.onclick = function(){
   escurecer.style.display = 'none'
   document.getElementById('modal').style.display = 'none'
}

for(let i = 0; i < classe.length; i++){
  classe[i].onclick = function(){
    document.getElementById('modal').style.display = 'block'
    escurecer.style.display = 'block'
    
    let attr = classe[i].getAttribute("href")
    // Coloco o conteudo
    
    if(attr == '#contato'){
      document.getElementById('modal').innerHTML = '<h1>Contato</h1>'
    }else{
        
      document.getElementById('modal').innerHTML = '<h1>Sobre</h1>'
    }
  }
}
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #e7e7e7;
    background-color: #f3f3f3;
}

li {
    float: left;
}

li a {
    display: block;
    color: #666;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover:not(.ativo) {
    background-color: #ddd;
}

li a.ativo {
    color: white;
    background-color: #4CAF50;
}

.escurecer{
    display: none;
    position: absolute;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 100%;
    background: black;
    z-index:1001;
    opacity:.80;
}

#modal {
    display: none;
    position: absolute;
    top: 30%;
    left: 37%;
    width: 25%;
    height: 37%;
    padding: 16px;
    border: 3px solid gray;
    background-color: white;
    z-index:1002;
    overflow: auto;
    text-align: center;
}

#contato {
    display: none;
    position: absolute;
    top: 30%;
    left: 37%;
    width: 25%;
    height: 37%;
    padding: 16px;
    border: 3px solid gray;
    background-color: white;
    z-index:1002;
    overflow: auto;
	  text-align: center;
}
<ul class="topnav">
  <li><a class="link ativo" href="#inicio">Inicio</a></li>
  <li><a class="link" href="#contato">Contato</a></li>
  <li><a class="link" href="#sobre">Sobre</a></li>
</ul>

<div id="modal" class="box"></div>
<div id="escurecer" class="escurecer"></div>

Remembering that it’s just an example of doing something Generatic, you can improve the code to get even better.

  • I made an edit to close the modal by clicking on div darken

  • I understand what you meant by - "There are several ways to do this, a basic example to make your code cleaner is... evaluate an arithmetic expression using the function. let". Well, I interpreted your code that way. It was cool!

  • I came across an obstacle, which comes down to the code of the script. I mean, I could not use it among the tags <head></head>. But yes, among the tags </body> and </html>. Thus the HTML document, saves time for the load of the elements themselves, be complete. And the console do not accuse how null(This means that the predestined elements do not yet exist). It also serves for the "script" being in external file, thus - </body><script src="script/script.js"></script></html>. I leave it at that, so that everyone knows how to solve it. Without further ado, I thank you.

  • @Diegohenrique the nominee is between the tags <body></body> same, and if possible in a file externo.

  • 1

    @Diegohenrique I erred rs sorry, the correct is to use at the end before the closure of <body> all the JS must be at the end of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.