简体   繁体   English

用cookie定义类的最佳方法

[英]Best way to define a class with a cookie

I have a switcher that is changing backgrounds and setting a cookie using the jquery cookie script. 我有一个切换器,它正在更改背景并使用jquery cookie脚本设置co​​okie。 It looks something like: 看起来像:

$(document).ready(function(){
var colors = $.cookie('colors');

    $(".box_2f2").click(function(){
        $('body').removeClass('c000 cfff c2f2');
        $('body').addClass('c2f2');
        $.cookie('colors','middleGrey');
        return false;
        });
    $(".box_fff").click(function(){
        $('body').removeClass('c000 cfff c2f2');
        $('body').addClass('cfff');
        $.cookie('colors','white');
        return false;
        });
    $(".box_000").click(function(){
        $('body').removeClass('c000 cfff c2f2');
        $('body').addClass('c000');
        $.cookie('colors','black');
        return false;
        });
    if(colors == 'middleGrey')
    {
    $('body').addClass('c2f2');
    }
    if (colors == 'white')
    {
    $('body').addClass('cfff');
    }
    if(colors == 'black')
    {
    $('body').addClass('c000');
    }
    });

the problem with this is because the class isnt set until that piece of javascript is loaded or the content is cached, the page will blink the defualt white background before showing the cookied style. 问题在于,直到加载了该段javascript或内容被缓存后,该类才被设置,该页面将在显示cookie样式之前闪烁默认的白色背景。 What is a better way to code this so the class is applied earlier in the dom and does not have to wait for all of the scripts? 有什么更好的编码方法,以便将该类在dom中更早地应用并且不必等待所有脚本?

By using jQuery, you're dependent on the scripts to be either loaded from the cache or the web server. 通过使用jQuery,您依赖于要从缓存或Web服务器加载的脚本。 Unfortunately you'll have that bit of a flash while everything gets loaded. 不幸的是,在加载所有内容时,您会一闪而过。

ALTERNATIVELY, however, you could have something like the following applied just after opening the BODY tag of your page using NORMAL JavaScript (without being dependent on a library or framework): 但是,至少可以在使用NORMAL JavaScript打开页面的BODY标签之后应用以下内容(而不依赖于库或框架):

<script type="text/javascript">
    document.body.style["display"] = "none";
</script>

...then just set the "display" CSS property of the body back to "block" or whatever your preference is at the very end of your jQuery script there. ...然后只需将主体的“ display” CSS属性设置回“ block”或您的jQuery脚本结尾处的首选项即可。

This is the common flash-of-unstyled-markup problem. 这是常见的无样式标记问题。

You can frame the entire page in a div, mark it "display:none" or "visibility:collapse" at first, then display it at the end of your script. 您可以将整个页面放在div中,将其标记为“ display:none”或“ visibility:collapse”,然后在脚本末尾显示。 The page will be just the default browser background until your script completes, which may take some time. 在脚本完成之前,该页面只是默认的浏览器背景,这可能需要一些时间。

Consider having a "loading" graphic or splash screen before your script runs (if it takes more than 100-200ms), so that the user will know that something is happening and should wait a bit. 考虑在脚本运行之前准备一个“正在加载”图形或启动屏幕(如果需要100-200毫秒以上的时间),以便用户知道正在发生的事情,应该稍等片刻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM