带有切换功能的 jQuery 幻灯片效果?
嘿伙计们...我正在尝试产生一种效果,如果我单击按钮,则 1 个面板会滑到左侧,另一个面板会滑入视图。如果我再次单击,当前的会滑开,而之前的会像切换按钮一样滑回。
我有以下代码,它的工作原理“有点”...我的代码在第一次单击时看起来不错,但第二次单击时,面板在查看面板第一次隐藏之前重叠。
因此,为了简化我要寻找的内容,假设您有 2 个 400x600 的 div,默认情况下您正在查看 div 1,单击按钮时,div 1 向左滑出视图,div 2 也从左侧滑入div 1 隐藏后,再次单击时,div 2 向左滑动,div 1 也从左侧滑回……它会切换。
jQuery(".button").click(function() {
var $lefty = jQuery(".home");
$lefty.animate({
left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0},
function() {
jQuery(".member_home").show();
var $lefty = jQuery(".member_home");
$lefty.animate({
left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0
});
});
return false;
});
如果有人可以在这里帮助我,那就太好了!谢谢!
Hey guys...I am trying to produce an effect where if I click on a button 1 panel slides to the left and another slides into view. And if I click again, the current one slides away and the one from before slides back in like a toggle button.
I have the following code and it works "sort of"...My code when clicked looks ok the first time clicked, but the second time, the panel over laps before the viewing one hides first.
So to simplify what i am looking for is imagine you have 2 divs 400x600 and you're viewing div 1 by default and when the button is clicked, div 1 slides to the left out of view, and div 2 slides in also from the left after div 1 is hidden and when clicked again, div 2 slides to the left and div 1 slides back in also from the left...it toggles..
jQuery(".button").click(function() {
var $lefty = jQuery(".home");
$lefty.animate({
left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0},
function() {
jQuery(".member_home").show();
var $lefty = jQuery(".member_home");
$lefty.animate({
left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0
});
});
return false;
});
If anyone can give me a hand here, it would be great! thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于,您总是先对
.home
进行动画处理,然后对.member_home
进行动画处理,但实际上情况并非总是如此。当.home
离开屏幕时,您希望首先将.member-home
动画移出屏幕,然后再将.home
动画化。您看到的效果是因为您始终以相同的顺序对div
进行动画处理,无论哪个在屏幕外。这是一个添加逻辑来解决该问题的函数:
然后从按钮单击处理程序调用该函数:
在此处查看一个工作示例: http://jsfiddle.net/andrewwhitaker/qSvDz/。
I think the problem is that you're always animating
.home
followed by.member_home
, when this isn't actually the case all the time. When.home
is off-screen, you want to animate.member-home
off the screen first, and then animate.home
in. The effect you're seeing is because you're always animating thediv
s in the same order, no matter which is off-screen.Here's a function that adds logic that will fix that:
And then call that function from your button click handler:
Check out a working example here: http://jsfiddle.net/andrewwhitaker/qSvDz/.
将第二个面板的滑入设为回调函数。然后它不会滑入,直到第一个面板被隐藏。
编辑
Make the sliding in of the second panel a callback function. Then it won't slide in until the first panel has been hidden.
edit