简体   繁体   English

在 jQuery 中多次单击显示/隐藏 function

[英]Clicking multiple times on a show/hide function in jQuery

I made this something like this:我做了这样的事情:
Codepen or code down. Codepen或code down。

 $(document).ready(function () { var btn1Status = 0; $("button").click(function () { if (btn1Status === 0) { $(".info").slideDown(); $(".info").show("slow"); $("button").text("less"); btn1Status = 1; } else { $(".info").slideUp(); $(".nfo").hide("slow"); btn1Status = 0; $("button").text("More"); } }); });
 button { background-color: black; color: white; width: 50px; height: 30px; } button:focus { outline: none; } ul { list-style-type: none; margin: 10px; padding: 0; }.info { background-color: black; color: white; font-family: arial; width: 400px; display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div><img src="https://img.favpng.com/22/6/0/portable-network-graphics-movie-camera-film-clip-art-computer-icons-png-favpng-qfZ7hHPN2CCxiK8sYfjS2Sti9.jpg" width="400"><button>More</button></div> <div class="info"> <ul> <li>Name: movie1</li> <li>Time: 6:00PM</li> <li>Info 3: blablabla</li> <li>Info 4: blablabla</li> </ul> </div>

when someone clicks the button, the info div should appear.当有人点击按钮时,信息 div 应该会出现。 the problem is, if I click the button multiple times then lift it, it will keep showing and disappearing.问题是,如果我多次单击按钮然后将其抬起,它将继续显示和消失。 How do I fix this problem.我该如何解决这个问题。 Thanks for helping.感谢您的帮助。

If you want the ability to let the users click multiple times but not build a queue of animations you can use the stop function from jQuery to stop/clear the queue of any currently running animations.如果您想让用户点击多次但不构建动画队列,您可以使用 jQuery 中的stop function 来停止/清除任何当前正在运行的动画的队列。

if (btn1Status === 0) {
  $(".info").stop(true, true).slideDown();
  $(".info").show("slow");
  $("button").text("less");
  btn1Status = 1;
} else {
  $(".info").stop(true, true).slideUp();
  $(".info").hide("slow");
  btn1Status = 0;
  $("button").text("More");
}

This will cancel the queue of animations if the button is clicked multiple times.如果多次单击按钮,这将取消动画队列。 The two arguments of .stop() are: ( [clearQueue ] [, jumpToEnd ] ) . .stop()的两个 arguments 是: ( [clearQueue ] [, jumpToEnd ] ) clearQueue is needed to clear the current animation queue, jumpToEnd will either cancel the currently running animation before jumping to the next one, or begin running the next slide animation from the current animation frame. clearQueue is needed to clear the current animation queue, jumpToEnd will either cancel the currently running animation before jumping to the next one, or begin running the next slide animation from the current animation frame.

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

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