https://jsfiddle.net/d8gz67nt/1/
I will leave an example to serve as a basis for you to have a notion. At the end I left some links.
First we must know the structure of HTML, we will use a div
with class expand
so that we can create more than one expandable content.
<body>
<div class="expand"></div>
</body>
Now we need to adjust the styles.
.expand {
position: relative;
width: 400px;
min-height: 200px;
height: 200px;
background: #ccc;
overflow: hidden;
padding-bottom: 18px;
}
.expander {
position: absolute;
background: #aaa;
bottom: 0;
width: 100%;
height: 18px;
text-align: center;
cursor: row-resize;
}
.expand
are the blocks that will have concealable content.
.expander
is the dynamically generated element for clicking and showing/hiding content.
height
is the size of the element when hiding content.
overflow: hidden
makes the element have a scroll bar, but hides it. Without it all content would be visible.
Now we need to create the events in javascript
to allow content to be displayed.
function createExpander (container) {
var elem;
elem = document.createElement('div');
elem.className = 'expander show';
elem.innerHTML = 'Mostrar';
elem.addEventListener('click', function () {
// se o conteúdo estiver ocultado, altera o height
// para que se adapte ao tamanho do conteúdo.
// Se não, apenas retorna ao tamanho original (200px).
if (elem.className === 'expander show') {
elem.innerHTML = 'Ocultar';
elem.className = 'expander hide';
container.style.height = 'auto';
} else {
elem.innerHTML = 'Mostrar';
elem.className = 'expander show';
container.style.height = '200px';
}
});
container.appendChild(elem);
}
document.addEventListener('DOMContentLoaded', function () {
// aplicamos em todos os elementos .expand na página.
document.querySelectorAll('.expand').forEach(function (elem) {
createExpander(elem);
});
});
The function createExpander
generates the clickable element that shows/hides the content.
Here are some links to better understand some parts.
The CSS Overflow attribute
Overflow - CSS | MDN
Manipulating css with javascript
Basically the solution is to put the div as overflow:Hidden, but to know how to calculate the 50% depends on you put the code structure. A less noble solution is to use a pseudoelement with absolute position and 50% height, covering the content, but then the external div will occupy the same space as the 100% height. Click on [Edit] and put the HTML structure for the respondent can better evaluate the most appropriate solution.
– Bacco
As @Bacco said it is necessary to have a base to help. Put what you already tried and the code as it was for a more detailed help. Do you really want javascript? It can be jquery?
– Tiago Gomes
I could understand + or -, but I haven’t done anything yet I just wanted to know how you do it because I’m going to need
– Márcio Sebastião