【问题标题】:Separate Buttons for jQuery toggle event with cookies带有 cookie 的 jQuery 切换事件的单独按钮
【发布时间】:2014-09-20 15:39:36
【问题描述】:

我制作了一个 jQuery 切换,用户可以在其中选择主题的宽度,使其变得流畅或使用切换按钮固定。 事情工作正常,但我想添加单独的切换按钮,一个用于单击时固定宽度,另一个用于单击时流体宽度。

我试过了,但我没有成功。

这是我使用的代码

   $(document).ready(function () {
var setWidth = $.cookie("width");
if (typeof setWidth !== "undefined" && setWidth == "85%") {
    $('#container,.wrp').width(setWidth); // 85% width set using cookie
} else if (typeof setWidth !== "undefined" && setWidth == "960px") {
    $('#container,.wrp').width(setWidth); // 960px,1000px(for wrp) width set using cookie
}
$('#toggle-button').click(function () {
    var toggleWidth = $("#container").width() == 960 ? "85%" : "960px";

    $('#container, .wrp').animate({
        width: toggleWidth
    });
    $.cookie('width', toggleWidth);
});
});

这里是小提琴:http://jsfiddle.net/envira/kTrLL/

谢谢。

【问题讨论】:

    标签: jquery cookies width toggle


    【解决方案1】:

    您可以使用新按钮添加另一个点击功能。

    $('#toggle-button-resp').click(function () {
        var toggleWidth = "85%";
    
        $('#container, .wrp').animate({
            width: toggleWidth
        });
        $.cookie('width', toggleWidth);
    });
    

    jsfiddle

    【讨论】:

    • 您好,非常感谢您的帮助。是的,我试过这个并且它有效,但我的意思是切换开关是否有任何可能性:如果单击固定,固定将被隐藏并在同一个块中,显示流体按钮。就像按钮相互重叠。我希望你明白我所说的。谢谢
    • 我的意思是这里有一个小提琴的例子:jsfiddle.net/envira/NS9Z8@Emanuele Parisio 谢谢。
    • jsfiddle.net/gasp10/NS9Z8/3 切换 .hidden 类,如果可以的话不要使用内联样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2017-12-23
    相关资源
    最近更新 更多