提供一个思路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)
提供一个思路
CSS:
HTML:
JS: