1

我必须在 cookie 中维护侧边栏的状态。所以我所做的只是单击隐藏侧边栏按钮,我创建了 cookie 以保持其状态。看下面的代码。

//Create Cookie function which is usefull to create cookies

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
//Read Cookie function which is usefull to read cookies vlues
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
//Earse Cookie function which is usefull to earise cookies vlues
function eraseCookie(name) {
    createCookie(name,"",-1);
}
// This function is used for  sidebar state managemnet operations.
function checksidebarstatus()
{
    var sidebarval = '';
    sidebarval = readCookie('sidebarval');
    if(sidebarval == 'hide')
    {   
        jQuery(".show-sidebar").fadeIn();
        jQuery("#main").css( "width","950" );
        jQuery("#sidebar").hide();
    }
}

现在当 hidesidar 按钮代码到 hidesidebar 如下

// Sidebar Animation
jQuery("#hide-sidebar").click(function(){

    if(sidebarval == "hide")
    {
        //jQuery("#sidebar").delay(500).hide("slow");
        jQuery("#sidebar").hide("slide", { direction: "left" }, 500);
    }else
    {
        jQuery("#sidebar").hide("slide", { direction: "left" }, 500);
    }
    jQuery("#main").delay(500).animate({
        width: 950
    }, 1000 );
    jQuery(".show-sidebar").delay(1500).fadeIn();
    createCookie('sidebarval','hide',7);
});

jQuery("#show-sidebar").click(function(){

    jQuery(".show-sidebar").fadeOut();
    jQuery("#main").animate({
        width: 760
    }, 500 );
    if(sidebarval == "hide")
    {    
        //jQuery("#sidebar").delay(500).show("slow");
        jQuery("#sidebar").delay(500).show("slide", { direction: "left" }, 1000);
    }else
    {
        jQuery("#sidebar").delay(500).show("slide", { direction: "left" }, 1000);
    }   
    createCookie('sidebarval','show',7);
});

现在我有 js 变量来获取 cookie 数据 id 如下

var sidebarval = readCookie('sidebarval');

现在作为我必须运行的代码,但是当我获得 cookie 值时,它将不允许这样做

jQuery("#sidebar").delay(500).show("slide", { direction: "left" }, 1000);

如果我取消注释此行,则此 show 方法有效。

jQuery("#sidebar").delay(500).show("slow");
//jQuery("#sidebar").delay(500).show("slide", { direction: "left" }, 1000);

谁能告诉我这是怎么可能的。谢谢。

4

1 回答 1

0

您需要在项目中包含JQuery UI文件。最适合您的节目是因为它位于默认的 JQuery 代码库中。底部的 show 方法(不起作用的方法)是 JQuery UI Effects Core 的一部分。您需要自己构建包含此选项的 JQuery UI 文件,它将起作用。

这是那里显示功能的链接:http: //jqueryui.com/demos/show/

于 2012-05-09T09:49:32.273 回答