1
我在两个div之间有一个分离器,我可以移动分离器并更改两个容器的大小。我想要做的是保存分离器的位置,两个容器在缓存中的宽度以及刷新浏览器时我希望能够保留这些宽度和位置。这里的代码片段,jquery splitter在cookie中保存位置
var separatorPos = $('.content-columns-sep').position().left;
var leftColumnWidth = $('.content-left').width();
var rightColumnWidth = $('.content-right').width();
if ($.cookie('columnPos')) {
var cookieVars = $.cookie('columnPos').split(',');
var newPos = cookieVars[0];
var newWidth = cookieVars[1];
var newRightWidth = cookieVars[2];
var posDiff = newPos - separatorPos;
$('.content-columns-sep').offset({
left: newPos});
separatorPos = newPos;
$('.content-left').width(newWidth);
$('.content-right').width(newRightWidth);
leftColumnWidth = newWidth;
rightColumnWidth = newRightWidth;
}
$(".content-columns-sep").mouseover(function(){
$('.content-columns-sep').css('cursor', 'crosshair');
});
$(".content-columns-sep").draggable({
axis: "x",
containment: "parent",
cursor: "crosshair",
grid: [10, 0],
drag: function(event, ui) {
var newPos = $('.content-columns-sep').position().left;
var posDiff = newPos - separatorPos;
separatorPos = newPos;
var newWidth = leftColumnWidth + posDiff ;
var newRightWidth = rightColumnWidth - posDiff;
$('.content-left').width(newWidth);
$('.content-right').width(newRightWidth);
leftColumnWidth = newWidth;
rightColumnWidth = newRightWidth;
},
stop: function(event, ui) {
var newPos = $('.content-columns-sep').position().left;
var posDiff = newPos - separatorPos;
separatorPos = newPos;
var newWidth = leftColumnWidth + posDiff ;
var newRightWidth = rightColumnWidth - posDiff;
$('.content-left').width(newWidth);
$('.content-right').width(newRightWidth);
leftColumnWidth = newWidth;
rightColumnWidth = newRightWidth;
$.cookie('columnPos', separatorPos+','+newWidth+','+newRightWidth);
}
});
But I am unable to get the result as expected, splitter always overlaps one of the div by some 35 pixel or so, what could be the possible reasons ?
你已经在你粘贴的代码中使用了cookie,是代码不工作,还是你想扩展它?你可以提供一个[JsFiddle](http://www.jsfiddle.net),代码在某些标记的上下文中运行,以便我们可以看看可能出现的问题? –
当然我正在研究它 –