问题
I recently asked about how to add cookies to a sidebar menu, and got help by amazing Trincot, here. Found and edited another code to get that to work, now the problem with that code is that it the cookies lag - if you close the sidebar and move to another page, it shows up for a second, then disappears, and considering the entire site's content moves with it, this is incredibly annoying. I think I found a similar question while looking for solution to my previous problem, but I can't remember what I searched for, and haven't been able to re-locate it. I think the fault is with the repetition of the code, but I need to be able to open and close it from two places. Any ideas, anyone?
<script type='text/javascript' src='http://yourjavascript.com/1211131136/jquery.cookie.js'></script>
<script type='text/javascript'>
/**code written by kismet of RPG-Directory.com**/
$(function(){
var website = 'SKY';
var cookie1 = website+'cbox';
if($.cookie(cookie1) === 'on' ||
$.cookie(cookie1) === null){
$('.menu').animate({
left: "-240px"
}, 200);
$('body').animate({
left: "0px"
}, 200); }
else {
$('.menu').animate({
left: "0px"
}, 200);
$('body').animate({
left: "240px"
}, 200); }
$('.icon-close').click(function(){
$.cookie(cookie1, 'on');
$('.menu').animate({
left: "-240px"
}, 200);
$('body').animate({
left: "0px"
}, 200);
});
$('.icon-menu').click(function(){
if($.cookie(cookie1) === 'on' ||
$.cookie(cookie1) === null){
$.cookie(cookie1, 'off');
$('.menu').animate({
left: "0px"
}, 200);
$('body').animate({
left: "240px"
}, 200); }
else {
$.cookie(cookie1, 'on');
$('.menu').animate({
left: "-240px"
}, 200);
$('body').animate({
left: "0px"
}, 200); }
});
});
</script>
Thanks a bunch!
来源:https://stackoverflow.com/questions/33520116/cookies-lagging