【问题标题】:How to scroll DIV horizontally to center of container?如何将 DIV 水平滚动到容器中心?
【发布时间】:2017-08-11 16:02:49
【问题描述】:

我有一个带有 2 个 DIV 的容器。当我单击一个按钮时,我试图将黄色 DIV 在容器中水平居中。但是,如果我多次单击该按钮,它会来回滚动,如果我手动滚动容器然后单击该按钮,则黄色 DIV 永远不会滚动到视图中。

这是 jsfiddle。如果您一遍又一遍地单击该按钮,它将来回滚动。为什么这样做?: https://jsfiddle.net/nug4urna/

HTML:

<div id='container'>
<div id='blue'></div>
<div id='yellow'></div>
</div>
<div id='mybutton'>
click to scroll yellow div into view
</div>
<div id='log'>
</div>

JAVSCRIPT:

function scrollDivIntoView(id) {
      var elOffset = $(id).offset().left;
      var elWidth = $(id).width();
      var containerWidth = $('#container').width();
      var offset = elOffset - ((containerWidth - elWidth) / 2);

      $('#container').scrollLeft(offset);

      var _log = 'elOffset = ' + elOffset + '<br>';
      _log += 'elWidth = ' + elWidth + '<br>';
      _log += 'containerWidth = ' + containerWidth + '<br>';
      _log += 'offset = ' + offset;

      $('#log').html(_log);
}

$(document).ready(function() { 
    $('body').on('click', '#mybutton', function(){ 
        scrollDivIntoView('#yellow');
    }); 
});

CSS:

#container{
  width:100%;
  height:150px;
  border:1px solid red;
  display:inline-block;
  white-space:nowrap;
  overflow-y:hidden;
  overflow-x:auto;
}
#blue{
  width:2000px;
  height:100px;
  background-color: blue;
  margin:5px;
  display:inline-block;
}
#yellow{
  width:200px;
  height:100px;
  background-color: yellow;
  margin:5px;
  display:inline-block;
}
#mybutton{
  margin-top:10px; 
  cursor:pointer;
  background-color:black;
  color:white;
  width:400px;
  padding:4px;
  text-align:center;
}

【问题讨论】:

    标签: javascript jquery html css horizontal-scrolling


    【解决方案1】:

    如果您将第一行更改为var elOffset = $(id)[0].offsetLeft;,那么原始代码在每种情况下每次都有效。

    有关详细信息,请参阅以下内容: https://javascript.info/size-and-scroll

    【讨论】:

    • 我会接受这个作为答案,即使我很久以前就想出了一个“黑客”来让它工作。我的解决方案是在进行计算之前将容器 scrollLeft 设置回 0,所以我真的滚动了两次,它发生得太快了,你甚至看不到它。但你的解决方案更好。
    【解决方案2】:

    要使其居中,您需要在黄色容器中添加一个右边距以“填充”右侧并使其居中。我更新了你的小提琴: https://jsfiddle.net/nug4urna/2/

    #yellow{
      width:200px;
      height:100px;
      background-color: yellow;
      margin:5px;
      display:inline-block;
      margin-right: 100%;
    }
    

    它来回跳转的原因也是因为计算是基于var elOffset = $(id).offset().left;,每次点击后都会发生变化。我更新了小提琴以检查偏移量是否大于容器宽度。如果小于,我们会阻止更新。

      if (elOffset > containerWidth) {
        $('#container').scrollLeft(offset);
      }
    

    更新: 这是第一次工作,但如果用户滚动计算不是很正确。您应该查看https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth 以帮助计算滚动条实际可以移动多远。意思是当栏一直向右移动以显示所有内容时,数值与总宽度不同。

    【讨论】:

    • 好吧,你的jsfiddle有一个错误:if (elOffeset &gt; containerWidth) {应该是if (elOffset &gt; containerWidth) {。你是对的,它第一次工作,但肯定有办法让它工作,即使用户滚动?
    • 抱歉打错字了。更新了小提琴以使用全局来跟踪初始偏移量。 jsfiddle.net/nug4urna/4
    • 这对我不起作用......因为我的问题实际上比我在简单示例中显示的要多。我会将 div 附加到容器中,这样所有的数字都会改变。我没有将它添加到我的示例中,因为即使宽度发生变化,它仍然应该是相同的计算。
    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多