【问题标题】:Changing the margin of a child element on parent scroll makes child flicker in IE9更改父滚动上子元素的边距会使子元素在 IE9 中闪烁
【发布时间】:2014-03-14 16:14:38
【问题描述】:

我有一个元素要贴在其父容器的顶部。父容器是可滚动的,粘性元素的兄弟元素应该滚动。

<div class="parent">
    <div class="fixed">
        <p>
            I shouldn't scroll, you know
        </p>
    </div>
    <div class="scrollable">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
            <li>10</li>
            <li>11</li>
            <li>12</li>
            <li>13</li>
            <li>14</li>
            <li>15</li>
            <li>16</li>
            <li>17</li>
            <li>18</li>
            <li>19</li>
            <li>20</li>
        </ul>
    </div>
</div>

CSS:

.parent
{
    border:1px solid #ccc;
    margin:20px 0;
    max-height:300px;
    position:relative;
    overflow:auto;
}

.fixed, .scrollable
{
    display:inline-block;
    vertical-align:top;
}

.fixed
{
    background:rgba(0, 0, 0, 0.1);
    padding:10px;
    text-align:center;
    width:20rem;
}

.scrollable
{
    padding:10px;
}

当您滚动父元素时,粘性元素会保持原位,但在 IE9 中会闪烁。这是我的 jQuery 代码:

$(function(){
    $('.parent').scroll(function(e){
        $(this).find('.fixed').css('margin-top', $(this).scrollTop());
    });
});

这是jsfiddle。如何消除 IE9 中的闪烁?谢谢!

【问题讨论】:

    标签: jquery css


    【解决方案1】:

    使用 animate 方法而不是 css 方法:

    $(this).find('.fixed').animate({'margin-top': $(this).scrollTop()},1);
    

    但它可以像这样使用 css 来完成:

    .fixed{
    position: fixed;
    top: 0;
    left: 0;
    width: 350px;
    height: 200px;
    }
    

    【讨论】:

    • 嗨,@C-link 它实际上仍然在 IE9 中闪烁,并且也会使 chrome 和 firefox 闪烁。
    • 它应该只粘在可滚动的父容器的顶部,而不是整个文档
    • 然后你可以使用 position: sticky;但尚未实施。
    猜你喜欢
    • 2010-12-18
    • 2013-04-24
    • 2022-01-03
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多