0

я только начинаю учить jquery , и не могу понять как остановить функцию goBack() после первого использования. Мне кажется она продолжает работать из-за чего не выполняется первая анимация при повторном клике после выполнения всех анимаций. Подскажите пожалуйста , как можно решить данную ошибку ?

Js

$(function () {

    let arr = [142, 302, 462, 622, 782]

    $('.box').each(function (index) {
        $(this).on('click', function () {
            $(this).addClass('big')
            $('.modal-container').css({
                backgroundColor: 'rgba(0, 0, 0, 0.483)',
                zIndex: 3
            });

            $(this).stop().animate({
                top: '120%',
                left: '30%',
                width: '300px',
                height: '300px'
            }, 1000)
            setTimeout(function () {
                goBack(index);
            }, 1000);
        })

    })

    function goBack(index) {
        $('.big').queue(function () {
            $(this).on('click', function () {
                setTimeout(function (elem) {
                    $('.big').removeClass('big')
                    $('.modal-container').css({
                        backgroundColor: '#fff',
                        zIndex: -3
                    });

                }, 1000);


                $(this).stop().animate({
                    top: '10px',
                    left: arr[index] - 132,
                    width: '150px',
                    height: '150px'
                }, 1000, )

            })
            $(this).dequeue();

        })
    }

})

JS-FIDDLE

https://jsfiddle.net/fg5r34xh/

1 ответ 1

0

Проблема была в том, что Вы на один элемент вешаете 2 обработчика click и они срабатывают одновременно (после первого показа)

$(function () {
  let arr = [142, 302, 462, 622, 782];

  $(".box").each(function (index, item) {
    $(this).on("click", function () {
      if (!$(this).hasClass("big")) {
        $(this).addClass("big");
        $(".modal-container").css({
          backgroundColor: "rgba(0, 0, 0, 0.483)",
          zIndex: 3,
        });

        $(this).stop().animate(
          {
            top: "120%",
            left: "30%",
            width: "300px",
            height: "300px",
          },
          1000
        );
      } else {
        setTimeout(function (elem) {
          $(".big").removeClass("big");
          $(".modal-container").css({
            backgroundColor: "#fff",
            zIndex: -3,
          });
        }, 1000);

        $(this)
          .stop()
          .animate(
            {
              top: "10px",
              left: arr[index] - 132,
              width: "150px",
              height: "150px",
            },
            1000
          );
      }
    });
  });
});
1
  • большое спасибо !!!!
    – daydian
    11 мая 2021 в 11:42

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.