3
Good morning! I rode several blocks with a div
that through Javascript, it interacts as if it were opening a drawer (block) with a click and closing it with two clicks through a function.
But when you open one block and the other at the same time, the two are open on the same page, taking the visual style out of the HTML/CSS structure. And when you open them all at the same time, there is a disorganized structure, because I defined that the 5 blocks (guides) occupy a width of 4% and the "drawer" that was going to be opened, would be 80% wide. This error happens because the maximum width of the page goes up to 100% and all windows open, exceeds the defined width, leaving all the blocks under each other.
function Exibir1() {
document.getElementById("div01").style.display = "block";
}
function Ocultar1(){
document.getElementById("div01").style.display = "none";
}
function Exibir2() {
document.getElementById("div02").style.display = "block";
}
function Ocultar2(){
document.getElementById("div02").style.display = "none";
}
function Exibir3() {
document.getElementById("div03").style.display = "block";
}
function Ocultar3(){
document.getElementById("div03").style.display = "none";
}
function Exibir4() {
document.getElementById("div04").style.display = "block";
}
function Ocultar4(){
document.getElementById("div04").style.display = "none";
}
function Exibir5() {
document.getElementById("div05").style.display = "block";
}
function Ocultar5(){
document.getElementById("div05").style.display = "none";
}
#blockpop{
background-color: lightgray;
width: 4%;
height: 300px;
float: left;
border: 1px solid;
padding-top: 9%;
padding-left: 1.9%;
}
h2{
width: 50%;
}
#textblock{
transform: rotate(-90deg);
width: 700%;
margin-left: -350%;
text-align: center;
font-size: 10px;
font-weight: bold;
}
#div01{background-color: red;}
#div02{background-color: gold;}
#div03{background-color: lightgreen;}
#div04{background-color: royalblue;}
#div05{background-color: pink;}
#div01,#div02,#div03,#div04,#div05{
width: 80%;
float: left;
display: none;
padding-left: 1%;
height: 300px;
overflow-x: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="blockpop" onclick="Exibir1()" ondblclick="Ocultar1()">
<div id="textblock">BLOCO 1</div>
</div>
<div id="div01">
<h2>BLOCO 1</h2>
<b>conteúdo</b><br>
</div>
<div id="blockpop" onclick="Exibir2()" ondblclick="Ocultar2()">
<div id="textblock">BLOCO 2</div>
</div>
<div id="div02">
<h2>BLOCO 2</h2>
<b>conteúdo</b>
</div>
<div id="blockpop" onclick="Exibir3()" ondblclick="Ocultar3()">
<div id="textblock">BLOCO 3</div>
</div>
<div id="div03">
<h2>BLOCO 3</h2>
<b>conteúdo</b>
</div>
<div id="blockpop" onclick="Exibir4()" ondblclick="Ocultar4()">
<div id="textblock">BLOCO 4</div>
</div>
<div id="div04">
<h2>BLOCO 4</h2>
<b>conteúdo</b>
</div>
<div id="blockpop" onclick="Exibir5()" ondblclick="Ocultar5()">
<div id="textblock">BLOCO 5</div>
</div>
<div id="div05">
<h2>BLOCO 5</h2>
<b>conteúdo</b>
</div>
In this way, what would be the most practical way to create a script that when opening a block, close all others that are open on the page? It is possible to create a function within the attribute
onclick
in a tag that recognizes only two clicks to open and close any block?
I don’t know if it’s just because of studies and you really want to know how it does with JS, but this component is possible to do only with CSS, if you are interested I can make a template.
– hugocsl
Yes, I would be grateful if you had a practical example here in the post, because I’ve seen this method done only with pre-defined functions in Javascript, already only by CSS, never heard of this form.
– pe.Math
Dude I made the model and is 100% functional, the problem is that it is not exactly as you want, because it does not close at double click, actually after opened it is open, but alternates closing one when opening another normally... See this gif, if you are interested I put as a response option... https://imgur.com/a/w9EBkAZ
– hugocsl
I managed to do it with jQuery opening and closing the way you want, only with a simple click to close and not with a double click to close. But the rest is 100%, open one and close the others, and if you click what is open it closes. One problem I noticed in your code is that you are repeating the ID on several elements, this is not cool, until pq a simple css class would be more suitable. If you want I can include in the answer the two codes https://imgur.com/a/3LsmG4z
– hugocsl
@hugocsl, you can put yes, it is even good for some users to see and be interested in the subject, because there are some websites that have various information that choose to use this form of content division. But, if possible, you can put as an answer here in the post. ;-)
– pe.Math
By the way, I used the
ondblclick
as if it were an improvisation, as I worked very little with these events in my course, so I tried to simulate a drawer with only two functions.– pe.Math