@ligisayan

Почему не выполняются условия скрипта $(window).resize?

Привет! Есть на сайте товарные секции, в которых хочу выполнить определенные действия при определенных размерах экрана. Не могу понять почему не выполняется условия скрипта $(window).width()?
Вот так работает:
$(window).resize(function() {
  console.log("resize");
  $('.main-product .product-frame').each(function() {
    var $this = $(this);
    var height = $this.find('.product-section h3').height();
    if (height > 21) {
      if (!$this.find('.variations_form').length) {
        console.log("simple");
        if ($(window).width() >= 992 && $(window).width() < 1200) {
          console.log("simple price");
        }
      }
    }
  });
});

<div class="main-product">
  <div class="product-frame">
    <div class="product-section">
      <h3>Товар 1</h3>
    </div>
  </div>
  <div class="product-frame">
    <div class="product-section">
      <h3>Товар 2</h3>
    </div>
  </div>
</div>


А когда добавляю проверку еще одного медиазапроса, то уже нет..
$(window).resize(function() {
  console.log("resize");
  $('.main-product .product-frame').each(function() {
    var $this = $(this);
    var height = $this.find('.product-section h3').height();
    if (height > 21) {
      if (!$this.find('.variations_form').length) {
        console.log("simple");
        if ($(window).width() >= 992 && $(window).width() < 1200) {
          console.log("simple price");
        }
        if ($(window).width() >= 768 && $(window).width() <= 991) {
          console.log("simple price");
        }
      }
    }
  });
});
  • Вопрос задан
  • 722 просмотра
Пригласить эксперта
Ответы на вопрос 2
@dmitryKovalskiy
программист средней руки
Вот вы height сохранили во временную переменную, а width постоянно пересчитываете, в чем логика? А по вопросу - в чем проблема? надпись "simple price" не появляется ни разу или что?
P.S. у вас точка 991 выпала из проверок.
Ответ написан
@Eugeny1987
Работаю с HostCMS
может все-таки так?
$(window).resize(function() {
  console.log("resize");
  $('.main-product .product-frame').each(function() {
    var $this = $(this);
    var height = $this.find('.product-section h3').height();
    if (height > 21) {
      if (!$this.find('.variations_form').length) {
		 var width = $(window).width();
        console.log("simple");
        if (width >= 992 && width < 1200) {
          console.log("simple price");
        }
        if (width >= 768 && width <= 991) {
          console.log("simple price");
        }
      }
    }
  });
});
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы