jQuery 动画完成后如何执行 .click() ?
我想在单击链接后向上滑动一个 div,然后向下滑动另一个 div,然后转到下一页。
问题:在动画完成之前加载新页面。
问题:我该怎么做?
我应该使用 PreventDefault 然后通过某种方式恢复它吗?
这是网址: http://www.jolandaschouten.nl/wordpress
这是代码,即现在仅针对第一个菜单项实现:
jQuery(document).ready(function($){
var allDivs = $("#introductie").add("#agenda").add("#werk").add("#onderwijs-organisatie").add("#publicaties").add("#links").add("#contact");
console.log(allDivs);
$("li.page-item-11 a").click(slideUp);
function slideUp(){
allDivs.slideUp(500, slideDown);
}
function slideDown(){
$("#introductie").slideDown(500);
}
});
ps:由于 SEO 和浏览器导航问题,我不想使用 AJAX。单击实际上应该导致转到下一页。
I want to slideUp one div after clicking a link, then slideDown another div, then go to the next page.
Problem: new page loads before the animations are finished.
Question: How can I do this?
Should I use preventDefault and then some way to resume it?
This is the URL: http://www.jolandaschouten.nl/wordpress
This is the code, which is now only implemented for the first menu item:
jQuery(document).ready(function($){
var allDivs = $("#introductie").add("#agenda").add("#werk").add("#onderwijs-organisatie").add("#publicaties").add("#links").add("#contact");
console.log(allDivs);
$("li.page-item-11 a").click(slideUp);
function slideUp(){
allDivs.slideUp(500, slideDown);
}
function slideDown(){
$("#introductie").slideDown(500);
}
});
ps: I don't want to use AJAX, because of SEO and browser navigation problems. The click should really result in going to the next page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
返回 false 来阻止默认操作(跟随链接),使用闭包来锁定被单击的项目,然后手动跟随它:(
第一个回调实际上不必是内联像这样的函数文字,但其他函数这样做是为了关闭 clickedAnchor 值。)
Return
false
to prevent the default action (following the link), use closures to latch onto the item that was clicked, and then manually follow it:(The first callback doesn't actually have to be an inline function literal like that, but the others do in order to close around the
clickedAnchor
value.)您可以做的一件事是将整个动画过程包装在 .animate() 中,然后使用回调直接进入新页面。 .animate() 下的回调在动画完成时发生。这应该会给你你正在寻找的行为。
one thing you could do is wrap your whole animation process in .animate() and then use the callback to direct to the new page. The callback under .animate() happens when the animation is complete. This should give you the behavior you are looking for.
检查内嵌评论。简化了初始选择。要加载新页面,请使用
slideDown
回调。Check the inline comments. Simplified the initial selection. To load the new page use the
slideDown
callback.非常感谢大家的帮助!
我花了一段时间才得到正确的代码(有点......),因为当新页面尚未加载时没有太多可以向下滑动的内容,所以我无法使用slideDown回调。
我现在有一个临时解决方案:我使用 if 语句来确定我在哪个页面上,然后执行动画的后半部分,即幻灯片部分。对于动画的第一部分,即 SlideUp 部分,我将“location.href”作为回调,并使用“return false”,如 Phrogz 所示。
像这样:
然后,在页面重新加载之后(还有更多 if 语句,每个页面一个):
现在我陷入困境,因为我必须找出一种方法来实现从一个页面到另一个页面之间的差异,从一个子页面到另一个子页面,从一个页面到另一个子页面,等等。但这将是另一个线程。
Thanks so much all of you, for the help!
It took me a while to get the code right (sort of...), because there's not much to slide down when the new page hasn't loaded yet, so I couldn't use the slideDown callback.
I now have a temporary solution which is: I use if statements to determine on which page I am, and then do the second half of the animation, the slideDown-part. For the first part of the animation, the slideUp-part, I have the 'location.href' as a callback, with 'return false' as Phrogz demonstrated.
Like this:
And then, after the page reloads (and some more if statements follow this, one for each page):
Now I'm stuck because I have to figure out a way to implement a difference between going from one page to another, and going from one sub-page to another sub-page, and from page to sub-page, etc. But that will be another thread.