简体   繁体   English

jQuery动画元素并隐藏

[英]jQuery animate element and hide

I'm building Windows 8 app in JavaScript. 我正在用JavaScript构建Windows 8应用程序。 What I'm trying to do is to slide the html element out of the screen and then change its "display" property to "none": 我想做的是将html元素滑出屏幕,然后将其“ display”属性更改为“ none”:

var panelContainer = $('#panelContainer');
panelContainer.animate({ right: '-400px' }, 200, function () {
    panelContainer.hide();
});

But this code doesn't work correctly: it just immediately hides the element without animation. 但是这段代码无法正常工作:它只是立即隐藏了没有动画的元素。 I've also tried: 我也尝试过:

var panelContainer = $('#panelContainer');
panelContainer.animate({ right: '-400px' }, 200, function () {
    panelContainer.hide(200);
});

and it works, but it's a hack: I don't want to change the opacity when animating and I don't need to have additional timeout for hiding. 并且它可以工作,但这是一个hack:我不想在设置动画时更改不透明度,也不需要额外的超时来隐藏。 I've found that jQuery UI library has extended show and hide methods that do that, but I would like not to reference this library just for one call. 我发现jQuery UI库已经扩展了执行该操作的show和hide方法,但是我不想只为一个调用引用该库。 I'm aware that there is a WinJS.UI.Flyout that performs similar operation, but it's not applicable in my case. 我知道有一个WinJS.UI.Flyout执行类似的操作,但是不适用于我的情况。 Any ideas how this can be done? 任何想法如何做到这一点?

The problem was that jQuery does not put hide animation into its animation queue by default. 问题在于,jQuery默认不会将hide动画放入其动画队列中。 That's why my initial code was hiding the html element first and then animating it. 这就是为什么我的初始代码先隐藏html元素然后对其进行动画处理的原因。 The solution for that is to call hide with the parameter that explicitly specifies that hide call should be queued: 解决该问题的方法是使用明确指定应将hide呼叫排队的参数调用hide:

panelContainer.hide({queue: true});

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

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