简体   繁体   English

响应式Jquery切换命令

[英]Responsive Jquery toggle command

I am having an issue with my code. 我的代码有问题。 Whenever, I load up the page in the browser or in a mobile device, and I try to toggle it suddenly toggles multiple times when I just clicked on it once. 每当我将页面加载到浏览器或移动设备中时,只要单击一次,我就会尝试将其切换多次。 I am trying to use this syntax code to make it responsive. 我正在尝试使用此语法代码使其具有响应能力。

My CodePen 我的密码笔

$(window).resize(function() {
  $(".textcontent").hide();

  if ($(window).width() <= 480) {

    jQuery(function($) {
      $(document).ready(function() {
        $(".textcontent").hide();
        $(".titleheader").click(function() {
          $(this).toggleClass("active").next().slideToggle("fast");
          return false;
        });
      });
    }); 

  }
});

The text toggles more than once because you are binding the click handler each time the resize event fires. 文本会多次切换,因为每次resize事件触发时,您都将绑定单击处理程序。 Each bind attaches another handler so, depending on how many times the resize event fires, you might end up with many click handlers firing at once. 每个绑定都附加一个处理程序,因此,根据重新调整大小事件触发的次数,您可能最终会一次触发许多单击处理程序。

I suggest that you bind or unbind your click handler depending on the screen width, like so: 我建议您根据屏幕宽度绑定或取消绑定点击处理程序,如下所示:

$(function() {

  // function to toggle a text section

  function toggleText(elm) {
    $(elm).toggleClass("active").next().slideToggle("fast");
  }

  // resize event handler

  $(window).on('resize', function() {

    if ($(window).width() <= 480) {

      // if window <= 480, unbind and rebind click handlers, and hide all text content

      $(".titleheader").off('click').on('click', function() {
        toggleText(this);
      });

      $(".textcontent").hide();

    } else {

      // if the window > 480, unbind click handlers and hide all text

      $(".titleheader").off('click');
      $(".textcontent").show();

    }

  }).trigger('resize'); // initialize - trigger the resize once upon load

});

WORKING EXAMPLE 工作实例

You might also want to throttle or "debounce" your resize handler so it won't fire continuously in IE, Safari, and Chrome. 您可能还需要节流“反跳”的大小调整处理程序,以便它不会继续火在IE,Safari和Chrome浏览。


EDIT: 编辑:

An alternate method is to set a flag to indicate whether the layout is "small" or "large". 另一种方法是设置标志以指示布局是“小”还是“大”。 Then, only change the layout if the flag does not indicate the desired layout: 然后,仅当标志未指示所需的布局时才更改布局:

$(function() {

  // layout flag (defaults to "not small")
  var small = false;

  // function to toggle a text section
  function toggleText(elm) {
    $(elm).toggleClass("active").next().slideToggle("fast");
  }

  // resize event handler
  $(window).on('resize', function() {


    if ($(window).width() <= 480) {

      // if window <= 480 and the layout is "not small", bind click handlers and hide all text content

      if (!small) {

        console.log('made small');
        $(".titleheader").on('click', function() {
          toggleText(this);
        });
        $(".textcontent").hide();

        // set the layout flag to "small"
        small = true;

      }

    } else {

      // if the window > 480 and the layout is "small", unbind click handlers and hide all text

      if (small) {

        console.log('made large');
        $(".titleheader").off('click');
        $(".textcontent").show();

        // set the layout flag to "not small"
        small = false;
      }

    }

  }).trigger('resize'); // initialize - trigger the resize once upon load

});

WORKING EXAMPLE 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM