首页 > 解决方案 > 如何使用javascript在点击时设置id的位置?

问题描述

我有多个带有“id”的段落,点击时工作正常,但我想在点击左侧菜单后设置 para 的位置。

我的代码:-

html{ scroll-behavior: smooth;
}
  .header{ background:#ccc; padding:50px; text-align:center; position:sticky; top:0px; left:0px; width:100%; z-index:1000;}
  .box{margin-bottom:25px;}
  .list{ position:fixed; top:150px; left:0px;}
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  
  
  <div class="header">Header</div>
<div class="container">
 <div class="row">

 <div class="col-4">
 <ul class="list">
 <li><a href="#one">one</a></li>
 <li><a href="#two">two</a></li>
 <li><a href="#three">three</a></li>
 <li><a href="#four">four</a></li>
 <li><a href="#five">five</a></li>
 </ul>
 </div>
 <div class="col-8">
 
 <div class="box">
 <h2 id="one">one</h2>
 <p>
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 </p>
 </div>
 
 <div class="box">
 <h2 id="two">two</h2>
 <p>
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 </p>
 </div>
 
  <div class="box">
 <h2 id="three">three</h2>
 <p>
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 </p>
 </div> 
 
   <div class="box">
 <h2 id="four">four</h2>
 <p>
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 </p>
 </div> 
 
    <div class="box">
 <h2 id="five">five</h2>
 <p>
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 </p>
 </div> 
 
 </div>
 </div>
</div>

谢谢你!

标签: javascripthtmlcss

解决方案


尝试修改其中一种样式

html{ scroll-behavior: smooth; scroll-padding-top: 124px;}

推荐阅读