简体   繁体   English

如何限制我可以使用 slideToggle 的次数?

[英]How do I set a limit on how many times I can use slideToggle?

If I rapidly click the div on an HTML website that calls the .slideToggle() method, it will continuously open and close as many times I click the button.如果我在HTML网站上快速点击调用.slideToggle()方法的div,它会随着我点击按钮的次数不断打开和关闭。 The issue comes that after I stop clicking, the <div> will still open and close itself and keep on doing so until it reaches the number of times I clicked it.问题来了,当我停止点击后, <div>仍然会自行打开和关闭,并一直这样做,直到达到我点击它的次数。 If you rapidly hit "Toggle" logo, this demo has the same issue.如果你快速点击“Toggle”标志,这个演示有同样的问题。 See this example: https://css-plus.com/examples/2010/03/slidetoggle/index看这个例子: https://css-plus.com/examples/2010/03/slidetoggle/index

$("#examplebutton1").click(function()
{
    $("#examplecontent1").slideToggle(500);
});

I originally tried inserting the .slideToggle() method, but that obviously didn't work.我最初尝试插入.slideToggle()方法,但这显然不起作用。 Is there a way I can make it so the method would work if the div if fully extended or fully closed?如果 div 完全扩展或完全关闭,有什么方法可以使该方法起作用?

You can use a setTimeout and a global variable to check if a transition is already going on:您可以使用setTimeout和全局变量来检查转换是否已经在进行:

let transitionIsHappening = false;
$("#examplebutton1").click(function() {
    if (transitionIsHappening) return;
    transitionIsHappening = true;
    $("#examplecontent1").slideToggle(500, () => transitionIsHappening = false);
});

The.slideToggle() method animates the height of the matched elements. .slideToggle() 方法为匹配元素的高度设置动画。 This causes lower parts of the page to slide up or down, appearing to reveal or conceal the items.这会导致页面的下部向上或向下滑动,以显示或隐藏项目。 If the element is initially displayed, it will be hidden;如果该元素最初显示,它将被隐藏; if hidden, it will be shown.如果隐藏,它将被显示。 The display property is saved and restored as needed.显示属性会根据需要保存和恢复。 If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.如果一个元素的显示值为内联,然后隐藏和显示,它将再次内联显示。 When the height reaches 0 after a hiding animation, the display style property is set to none to ensure that the element no longer affects the layout of the page.当隐藏animation后高度达到0时,将显示样式属性设置为none,保证该元素不再影响页面的布局。

Durations are given in milliseconds;持续时间以毫秒为单位; higher values indicate slower animations, not faster ones.较高的值表示较慢的动画,而不是较快的动画。 The strings 'fast' and 'slow' can be supplied to indicate durations of 200 and 600 milliseconds, respectively可以提供字符串 'fast' 和 'slow' 以分别指示 200 和 600 毫秒的持续时间

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

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