微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我在使用position的元素上的滚动条有一些问题:绝对.我正在经历的行为是chrome 21和firefox 15在框内显示滚动条,调整其内容,从而隐藏了一些文本,但是歌剧12和Internet Explorer 9也在内部显示它,但没有调整其内容并调整大小相反的盒子(在我看来是正确的,因为盒子没有定义宽度).是否有任何解决方案可以使这4种浏览器看起来相同?

Jsfiddlehttp://jsfiddle.net/Kukkimonsuta/GaMD7/2/

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

编辑:我的最终解决方案基于Siva Charananonymous down voting is lame的答案

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");
}

解决方法

在所有浏览器中动态执行此操作的唯一方法是使用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;
    };

});

原文地址:https://www.jb51.cc/css/215787.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。