te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Table scroll bar jumps up when table receives focus in IE - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Table scroll bar jumps up when table receives focus in IE - Stack Overflow

programmeradmin3浏览0评论

The issue: I've a table with a wrapping div with overflow-y : auto , once the table gets focus, the scroll bar jumps up. How can I prevent this?

I experience this behavior in IE9, not in Chrome.

Please note: I've added tabindex to the table so it can receive focus. And I focus on the table pragmatically upon a click on it.

jsFiddle: /

  1. Scroll down the table
  2. Click on other element on the page so table loss focus
  3. Click on the table to focus on it
  4. Scroll bar jumps up

HTML:

    <div>
        <table id="tabl" tabindex="1">
            <thead>
                <tr>
                    <th style="font-weight: bold">head</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>first</td>
                </tr>
                <tr>
                    <td>SEC</td>
                </tr>
                <tr>
                    <td>dsadfawdfadfa</td>
                </tr>
                <tr>
                    <td>dsadfawdfadfa</td>
                </tr>
.
.
.
                 <tr>
                    <td>dsadfawdfadfa</td>
                </tr>
            </tbody>
        </table>
    </div>

CSS:

table {
    table-layout: fixed;
    font-family: arial;
    font-size: 11px;
    text-align: left;
    outline: none;
    width: 625px;
}
div {
    overflow-y: auto;
    overflow-x: hidden;
    height: 150px;
    width: 300px
}

JS:

$('table').focusin(function (e) {
    console.log("table got focus - scroller jumps up");
}).click(function () {
    $('table').focus();
});

The issue: I've a table with a wrapping div with overflow-y : auto , once the table gets focus, the scroll bar jumps up. How can I prevent this?

I experience this behavior in IE9, not in Chrome.

Please note: I've added tabindex to the table so it can receive focus. And I focus on the table pragmatically upon a click on it.

jsFiddle: http://jsfiddle/msdevs/r6TzS/4/

  1. Scroll down the table
  2. Click on other element on the page so table loss focus
  3. Click on the table to focus on it
  4. Scroll bar jumps up

HTML:

    <div>
        <table id="tabl" tabindex="1">
            <thead>
                <tr>
                    <th style="font-weight: bold">head</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>first</td>
                </tr>
                <tr>
                    <td>SEC</td>
                </tr>
                <tr>
                    <td>dsadfawdfadfa</td>
                </tr>
                <tr>
                    <td>dsadfawdfadfa</td>
                </tr>
.
.
.
                 <tr>
                    <td>dsadfawdfadfa</td>
                </tr>
            </tbody>
        </table>
    </div>

CSS:

table {
    table-layout: fixed;
    font-family: arial;
    font-size: 11px;
    text-align: left;
    outline: none;
    width: 625px;
}
div {
    overflow-y: auto;
    overflow-x: hidden;
    height: 150px;
    width: 300px
}

JS:

$('table').focusin(function (e) {
    console.log("table got focus - scroller jumps up");
}).click(function () {
    $('table').focus();
});
Share Improve this question edited May 14, 2017 at 10:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 20, 2013 at 12:00 MichaelSMichaelS 7,10310 gold badges53 silver badges75 bronze badges 3
  • 1 If I was using a windows machine at the moment, I'd try by creating a handler for onblur (I think that's the opposite to onfocus) I would then get the current scroll pos of the table. That way, you can re-set the scroll pos when the table next gets focus. – enhzflep Commented Feb 22, 2013 at 13:42
  • 1 I want your nerd t-shirt NOW – Andrea Ligios Commented Feb 22, 2013 at 15:58
  • I was here. The problem is still there in IE 11. – Chris Commented Dec 18, 2014 at 14:01
Add a ment  | 

5 Answers 5

Reset to default 7 +50

This fixed it for me - keeping a record of the current scroll position on the wrapper, and re-instating it on blur.

http://jsfiddle/r6TzS/10/

$('#wrapper').scroll(function(){
    $(this).data( {posY: $(this).scrollTop()} )
})
.blur(function(){
    $(this).scrollTop( $(this).data("posY") );
})

The accepted answer for this question is actually not accurate. The main issue is Internet Explorer's implementation of focus(). In the accepted JSFiddle it never calls focus() so the original issue never happens. If you take out all of the javascript from that example the scrolling issue still does not occur. Instead of focus() you should call setActive() which sets the element as active, but does not try to scroll it into view. The setActive() method is described here - http://help.dottoro./ljqmdirr.php. Note that it is only supported by ie.

Here is a working example - http://jsfiddle/r6TzS/100/

$('table').focusin(function (e) {
    console.log("table got focus - scroller jumps up");
}).click(function () {
    $('table').setActive();
});

Update: By adding mouseleave and mouseenter I was able to get the scrollbar to work correctly on IE9 and Chrome v24.0.1312.57.

Working Example: http://jsfiddle/r6TzS/9/

var scrollPos = 0;
var ignoreScrollPos = 0;

$('div').mouseleave(function() {
    scrollPos = $('div').scrollTop(); 
    console.log("Scroll position set: " + scrollPos);        
    ignoreScrollPos = 0;
}).mouseenter(function() {
    ignoreScrollPos = 1;
});

$('table').focusin(function (e) {
    console.log("table got focus - scroller jumps up: " 
                + $('div').scrollTop()); 
}).click(function () {    
    if (!ignoreScrollPos) {
        console.log("Set position to: " + scrollPos);
        $('div').scrollTop(scrollPos);  
    }
});

This solution prevents the scrollbar from jumping up in IE9. However, now I find it does not work correctly in Chrome. Regardless, I thought it would be helpful to share this solution.

In IE, setting the focus on the <table/> tag visually resets the table, by setting the focus to the first element in the tag.

This can be easily demonstrated by explicitly setting the focus to the last element within the click event of <table/>. If you look closely, on clicking, the table actually scrolls to the top and then to the bottom very quickly.

This is because, the mere act of clicking on the table first sets the focus to the table (note that this is inherent behaviour, and not the result of your code), once that is done, the custom handler that you have defined receives the event and causes the td:nth-last-child(1), i.e., the last td to be set focus, thus scrolling the table to the bottom.

If you add a button that sets the focus, you will notice the difference.

Now that is only the why part of your question! I would be able to provide a more fitting how to fix answer if I knew of your actual requirement.

Have you tried to apply scroll-y option on table instead of DIV. Or you may try "tbody" tag and put all your TR in this. And apply scroll-y on this . I hope this will work to my knowledge. Let me know if it works otherwise I shall give you an other solution :)

发布评论

评论列表(0)

  1. 暂无评论