简体   繁体   English

jQuery 显示/隐藏后要执行的回调函数?

[英]Callback function to be executed after jQuery show / hide?

In iOS, the following code has a noticeable flicker between the hide() and the scrollBy():在 iOS 中,以下代码在 hide() 和 scrollBy() 之间有明显的闪烁:

element.hide();
window.scrollBy(0, -elementHeight);

This is because toggling between display: none and display: block on iOS is a heavy task, as if the elements are being added to and removed from the DOM.这是因为在 iOS 上的 display: none 和 display: block 之间切换是一项繁重的任务,就好像元素被添加到 DOM 和从 DOM 中删除一样。

I need a way to perform window.scrollBy() as a callback, once the hide() has successfully completed and the DOM has updated.我需要一种方法来执行 window.scrollBy() 作为回调,一旦 hide() 成功完成并且 DOM 已更新。 Is there a way to do this in jQuery?有没有办法在 jQuery 中做到这一点?

Either pass a duration and a callback, or just pass a callback option, like this:要么传递持续时间和回调,要么只传递回调选项,如下所示:

element.hide(0, some_function);

// or 

element.hide({done: some_function});

By default, the second option takes 400 ms.默认情况下,第二个选项需要 400 毫秒。 To do it immediately, use one of these:要立即执行此操作,请使用以下方法之一:

element.hide(0, some_function);

// or 

element.hide({duration: 0, done: some_function});

Here's a jsFiddle demo .这是一个 jsFiddle 演示

See the jQuery documentation for more details.有关更多详细信息,请参阅 jQuery 文档

From the jQuery api:来自 jQuery api:

.hide(options) .hide(选项)

complete Type: Function() A function to call once the animation is complete. complete 类型:Function() 动画完成后调用的函数。

Try this:尝试这个:

element.hide({complete: function(){ window.scrollBy(0, -elementHeight); }); element.hide({完成: function(){ window.scrollBy(0, -elementHeight); });

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

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