Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

提供一个思路

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...