我一个div有横向滚动条,如何让他页面一加载就让他滚动到中间的位置

新手上路,请多包涵

我一个div有横向滚动条,如何让他页面一加载就让他滚动到中间的位置

阅读 2.9k
2 个回答

提供一个思路

CSS:

 <style>
   .index{
       width: 600px;
       height: 200px;
       border: 1px solid red;
       overflow-x: auto;
     }
     .top{
       width: 1200px;
       height: 100px;
       border:1px solid skyblue;
       text-align: center;
     }
 </style>

HTML:

 <div class="index" id="box1">
    <div class="top"  id="box2">123</div>
 </div>

JS:

      const box1 = document.getElementById("box1")
      const box2 = document.getElementById("box2")
      const b1w = box1.offsetWidth
      const b2w = box2.offsetWidth
      console.log((b2w - b1w)/2)
      box1.scrollTo((b2w - b1w)/2,0)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题