如何停止 jQuery SlideToggle 溜溜球?
我是 jQuery 的新手,但这似乎应该工作得很好:
$(document).ready(
function()
{
$("#btn_toggle_transcript").click(
function()
{
$("#transcript_container").slideToggle(1000);
}
);
}
);
...它确实工作,但不正确。 ...至少,不像预期的那样...
我是否正确假设上述代码将向 ID 为“btn_toggle_transcript”的元素添加单击处理程序?那么,这个点击处理程序将通过打开目标 div(...id 为“transcript_container”)(如果它已关闭)或关闭它(如果它已打开)来响应点击?
当前发生的情况是这样的:单击按钮确实会打开 div 幻灯片。但是,当它完全打开时,它会立即(无需用户输入)再次滑动关闭。
我看到一些论坛帖子表明可能的原因是处理程序被添加了两次。但是,如果我注释掉上面的代码,则单击按钮时不会执行任何操作。所以,处理程序不会添加到其他任何地方...
任何人都可以告诉我为什么我的 div 会自行关闭吗?
按钮的 HTML:
<div id="tools_button_container">
<div class="tools_button" id="btn_toggle_transcript"></div>
</div>
滑动 div 的 HTML(内容动态设置):
<div id="transcript_container">
<div id="transcript_contents"></div>
</div>
和 CSS:
#transcript_container{
z-index:4;
position:absolute;
width:290px;
max-height:455px;
top:51px;
right:-2px;
background-color:#90B2D8;
padding:20px 10px 10px 10px;
border-bottom: 1px solid #000;
border-left: 1px solid #000;
border-right: 1px solid #000;
display:none;
}
#transcript_contents{
max-height:375px;
overflow:auto;
margin:20px 0px 10px 0px;
background-color:#ffffff;
padding:0px 5px 0px 5px;
}
#btn_toggle_transcript {
background: url('images/transcript.png') no-repeat 0 0;
display:block;
}
#circle_btn_toggle_transcript:hover {
background-position: 0px -50px;
}
==========
编辑:
我做了三件事似乎有帮助。我不知道他们为什么有效,但他们确实有效。
1) Drupal 6.x 使用旧版本的 jQuery (1.2.2)。有一个模块要更新到 1.3.2。我用 1.7.1 文件替换了 1.3.2 更新文件,现在我的 DOM 报告使用 jQuery v1.7.1。
2)& 3)尽管事实上我只绑定到单击事件一次(通过删除绑定并且在按钮单击时没有调用任何操作来验证),但我决定尝试添加一行来删除我的“幻像”绑定,然后再添加实际的绑定绑定。根据我的阅读,我决定使用现在推荐的 on() 和 off() 方法来添加/删除绑定。我的 read() 函数现在看起来像这样:
$(document).ready(
function(){
$("body").off("click", "#btn_toggle_transcript");
$("body").on("click", "#btn_toggle_transcript",
function(){
$("#transcript_container").slideToggle(1000);
}
);
}
);
现在,我的滑动 div 打开并保持打开状态,直到我再次将其关闭。耶!
非常感谢所有回答/评论的人。您的建议和指导帮助我找到了解决方案。
干杯!
更新: 正如我在其中一条评论中提到的,这是在 Drupal 内部运行的。正如还提到的,如果我注释掉单个绑定,则不会调用任何操作。我的推理是,由于删除一个绑定不再调用该操作,那么它不可能被绑定两次......
愚蠢!
虽然我是正确的,只有一个绑定代码块,但我完全忘记了我的模块已经将我的 js 文件添加到页面中。因为我还需要在非 Drupal 环境中部署内容,所以我还手动将 js 文件添加到我的页面中。 ...两个包含物意味着两个绑定。 ……这几天我就再也回不来了!
I am new to jQuery but this seems like it should work just fine:
$(document).ready(
function()
{
$("#btn_toggle_transcript").click(
function()
{
$("#transcript_container").slideToggle(1000);
}
);
}
);
...and it does work, but not correctly. ...at least, not as expected...
Am I correct in assuming that the above code would add a click handler to the element with an ID of "btn_toggle_transcript"? This click handler, then, would respond to the click by either opening the targetted div (...with the id of "transcript_container") if it is closed, or closing it if it is open?
What is currently happening is this: clicking the button does, indeed, make the div slide open. BUT, when it is fully open, it IMMEDIATELY - and with no user input - slides closed again.
I have seen a few forum posts suggesting the possible cause is the handler getting added twice. However, if I comment out the above code, no action is taken when the button is clicked. So, the handler is not getting added anywhere else...
Can ANYONE please tell me why my div is yo-yoing closed by itself?
The HTML for the button:
<div id="tools_button_container">
<div class="tools_button" id="btn_toggle_transcript"></div>
</div>
The HTML for the sliding div (content is set dynamically):
<div id="transcript_container">
<div id="transcript_contents"></div>
</div>
And the CSS:
#transcript_container{
z-index:4;
position:absolute;
width:290px;
max-height:455px;
top:51px;
right:-2px;
background-color:#90B2D8;
padding:20px 10px 10px 10px;
border-bottom: 1px solid #000;
border-left: 1px solid #000;
border-right: 1px solid #000;
display:none;
}
#transcript_contents{
max-height:375px;
overflow:auto;
margin:20px 0px 10px 0px;
background-color:#ffffff;
padding:0px 5px 0px 5px;
}
#btn_toggle_transcript {
background: url('images/transcript.png') no-repeat 0 0;
display:block;
}
#circle_btn_toggle_transcript:hover {
background-position: 0px -50px;
}
==========
EDIT:
I did three things that seem to have helped. Why they worked, I do not know, but they do.
1) Drupal 6.x uses an old version of jQuery (1.2.2). There is a module to update to 1.3.2. I replaced the 1.3.2 update files with the 1.7.1 files and now my DOM reports using v1.7.1 of jQuery.
2) & 3) Despite the fact that I only bind to the click event ONCE (verified by removing the binding and having no action invoked on a button click), I decided to try adding a line to remove my "phantom" binding before I add my actual binding. Based on my reading, I have decided to use the now recommended on() and off() methods to add/remove the bindings. My ready() function now looks like this:
$(document).ready(
function(){
$("body").off("click", "#btn_toggle_transcript");
$("body").on("click", "#btn_toggle_transcript",
function(){
$("#transcript_container").slideToggle(1000);
}
);
}
);
Now, my sliding div opens and stays open until I toggle it closed again. Yay!
A big thank you to all who answered/commented. Your suggestions and guidance helped me get to a solution.
Cheers!
UPDATE:
As I mentioned in one of the comments, this is running inside Drupal. As also mentioned, if I commented out the single binding, no action was invoked. My reasoning was that since the removal of ONE binding ceased to invoke the action, then there was no way it was being bound twice...
DUMB!
While I was correct that there was only one block of binding code, I completely forgot that my module was already adding my js file to the page. Because I need to deploy my content in non-drupal environments as well, I was also manually adding the js file to my page. ...two inclusions meant two bindings. ...that's a few days I'll never get back!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它的情况是点击被绑定了两次。看看你是否有这样做过的可能性。例如。在 js 文件中以及 html 文档中的脚本标签内。
此外,如果容器是动态的,您应该使用 live 而不是 click。
你可以试试这个。
Its case where click is binded two times. See for a possibility where you have done that. eg. in js file and as well as inside script tag in html document.
Also if container is dynamic, you should use live instead of click.
You can try this.
在我的答案末尾有一个新的解决方案。
我已经多次遇到同样的问题,并且肯定只调用了一次该函数。
由于某种原因,
tween.end
变量的每一个小步骤都会诉诸于 0。我的解决方法如下:有点混乱,但这就是我能想到的,因为我不能找到对该函数的另一个调用。
编辑
一个老问题 - 一个新的解决方案。
只需在 SlideToggle 之前添加 stop() 即可修复反复出现的溜溜球效果。
$('#element').stop().slideToggle();
There is a new solution near the end of my answer.
I've had this same problem numerous times and definitely only had one call to the function.
For some reason every little step of the way the
tween.end
variable would resort to 0. My workaround to this is as follows:Bit messy, but that's all I could come up with as I couldn't find another call to the function.
edit
An old question - with a new solution.
Simply adding stop() before your slideToggle fixes the recurring yo-yo effect.
$('#element').stop().slideToggle();
您等待事件结束来添加班级。
You wait for the end of the event to add the class.