是否可以使用CSS将DIV的垂直滚动条放在DIV的左侧?那么使用JavaScript呢?
是否可以使用CSS将DIV的垂直滚动条放在DIV的左侧?那么使用JavaScript呢?
我的需求很简单,因此选择了一种简单的CSS解决方案:
<div style="direction: rtl; height: 250px; overflow: scroll;">
<div style="direction: ltr; padding: 3px;">
Content goes here
</div>
</div>
你可以使用JQuery和这个插件:JScrollPane在任何你想要的地方添加一个伪滚动条。
好的,我编写了一个jQuery插件,可以为您提供完全本地化的左侧滚动条。
以下是其工作原理:
在窗格内注入一个内部 div
,以允许计算内容宽度 (content_width
)。然后,使用此宽度可以计算出本地滚动条的宽度:scrollbar_width = parent_width - content_width - horizontal_padding
。
在窗格内制作两个不同的 div
,都装满了内容。
一个用于“伪装”。它仅用于滚动条。使用负左边距,插件将其向左拉,以便只有滚动条可见(此 div
的内容在边缘处被截断)。
另一个 div
用于实际容纳可见滚动内容。
现在,绑定它们在一起。每50毫秒 (window.setInterval
),从“伪装”div
获取的scrollTop
偏移量应用于可见的滚动内容div
。所以,当您使用在 左侧的滚动条向上或向下滚动时,滚动偏移量会被应用回具有可见内容的 div
上。
这个解释可能很糟糕,并且实际上还有很多东西我没有描述,但是不再多说,就是这样:
$.fn.leftScrollbar = function(){
var items = $(this);
$(function(){
items.each(function(){
var e = $(this);
var content = e.html();
var ie = !jQuery.support.boxModel;
var w = e[ie?'innerWidth':'width'](), h = e[ie?'innerHeight':'height']();
//calculate paddings
var pad = {};
$(['top', 'right', 'bottom', 'left']).each(function(i, side){
pad[side] = parseInt(e.css('padding-' + side).replace('px',''));
});
//detect scrollbar width
var xfill = $('<div>').css({margin:0, padding:0, height:'1px'});
e.append(xfill);
var contentWidth = xfill.width();
var scrollerWidth = e.innerWidth() - contentWidth - pad.left - pad.right;
e.html('').css({overflow:'hidden'});
e.css('padding', '0');
var poserHeight = h - pad.top - pad.bottom;
var poser = $('<div>')
.html('<div style="visibility:hidden">'+content+'</div>')
.css({
marginLeft: -w+scrollerWidth-(ie?0:pad.left*2),
overflow: 'auto'
})
.height(poserHeight+(ie?pad.top+pad.bottom:0))
.width(w);
var pane = $('<div>').html(content).css({
width: w-scrollerWidth-(ie?0:pad.right+pad.left),
height: h-(ie?0:pad.bottom+pad.top),
overflow: 'hidden',
marginTop: -poserHeight-pad.top*2,
marginLeft: scrollerWidth
});
$(['top', 'right', 'bottom', 'left']).each(function(i, side){
poser.css('padding-'+side, pad[side]);
pane.css('padding-'+side, pad[side]);
});
e.append(poser).append(pane);
var hRatio = (pane.innerHeight()+pad.bottom) / poser.innerHeight();
window.setInterval(function(){
pane.scrollTop(poser.scrollTop()*hRatio);
}, 50);
});
});
};
在页面中引入jQuery和该插件后,应用左侧滚动条:
$('#scrollme').leftScrollbar();
将#scrollme
替换为您想应用左滚动条的元素的CSS选择器。
(显然,这样可以很好地降级)