javascript - 如何使用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>
谢谢你!
解决方案
尝试修改其中一种样式
html{ scroll-behavior: smooth; scroll-padding-top: 124px;}
推荐阅读
- python - 如何减少第一场比赛的延迟?Python
- php - 无法使用 php 更新 MySQL 中的数据有什么建议吗?
- r - 将函数列表传递给要评估的“微基准”
- kotlin - kotlin 类型与 shl 运算符不匹配
- jinja2 - jinja2 在 {for} 中使用 {if}
- r - 如何在不了解用户目录的情况下访问文件
- javascript - Angularjs 和 Angular 8 混合并执行代码微细化得到错误:无法解析 xxx-service 的所有参数
- google-cloud-platform - GCE 中共享 Docker 卷的等价物是什么?
- python - 在没有循环的python中重复应用函数
- javascript - 如何使用递归返回 N 个硬币翻转的所有组合?