我想做一个布局切换器。用户点击一个名为display
的特定div,然后它将一个名为display-grid
的类切换为该类严格用于视觉。jQuery Cookie没有设置每个点击就像它应该
与此同时,它为我的布局切换打开和关闭一些类。
我有一个左列和右列,对象是让用户选择哪一列在页面的哪一侧。就像一个侧栏可以在页面的左侧或右侧。
他们切换这种变化,要么添加或从我的其他2周的div删除类(类只是增加一个float: left;
和float:right;
并交换出来每个切换点击。
现在每个拨动它每次也保存状态到用户的cookie,所以如果他们改变侧边栏的位置,它将持续在整个网站上。
我的代码是草率的我不知道100 $如何做到这一点,但我做了,管理让它工作,然后它就停止了,上面描述的所有东西都可以工作,除了cookie现在给我一个问题,他们没有在每次改变时都设置一个cookie,这很奇怪因为大约10个小时前我有它的工作,所以我不知道发生了什么。
我希望有更多的Javascript知识的人可以帮助我得到这个工作,甚至可能改善它,但让它现在工作将使我的一天。
下面是我有这个代码。
不包括的是jQuery库,但我已经包含了cookie插件。
$(window).load(function(){
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};
我的代码
jQuery.noConflict();
jQuery(document).ready(function ($) {
// Layout Switcher for post list
// Load existing setting from Cookie
if ($.cookie('MODE_SWITCHER') == 'null') {
$('#content-left').attr('class', 'right-sidebar')
$('#content-right').attr('class', 'left-sidebar')
} else{
$('#content-right').attr('class', 'right-sidebar')
$('#content-left').attr('class', 'left-sidebar')
}
// Change LIVE by clicking the Toggle button on the site, save new value to Cookie
$('.display').click(function() {
$(this).toggleClass('display-grid');
$('#content-left').toggleClass('left-sidebar').toggleClass('right-sidebar');
$('#content-right').toggleClass('right-sidebar').toggleClass('left-sidebar');
if ($('#content-left').hasClass('left-sidebar')) {
var postmode = 'leftbody';
} else { //var postmode = 'right-sidebar';
var postmode = '';
}
// save preference to Cookie
$.cookie('MODE_SWITCHER', postmode, {
path: '/',
expires: 10000
});
return false;
});
});
HTML
<span class="display" title="Change Layout">List/Grid</span>
<div id="content-left"> Left column content </div>
<div id="content-right"> Right column content </div>
CSS
.display {
float:left;
width: 49px;
height: 20px;
margin-top: -2px;
background: url(http://codedevelopr.com/uploads/grid-switcher.gif) no-repeat 0 0;
text-indent: -5555em;
overflow: hidden;
cursor: pointer;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
.display-grid {
background: url(http://codedevelopr.com/uploads/grid-switcher.gif) no-repeat 0 -20px;
}
.grid-content .gridrow {
border-bottom: 1px solid #ECEDE8;
margin: 0 15px;
}
.left-sidebar{
float:left !important;
margin-right:15px;
}
.right-sidebar{
float:right !important;
margin-right:15px;
}
我想出了一些。似乎它正在写cookies大多数时候......当jQuery文档声称你可以使用'。.attr('class','replacement-class')'替代使用'.removeClass(“left-侧边栏“)。addClass(”右侧栏“)'因为我切换回该方法,大部分工作。我假设第一个不会很好玩,当有可能类名是空的有时 – JasonDavis 2011-12-24 19:51:41