You can do it through transition , it will be like this:
const openMenu = document.querySelector('.open-button'); openMenu.addEventListener('click', (e) => { e.preventDefault(); openMenu.nextElementSibling.classList.toggle('open-content--show') });
.open-content { max-height: 0; overflow: hidden; transition: 1s ease max-height } .open-content--show { max-height: 300px; }
<div> <a class="open-button" href="" title="" rel="nofollow">Анкор ссылки</a> <div id="opencontent" class="open-content"> <p>Ваш открывающийся материал</p> </div> </div>
For the best, try to do everything as much as possible through CSS , that is, either without Javascript interaction at all, or with minimal. Your example can be done without Javascript at all, and on pure CSS through the input type checkbox . But here I showed, based on your code.
If you need the old standard ES5 , then Javascript code will be like this
'use strict'; var openMenu = document.querySelector('.open-button'); openMenu.addEventListener('click', function (e) { e.preventDefault(); openMenu.nextElementSibling.classList.toggle('open-content--show'); });
UPDATE: Option on pure CSS
label { cursor: pointer; } label ~ .appear { max-height: 0; overflow: hidden; transition: 1s ease max-height; } input { display: none; } input:checked ~ .appear { max-height: 300px; }
<label for="the-checkbox">Раскрыть</label> <input type="checkbox" id="the-checkbox" /> <div class="appear">Некоторый нужный текст</div>
UPDATE 2: If you do not need animation, but need a pure html, then in general like this. It does not suit you. But let it be for others.
<details> <summary>Раскрыть</summary> Некоторый текст </details>
transition, but there is a bigНО,display: none"transition-нуnot subject to - Air