让麻烦的 JQuery Switcher 真正工作起来
我正在尝试自学 Jquery 效果以使我的网站充满活力。我创建了一个如下所示的练习页面:
<article><a class="front-fire" href="1"></a></article>
<article><a class="front-fire" href="2"></a></article>
<article><a class="front-fire" href="3"></a></article>
<div class="presentation-wrapper">
<div class="load"></div>
<div class="presentation"></div>
</div>
<div class="kill></div>
<article><a class="front-fire" href="4"></a></article>
<article><a class="front-fire" href="5"></a></article>
<article><a class="front-fire" href="6"></a></article>
<div class="presentation-wrapper">
<div class="load"></div>
<div class="presentation"></div>
</div>
<div class="kill></div>
它的目标是,如果我单击链接,它会展开最近的 presentation-wrapper
淡出加载 gif,加载内容,淡出 gif ,然后显示内容。单击另一个链接后,它会隐藏所有打开的演示包装器,然后重新显示打开序列。我想我已经掌握了如何将动画串在一起,但我不知道如何循环遍历 div
类并隐藏可见的动画。
这是我在外部文件中的 javascript。 #primary
附加用于我的调试目的:
$(document).ready(function() {
$('a').click(function(e) {
e.preventDefault();
$('#primary').append("pressed");
var toLoad = $(this).attr('href')+' #post-body';
var wrap = $(this).parents().filter('article').nextUntil('.kill', '.presentation-wrapper');
var load = $(wrap).children('.load');
var pres = $(wrap).children('.presentation');
var allwraps = $(".presentation-wrapper");
$(".presentation-wrapper").each(function() {
if ($(this).is(':visible')) {
$('#primary').append("6");
$('.presentation').fadeOut(3000, function() {
$('#primary').append("7");
$('.presentation-wrapper').slideUp(3000);
});
}
});
switcher();
function switcher() {
$(wrap).slideDown(3000, loadLoader);
$('#primary').append("1");
function loadLoader() {
$(load).fadeIn(3000, loadContent);
$('#primary').append("2");
}
function loadContent() {
$(pres).load(toLoad, hideLoader);
$('#primary').append("3");
}
function hideLoader() {
$(load).fadeOut(3000, showNewContent);
$('#primary').append("4");
}
function showNewContent() {
$(pres).fadeIn(3000);
$('#primary').append("5");
}
};
});
});
我知道这是一个大问题,也许这不是问的地方,但我一直在用头撞墙试图解决这个问题出了什么问题。它在第一次点击时有效,但是 .each()
似乎把整个事情搞砸了!
::补充:也许这个细节会有所帮助。由于某种原因,只要任何 div 随时可见,.each() 中的“7”函数就会不断触发。就像一个警察在接到电话后否认发生的任何事情!::
I'm trying to teach myself Jquery effects to enliven my websites. I've created a practice page that looks like this:
<article><a class="front-fire" href="1"></a></article>
<article><a class="front-fire" href="2"></a></article>
<article><a class="front-fire" href="3"></a></article>
<div class="presentation-wrapper">
<div class="load"></div>
<div class="presentation"></div>
</div>
<div class="kill></div>
<article><a class="front-fire" href="4"></a></article>
<article><a class="front-fire" href="5"></a></article>
<article><a class="front-fire" href="6"></a></article>
<div class="presentation-wrapper">
<div class="load"></div>
<div class="presentation"></div>
</div>
<div class="kill></div>
It's goal is that if I click a link, it expands the nearest presentation-wrapper
fades a loading gif up, loads the content, fades out the gif, then shows the content. Once another link is clicked it hides any open presentation-wrapper
s then shows does the opening sequence afresh. I think I've got a handle on how to string animations together, but I can't figure out how to cycle through a class of div
s and hide the ones that are visible.
Here's my javascript in an external file. The #primary
appends are for my debugging purposes:
$(document).ready(function() {
$('a').click(function(e) {
e.preventDefault();
$('#primary').append("pressed");
var toLoad = $(this).attr('href')+' #post-body';
var wrap = $(this).parents().filter('article').nextUntil('.kill', '.presentation-wrapper');
var load = $(wrap).children('.load');
var pres = $(wrap).children('.presentation');
var allwraps = $(".presentation-wrapper");
$(".presentation-wrapper").each(function() {
if ($(this).is(':visible')) {
$('#primary').append("6");
$('.presentation').fadeOut(3000, function() {
$('#primary').append("7");
$('.presentation-wrapper').slideUp(3000);
});
}
});
switcher();
function switcher() {
$(wrap).slideDown(3000, loadLoader);
$('#primary').append("1");
function loadLoader() {
$(load).fadeIn(3000, loadContent);
$('#primary').append("2");
}
function loadContent() {
$(pres).load(toLoad, hideLoader);
$('#primary').append("3");
}
function hideLoader() {
$(load).fadeOut(3000, showNewContent);
$('#primary').append("4");
}
function showNewContent() {
$(pres).fadeIn(3000);
$('#primary').append("5");
}
};
});
});
I know this is a big question, and maybe this isn't the place to ask, but I've been banging my head against the wall trying to suss out what is wrong. It works on the first click, but the .each()
seems to screw the whole thing up!
::Addition: maybe this detail will help. For some reason the "7" function in .each() keeps firing whenever any of the divs are visible at any time. Like it's a policeman negating anything that happens after it is called!::
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上还没有尝试过这个,但看起来问题出在
.each()
块中 - 当您引用$('.presentation')
和if
语句中的$('.presentation-wrapper')
是否希望它仅引用当前的.presentation-wrapper
并且它的子.presentation
?因为事实上,这些语句引用了页面上与这些选择器匹配的所有元素。这就是"7"
位每次都会触发的原因。我认为这应该做到这一点:
这里有功能齐全的示例。
但是,实际上,在没有
.each()
函数的情况下执行此操作实际上更干净:此外,我意识到,如果您尝试将内容加载到同一个中,您将需要额外的逻辑
.presentation-wrapper
- 在这种情况下,您只想立即隐藏子.presentation
:在这里更新了 jsFiddle。
I haven't actually tried this out, but it looks like the problem is in the
.each()
block - when you refer to$('.presentation')
and$('.presentation-wrapper')
in theif
statement, are you expecting this to only refer to the current.presentation-wrapper
and its child.presentation
? Because in fact, these statements are referring to all elements on the page matching those selectors. That's why the"7"
bit fires every time.I think this should do it:
Fully functional example here.
But, it's actually cleaner to do this without the
.each()
function at all:Also, I realized that you're going to need additional logic if you're trying to load content into the same
.presentation-wrapper
- you just want to hide the child.presentation
immediately in that case:Updated jsFiddle here.