Как сделать анимацию аккордиону через JS

Рейтинг: 0Ответов: 1Опубликовано: 26.04.2023

Помогите пожалуйста мне нужна анимация для аккордиона. Сам аккордион взял с интернета, а JS я пока что не знаю.

Вот сам аккордион, я под себя уже переделал, но мне нужна анимация через JS, чтобы доп. контент плавно выезжал, но я знаю как это сделать.

const questions = document.querySelectorAll(".accordion");

questions.forEach((question) => {
  const btn = question.querySelector(".accordion__btn");

  btn.addEventListener("click", () => {
    console.log(question);
    questions.forEach((item) => {
      if (item !== question) {
        item.classList.remove("active");
        //console.log(item);
      }
    });

    question.classList.toggle("active");

    //console.log(question);

  });
});
.accordion {
  width: 590px;
}

.accordion__title {
  margin-top: 26px;
  display: flex;
  justify-content: space-between;
  font-family: Montserrat;
  font-weight: 600;
  font-size: 16px;
  line-height: 140%;
  color: #1B1B22;
}

.accordion__title-top {
  margin-top: 0;
}

.accordion__title p {
  margin-top: 4px;
  width: 488px;
}

.accordion__btn {
  position: relative;
  width: 30px;
  height: 30px;
  background: #F4F4F4;
  border: none;
  border-radius: 50%;
  transition: 0.5s;
  cursor: pointer;
}

.accordion__btn span:first-child {
  display: block;
  width: 11px;
  height: 1px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #000;
  transition: 0.5s;
}

.accordion__btn span:last-child {
  display: block;
  width: 1px;
  height: 11px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #000;
  transition: 0.5s;
}

.accordion__btn:hover {
  background: #1111FF;
}

.accordion__btn:hover span:first-child {
  background: #fff;
}

.accordion__btn:hover span:last-child {
  background: #fff;
}

.accordion__text {
  display: none;
  font-family: Montserrat;
  font-weight: 400;
  font-size: 16px;
  line-height: 140%;
  color: #1B1B22;
  transition: 0.5s;
}

.accordion__text a {
  text-decoration: underline;
}

.accordion__text p {
  width: 560px;
}

.accordion__line {
  margin-top: 26px;
  width: 100%;
  height: 1px;
  background: #C8DAEA;
}

.line_2 {
  margin-top: 7px;
}

.active .accordion__btn {
  transform: rotate(45deg);
}

.active .accordion__text {
  display: block;
  margin-top: 12px;
}

.active .line_2 {
  margin-top: 26px;
}
<div class="accordion">
  <div class="accordion__title accordion__title-top">
    <p>Как стать участником программы?</p>
    <button type="button" class="accordion__btn">
            <span></span>
            <span></span>
        </button>
  </div>
  <div class="accordion__text">
    <p>Для участия в проекте необходимо пройти по ссылке и присоединиться к <a href="#">Telegram-боту.</a></p>
  </div>
  <div class="accordion__line"></div>
</div>

Буду благодарен за помощь!

Ответы

▲ 1

Пожалуйста

const questions = document.querySelectorAll(".accordion");

questions.forEach((question) => {
    const btn = question.querySelector(".accordion__btn");

    btn.addEventListener("click", () => {
        questions.forEach((item) => {
            if (item !== question) {
                item.classList.remove("active");
            }
        });

        question.classList.toggle("active");


    });
});
.accordion {
    position: relative;
    width: 590px;
    overflow: hidden;
    max-height: 60px;
    transition: all 0.2s;
}

.accordion::after {
    position: absolute;
    content: "";
    left: 0;
    bottom: 10px;
    width: 100%;
    height: 1px;
    background-color: #C8DAEA
}

.accordion__title {
    display: flex;
    height: 50px;
    justify-content: space-between;
    align-items: center;
    font-family: Montserrat;
    font-weight: 600;
    font-size: 16px;
    line-height: 140%;
    color: #1B1B22;
}



.accordion__btn {
    position: relative;
    width: 30px;
    height: 30px;
    background: #F4F4F4;
    border: none;
    border-radius: 50%;
    transition: 0.3s;
    cursor: pointer;
}

.accordion__btn span:first-child {
    display: block;
    width: 11px;
    height: 1px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #000;
    transition: 0.3s;
}

.accordion__btn span:last-child {
    display: block;
    width: 1px;
    height: 11px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #000;
    transition: 0.3s;
}

.accordion__btn:hover {
    background: #1111FF;
}

.accordion__btn:hover span:first-child {
    background: #fff;
}

.accordion__btn:hover span:last-child {
    background: #fff;
}

.accordion__text {
    font-family: Montserrat;
    font-weight: 400;
    font-size: 16px;
    line-height: 140%;
    padding-bottom: 20px;
    color: #1B1B22;
}

.accordion__text a {
    text-decoration: underline;
}

.accordion__text p {
    width: 560px;
}

.line_2 {
    margin-top: 7px;
}

.accordion.active {
    max-height: 3000px;
    transition: 2s !important;
}

.active .accordion__btn {
    transform: rotate(45deg);
}

.active .accordion__text {
    margin-top: 12px;
}
<div class="accordion">
        <div class="accordion__title accordion__title-top">
            <p>Как стать участником программы?</p>
            <button type="button" class="accordion__btn">
                <span></span>
                <span></span>
            </button>
        </div>
        <div class="accordion__text">
            <p>Для участия в проекте необходимо пройти по ссылке и присоединиться к <a href="#">Telegram-боту.</a>
            </p>
        </div>
    </div>
    <div class="accordion">
        <div class="accordion__title accordion__title-top">
            <p>Как стать участником программы?</p>
            <button type="button" class="accordion__btn">
                <span></span>
                <span></span>
            </button>
        </div>
        <div class="accordion__text">
            <p>Для участия в проекте необходимо пройти по ссылке и присоединиться к <a href="#">Telegram-боту.</a>
            </p>
        </div>
    </div>
    <div class="accordion">
        <div class="accordion__title accordion__title-top">
            <p>Как стать участником программы?</p>
            <button type="button" class="accordion__btn">
                <span></span>
                <span></span>
            </button>
        </div>
        <div class="accordion__text">
            <p>Для участия в проекте необходимо пройти по ссылке и присоединиться к <a href="#">Telegram-боту.</a>
            </p>
        </div>
    </div>