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

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

提供一个思路

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...