CSS切换器-寻找更无缝的实现
CSS switcher - looking for more seamless implementation
我正在使用一个JS CSS切换器,效果很好,真的很出色。然而,如果它能更无缝地工作,我会很高兴。在网站上打开一个新页面时,默认的css样式通常会短暂地闪烁,然后cookie会重新应用所选择的css样式。
。画布样式是默认样式,它在第一次访问网站时打开,用户选择公司样式,他们打开网站中的另一个页面-画布样式显示一瞬间,然后公司样式加载到它上面。在旧电脑上情况更糟,但在我的主电脑上,Firefox不经常出现这种情况,尽管在其他浏览器上,尤其是Chrome,这种情况非常明显。有没有人有专业知识来更新下面的工作方式,比如,首先检查cookie,如果没有cookie,则应用默认样式,而不是同时应用默认样式?
我使用的代码如下:
:
<script type="text/javascript">
$(function()
{
// Call stylesheet init so that all stylesheet changing functions
// will work.
$.stylesheetInit();
// This code loops through the stylesheets when you click the link with
// an ID of "toggler" below.
$('#toggler').bind(
'click',
function(e)
{
$.switcher();
return false;
}
);
// When one of the styleswitch links is clicked then switch the stylesheet to
// the one matching the value of that links rel attribute.
$('.styleswitch').bind(
'click',
function(e)
{
$.stylesheetSwitch(this.getAttribute('rel'));
return false;
}
);
}
);
</script>
<link rel="stylesheet" type="text/css" href="/css/canvas.css " title="canvas">
<link rel="alternate stylesheet" type="text/css" href="/css/corporate.css " title="corporate">
<link rel="alternate stylesheet" type="text/css" href="/css/earth.css " title="earth">
<link rel="alternate stylesheet" type="text/css" href="/css/space-and-stars.css " title="space-and-stars">
<link rel="alternate stylesheet" type="text/css" href="/css/under-the-sea.css " title="under-the-sea">
<link rel="alternate stylesheet" type="text/css" href="/css/classical.css " title="classical">
<link rel="alternate stylesheet" type="text/css" href="/css/creative.css " title="creative">
JS
(function($)
{
// Local vars for toggle
var availableStylesheets = [];
var activeStylesheetIndex = 0;
// To loop through available stylesheets
$.switcher = function()
{
activeStylesheetIndex ++;
activeStylesheetIndex %= availableStylesheets.length;
$.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
};
// To switch to a specific named stylesheet
$.stylesheetSwitch = function(styleName)
{
$('link[@rel*=style][title]').each(
function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) {
this.disabled = false;
activeStylesheetIndex = i;
}
}
);
createCookie('style', styleName, 365);
};
// To initialise the stylesheet with it's
$.stylesheetInit = function()
{
$('link[rel*=style][title]').each(
function(i)
{
availableStylesheets.push(this.getAttribute('title'));
}
);
var c = readCookie('style');
if (c) {
$.stylesheetSwitch(c);
}
};
}
)(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
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=/";
}
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;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
// /cookie functions
我会在服务器端做…
无论如何,当你做的时候
$(function()
{
});
jQuery等待直到DOM完全加载后才执行函数。
因此,您应该将javascript放置在<link />
s部分下方,在$(function(){});
之外。这将使该脚本在浏览器解析后立即生成,并且在页面完全加载之前生成。
我认为您可能希望在DOM准备好之前运行代码。也许你可以立即运行它,而不是使用$(function(){…}),因为jQuery等待,直到页面加载执行任何东西(因此闪烁)。
也许这也能给我们一些启示。http://forum.jquery.com/topic/use jquery - - - dom之前准备好- 12 - 1 - 2010
链接不工作的调试步骤:
>>> $('link[@rel*=style][title]') --> seems to show your styles are there
[..., <link rel="alternate stylesheet" type="text/css" href="/css/corporate.css " title="corporate">, ...]
>>> $.stylesheetSwitch('corporate') --> seems to work
唯一的问题是链接没有任何东西绑定到onclick;等待……你是说this.getAttribute('title')吗?正如你在上面看到的,rel="alternate stylesheet"可能并不是你想用它作为唯一的主题标识符。
- 为样式表切换器存储js cookie
- 与号.js多个视图切换器
- 通过Alloy UI(Liferay 6.2)在切换器中加载视频
- 当我单击返回第一个切换时,第二个切换器应该关闭
- 在Javascript和php中实现计时器的最佳方法
- Javascript样式表切换器
- 使用Javascript为样式切换器添加样式表的最佳方法
- 选中取消选中切换器
- jQuery内容面板切换器阻止访问表单元素
- 在 Jquery 中为语言切换器分配一个全局变量
- UIkit:将切换器选项卡设置为当前窗口哈希
- 多语言语言切换器 asp.net
- 即时CSS样式切换器/滚轮
- 如何构建一个jQuery驱动的切换器来添加/删除css属性
- jQuery切换器需要限制所选开关的数量
- 寻找一种风格切换器
- 使用jQuery和下拉菜单的简单样式表切换器
- 我创建了一个CSS切换器,但我只需要更改class=“”的元素;红色”;身体
- jquery css样式切换器将cookie设置为css
- CSS切换器-寻找更无缝的实现