私はレイアウトスイッチャーを作ろうとしています。ユーザーがdisplay
という特定のdivをクリックすると、display-grid
というクラスが厳密に視覚的にそのクラスに切り替わります。jQuery Cookieが各クリックで設定されていないようです
同時に、私のレイアウトのためにいくつかのクラスをオン/オフします。
私は左の列と右の列を持っていますが、目的はユーザーがページのどの辺にあるかを選択させることです。サイドバーのように、ページの左側または右側にすることができます。
彼らはそれは(クラスがちょうどfloat: left;
とfloat:right;
を追加し、各トグルクリックでそれらをスワップしている私の2つの他のdivのクラスを追加または削除のいずれか、この変更を切り替えるたびに。
今トグルごとにまた、ユーザーのCookieに状態を保存するので、サイドバーの場所が変更されても、サイト全体に保持されます。
私のコードはわかりません100 $これを行う方法、まあ、上記のすべてのことは、クッキーが私に今問題を与えていることを除いて動作します、彼らは変更が行われるたびにクッキーを設定していません。約10時間前に私はそれが働いていたので、何が起こったのか分かりません。
私はJavascriptの知識が豊富な人がこの作業を助けたり、それを改善したりすることができますが、現在はそれを稼働させることが私の一日を成し遂げてくれることを願っています。
以下はこれまでのコードです。
jQueryライブラリは含まれていませんが、Cookieプラグインが含まれています。クッキーのコードに到達することはありませんので、あなたは右のあなたのクッキーのコードの前にreturnステートメントを持って
$(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;
}
私はこれをいくつか考え出しました。クッキーを書いていると思われるほとんどの場合、jQueryのドキュメントは、.removeClass( "left-class")の代わりに '$ .attr( 'class'、 'replacement-class'サイドバー ")。addClass(" right-sidebar ")'私はそれをそのメソッドに戻し、そのほとんどが動作するからです。私はクラス名が空である可能性があるときに、最初のものがうまくいきませんと仮定しています。 – JasonDavis