jquery - scrolltop 不适用于 Bootstrap 网格

标签 jquery html css twitter-bootstrap

我正在尝试使用以下代码滚动到 div

 $('html,body').animate({scrollTop:$("#hot").offset().top}, 500);

这工作正常,但当我将它与 Bootstrap 网格一起使用时,它只是滚动一点点。

下面是我正在测试的示例 html 和 css

.mybox {
  color: white;
  background: black;
  min-height: 300px;
}

..

      <div class="row">

        <h1 class="center" id="popular"> POPULAR DEALS </h1>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>

        <h1 id="hot" class='center'> HOT DEALS </h1>

        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
        <div class="mybox col-lg-4 col-xs-3">test</div>
      </div>
    </div>
    <div class="scrollhere">

    </div>

这是演示 - https://jsfiddle.net/4nymvo3a/

最佳答案

它不能正常工作,因为 Bootstrap 网格使用 float 。因此,#hot 的偏移量位于网格的顶部,即使它出现在第一组网格项的下方。要修复偏移量,您可以添加:

#hot
{
    clear: both;
}

到您的 CSS。这会清除 float 并正确计算 #hot 的偏移量,同时不会影响显示。这是一个演示:https://jsfiddle.net/t22sxxk6/ .

关于jquery - scrolltop 不适用于 Bootstrap 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070398/

相关文章:

javascript - 8 个缩略图链接到 8 个不同的画廊,但只编程一次

javascript - 如何使用 JQuery 滚动到 ng-repeat Angular JS 生成的 div 的底部?

css - 文本脱离 flexbox 容器

javascript - 使用 JS/jQuery 根据子 div 的内容使 div 不可见

javascript - 变量范围澄清

html - 表中的不同宽度 `th` & `td`

javascript - 使用媒体源扩展 API 时的视频 'Stuck'

javascript - 无法使用 JavaScript 显示 "block"

javascript - 使用 vw 和 vh 度量调整窗口大小的问题

javascript - 如何用js添加表格?