绝对定位元素内的本机滚动条

Luk*_*tný 8 css

我在元素上的滚动条有一些问题position: absolute.我遇到的问题是,从Chrome 21和Firefox 15显示箱内滚动条,调整它的内容从而隐藏一些文字,但歌剧12和Internet Explorer 9级的显示器也放在里面,但不调整它的内容和调整相反的盒子(在我看来是正确的,因为盒子没有定义宽度).是否有任何解决方案可以使这4种浏览器看起来相同?

JsFiddle:http://jsfiddle.net/Kukkimonsuta/GaMD7/2/

编辑:正如Siva Charan指出的那样,当overflow-y设置为"滚动"时,它可以正常工作,但是显示滚动条总是不需要

编辑:根据Siva Charan匿名投票的答案我的最终解决方案是蹩脚的

http://jsfiddle.net/Kukkimonsuta/GaMD7/15/

function updateAutoScroll(element) {
    var $element = $(element);

    if (element.scrollHeight > element.clientHeight) 
        $element.css("overflow-y", "scroll");
    else 
        $element.css("overflow-y", "auto");
}
Run Code Online (Sandbox Code Playgroud)

iam*_*eed 2

在所有浏览器中动态执行此操作的唯一方法是使用 JavaScript,为简单起见,我使用了 jQuery。

http://jsfiddle.net/iambriansreed/mYuQx/

$(function(){

    // loops through each container
    $('.container').each(function(){                
        if(this.scrollHeight>this.clientHeight)
            $(this).children().wrapAll(
                '<div style="padding-right:'+scrollbarWidth()+'px;"/>'
            );
    });

    // gets the browsers current scrollbar width
    function scrollbarWidth() {
        var parent, child, width;    
        if(width===undefined) {
            parent = $('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body');
            child = parent.children();
            width = child.innerWidth() - 
                child.height(99).innerWidth();
            parent.remove();
        }    
        return width;
    };

});
Run Code Online (Sandbox Code Playgroud)