html - 谷歌浏览器滚动溢出错误

标签 html google-chrome select scroll overflow

这是一个奇怪的错误,很难弄清楚。

我们创建了一个 map 应用程序,弹出窗口中有一个 select 元素。它设置了 size 属性。 select 元素的父元素具有 overflow:auto 样式。当出现滚动时,尝试在选择元素上选择某些内容会向下滚动主体。但是 body 有 overflow:hidden 风格,它会杀了我们。

这是我创建的两个 fiddle :

http://jsfiddle.net/e6BK3/1/

http://jsfiddle.net/e6BK3/8/

Try to select first option on select element when its not focused on google chrome.然后看到它滚动所有可用的父元素进行滚动。

谷歌浏览器版本:34.0.1847.131

最佳答案

我想我做到了! :)

重点是防止在 option 标签上的任何其他传播 mousedown 事件,并手动管理 select 框的焦点。

参见 jsFiddle

我尽量说清楚了:

$('option')
.on('mousedown', function(e){
    // Prevent any other propagations
    e.stopImmediatePropagation();

    // Focus on the select DOM element
    // but saving the scrollTop of each parent before
    $(this)
    .parents()
    .filter(function(){
        // filter only those which have a scroll visible
        return $(this)[0].scrollHeight > $(this)[0].clientHeight;
    })
    .each(function(_,elt){
        // and for each, save the scroll value in data set
        $(elt).data('saveScroll', $(elt).scrollTop());
    })

    // Then trigger the focus option of the select DOM element
    // And finally the custom 'scrollback' event
    $(this).parent('select')
    .trigger('focus')
    .trigger('scrollback');

});

$('select')
// This customized event is for getting back the value of the scroll of all parents
// only if this value exists
.on('scrollback'
    , function(e){
        $(this)
        .parents()
        .filter(function(){
            // filter only those which have this data in data-set
            return 0 === $(this).data('saveScroll') || ~~$(this).data('saveScroll');
        })
        .each(function(_,elt){
            // and for each, putting back the right scroll value
            $(elt).scrollTop($(elt).data('saveScroll'));
            $(elt).removeData('saveScroll');
        })
    });

这可能并不完美,因为它是 hack。

至少它甚至可以与 multiple 类型的 select 一起使用。而且它是跨浏览器兼容的。

关于html - 谷歌浏览器滚动溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23566661/

相关文章:

winforms - 是否可以在datagrid中仅选择一行?

mysql - "select like"找到文本行

javascript - 如何进行缩放以适合我的情况?

jquery - 如何禁用jquery列表下img标签的点击?

css - 如何在 css 和 html 中构建悬停,当悬停在彩色背景上时会出现图像?

google-chrome - Three.js完全卡住Chrome,GLTF模型中的巨大纹理

html - Opera 和 Chrome 为 TD 元素添加高度

google-chrome - Chrome 开发工具 - 无法再单击元素?

javascript - 与 Bootstrap 下拉菜单链接时如何使用 Javascript if 语句

PostgreSql 选择查询操作