带有切换功能的 jQuery 幻灯片效果?

发布于 2024-10-09 10:41:02 字数 755 浏览 0 评论 0原文

嘿伙计们...我正在尝试产生一种效果,如果我单击按钮,则 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2024-10-16 10:41:02

我认为问题在于,您总是先对 .home 进行动画处理,然后对 .member_home 进行动画处理,但实际上情况并非总是如此。当 .home 离开屏幕时,您希望首先将 .member-home 动画移出屏幕,然后再将 .home 动画化。您看到的效果是因为您始终以相同的顺序对 div 进行动画处理,无论哪个在屏幕外。

这是一个添加逻辑来解决该问题的函数:

function toggleDivs() {
    var $home = $("#home");
    var $memberHome = $("#member-home");
    var $slideOut, $slideIn;

    // See which <divs> should be animated in/out.
    if ($home.position().left < 0) {
        $slideIn = $home;
        $slideOut = $memberHome;
    }
    else {
        $slideIn = $memberHome;
        $slideOut = $home;
    }

    $slideOut.animate({
        left: "-" + $slideOut.width() + "px"
    }, function() {
        $slideIn.animate({ left: "0px" });
    });
}

然后从按钮单击处理程序调用该函数:

$("button").bind("click", function() {
    toggleDivs();
});

在此处查看一个工作示例: 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 the divs in the same order, no matter which is off-screen.

Here's a function that adds logic that will fix that:

function toggleDivs() {
    var $home = $("#home");
    var $memberHome = $("#member-home");
    var $slideOut, $slideIn;

    // See which <divs> should be animated in/out.
    if ($home.position().left < 0) {
        $slideIn = $home;
        $slideOut = $memberHome;
    }
    else {
        $slideIn = $memberHome;
        $slideOut = $home;
    }

    $slideOut.animate({
        left: "-" + $slideOut.width() + "px"
    }, function() {
        $slideIn.animate({ left: "0px" });
    });
}

And then call that function from your button click handler:

$("button").bind("click", function() {
    toggleDivs();
});

Check out a working example here: http://jsfiddle.net/andrewwhitaker/qSvDz/.

青衫负雪 2024-10-16 10:41:02

将第二个面板的滑入设为回调函数。然后它不会滑入,直到第一个面板被隐藏。
编辑

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(400,function() {
        var $lefty = jQuery(".member_home");
           $lefty.animate({
           left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0
    });
});

});
    return false;
});

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

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(400,function() {
        var $lefty = jQuery(".member_home");
           $lefty.animate({
           left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0
    });
});

});
    return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文