使用 2 个元素进行 Jquery 切换

发布于 2024-12-15 14:04:07 字数 862 浏览 3 评论 0原文

这是我的脚本,我正在使用切换开关,这样我就可以为滑动菜单设置动画。

    $("div.show-menu").click().toggle(
        function() {
            // first alternation
            $("#slideover").animate({
                right: "512px"
                }, 300);
            $(".menu-button").html('close menu');


        }, function() {
            // second alternation
            $("#slideover").animate({
                right: "0"
                }, 300);
            $(".menu-button").html('open menu');

    });

尽管我确实需要切换才能使用页面上的 2 个元素。例如,请参见下面...

<div class="show-menu one">open menu</div> // this is element one

<div class="show-menu two">open menu</div> // this is element two

我需要它,如果首先单击元素一,您可以在第一次单击时使用元素二关闭菜单。现在发生的情况是,您必须单击元素二两次才能关闭菜单 - 反之亦然,

我想这是正在发挥作用的切换,任何帮助将非常感谢。

干杯 乔什

this is my script, I'm using a toggle so I can animate a sliding menu.

    $("div.show-menu").click().toggle(
        function() {
            // first alternation
            $("#slideover").animate({
                right: "512px"
                }, 300);
            $(".menu-button").html('close menu');


        }, function() {
            // second alternation
            $("#slideover").animate({
                right: "0"
                }, 300);
            $(".menu-button").html('open menu');

    });

Though I really need the toggle to work using 2 elements on the page. For example see below...

<div class="show-menu one">open menu</div> // this is element one

<div class="show-menu two">open menu</div> // this is element two

I need it so, if element one get's click first, you can close the menu using element two on the first click. What is happening now is that you have to click element two twice in order for it to close the menu - vice versa

I guess this is the toggle coming into play, any help would be great thanks.

Cheers
Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

她说她爱他 2024-12-22 14:04:08
$('.show-menu').on('click', function () {

    //check the state of the .menu-button, if it's closed then run the open animation, if it's open then run the close animation
    if ($(".menu-button").html() == 'close menu') {
        $("#slideover").animate({
            right: "0"
        }, 300);
        $(".menu-button").html('open menu');
    } else {
        $("#slideover").animate({
            right: "512px"
        }, 300);
        $(".menu-button").html('close menu');
    }
});

.on() 是 jQuery 1.7 中的新功能,如果您使用的是 jQuery .bind(),则在此实例中将替换 .bind() 1.7 然后使用.bind()

$('.show-menu').on('click', function () {

    //check the state of the .menu-button, if it's closed then run the open animation, if it's open then run the close animation
    if ($(".menu-button").html() == 'close menu') {
        $("#slideover").animate({
            right: "0"
        }, 300);
        $(".menu-button").html('open menu');
    } else {
        $("#slideover").animate({
            right: "512px"
        }, 300);
        $(".menu-button").html('close menu');
    }
});

.on() is new in jQuery 1.7 and replaces .bind() in this instance, if you are using jQuery < 1.7 then use .bind().

失与倦" 2024-12-22 14:04:08

怎么样

$('.show-menu').on('click', function () {
    if ($("#slideover").css("right") == '512px') {
        $("#slideover").animate({
            right: "0"
        }, 300);
        $(".menu-button").html('open menu');

    } else {
        $("#slideover").animate({
            right: "512px"
        }, 300);
        $(".menu-button").html('close menu');
    }
});

What about

$('.show-menu').on('click', function () {
    if ($("#slideover").css("right") == '512px') {
        $("#slideover").animate({
            right: "0"
        }, 300);
        $(".menu-button").html('open menu');

    } else {
        $("#slideover").animate({
            right: "512px"
        }, 300);
        $(".menu-button").html('close menu');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文