cookies - Jquery Style switcher - How to remove style flickering while a .css loads? -
i have simple code change styles simple clic. works fine has style flickering prevent - little timeout between 1 disabled , other not totaly loaded -
also add cookie user won't have select prefered style everytime he/she enters. i'm not sure how i'm new jquery. if able me great!
thanks reading!
i have code switch styles on site:
html call main style , main color
<style type="text/css"> @import url("style.css");</style> <link href="yellow.css" rel="stylesheet" type="text/css" title="yellow theme" />
then call scripts usual
<script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript" src="script.js"></script>
each button has this:
<a class="colorbox colorred" href="?theme=red" title="red theme"><img src="red.png" width="x" height="x" /></a>
now here script.js code:
google.load("jquery", "1.3.1"); google.setonloadcallback(function() { // color changer $(".colorblack").click(function(){ $("link").attr("href", "black.css"); return false; }); $(".colorred").click(function(){ $("link").attr("href", "red.css"); return false; }); $(".colorwhite").click(function(){ $("link").attr("href", "white.css"); return false; }); $(".coloryellow").click(function(){ $("link").attr("href", "yellow.css"); return false; }); });
ok. i've done little more research , experimentation this, , think i've found solution. first background info. far can tell, problem have flickering css not being loaded. want loading done @ page load long it's not major performance issue. load them dynamically needed leads problem have here, trying use data not yet loaded. best solution can see load of css @ start , change precedence dynamically. have tested , work. cascading style sheets (css), cascading part of name means when load style sheet , load one, last 1 takes overriding precedence. first thought add style sheet override previous one. way, loaded, old style sheet still being used , there no flicker. problem ran linked style sheet precedence reversed, meaning have override first 1 , not last one. here code illustrating process:
<html> <head> <link href="yellow.css" rel="stylesheet" type="text/css" title="yellow theme" /> <link href="black.css" rel="stylesheet" type="text/css" title="black theme" /> <link href="white.css" rel="stylesheet" type="text/css" title="white theme" /> <link href="red.css" rel="stylesheet" type="text/css" title="red theme" /> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $(document).ready(function(){ $(".colorblack").click(function(){ $("link:first").attr("href", "black.css"); return false; }); $(".colorred").click(function(){ $("link:first").attr("href", "red.css"); return false; }); $(".colorwhite").click(function(){ $("link:first").attr("href", "white.css"); return false; }); $(".coloryellow").click(function(){ $("link:first").attr("href", "yellow.css"); return false; }); }); </script> </head> <body> <a class="colorbox colorred" href="?theme=red" title="red theme">red theme</a></br> <a class="colorbox colorblack" href="?theme=black" title="black theme">black theme</a></br> <a class="colorbox colorwhite" href="?theme=white" title="white theme">white theme</a></br> <a class="colorbox coloryellow" href="?theme=yellow" title="yellow theme">yellow theme</a></br> </body> </html>
and css pretty simple. e.g.
body { background-color:yellow; }
this works in chrome , ff, not work in ie or opera. replacing link nodes seems not work in two. basically, that's how it. :)
Comments
Post a Comment