javascript - 自动递增左侧的编号列

标签 javascript html css

首先,请原谅我,因为我是 HTML、CSS 和 JS 的新手。

我想要一列数字,例如尺子或卷尺,模板,我可以将其粘贴在任何页面的左侧,并且在我粘贴它的任何页面中看起来都一样。我不想使用图形,因为页面可能短一厘米或几英里长,所以我想要一个随着页面长度的增加而增加长度的“标尺”。

我只是想将该编号列添加到非常基本的静态“hello-world”类型 html 页面中。

<html>
<head></head>
<body>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
</body>

</html>

我想要这样的东西:

Example of what I'm trying to achieve

When the page grows longer, the ruler grows along with it

Example of the ruler on a more complex page

我尝试使用谷歌搜索解决方案,使用 CSS JS 编号列、CSS JS 编号网格(最终向我展示了用于设计目的的 CSS 网格)、CSS JS 自动编号列左侧,但答案总是最终变得比我想象的更复杂,添加表格和编号的元素等等。

请问我如何使用 html、css 和 js 实现?有可能吗?

谢谢!

编辑: 最终解决方案(谢谢,justDan!)。

<html>
<head>
<style>
html, body {
  /* height: 100%; */
}

body {
  margin: 0;
  padding: 0 0 0 50px;
  position: relative;
}

p {
  margin: 0;
}

#ruler {
  background: #4c4c4c;
  height: 100%;
  left: 0;
  overflow: hidden;
  padding: 5px 0;
  position: absolute;
  top: 0;
}

#ruler p {
  color: #ccc;
  font-family: arial;
  font-weight: bold;
  margin: 0 0 15px 0;
  text-align: center;
}
</style>
</head>
<body>
  <div id='ruler'></div>

<div id='content'>
<p>Plenty of text here.1</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.2</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.3</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.4</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.5</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.6</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.7</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.8</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.1</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.2</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.3</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.4</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.5</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.6</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.7</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.8</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
<p>Plenty of text here.</p>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script type="text/javascript">
let height = document.querySelector('#content').offsetHeight,
    ruler  = document.querySelector('#ruler'),
    count  = 1;

for (let i = 0; i < height; i++) {
  let num = document.createElement('p');
      num.textContent = count;

  ruler.appendChild(num);

  count++;
}
</script>

</body>

</html>

最佳答案

我不能肯定地说,但根据你发布的你想要实现的图片,看起来你正在寻找文本编辑器使用的东西。如果是这种情况,您可以使用一点 js 来获取它。试试下面的代码。

编辑:更新了 fiddle 和片段。

fiddle :https://jsfiddle.net/m8xteugf/4/

let height = document.querySelector('#content').offsetHeight,
    ruler  = document.querySelector('#ruler'),
    count  = 1;

for (let i = 0; i < height; i++) {
  let num = document.createElement('p');
      num.textContent = count;
      
  ruler.appendChild(num);
  
  count++;
}
body {
  margin: 0;
  padding: 0 0 0 50px;
  position: relative;
}

p {
  margin: 0;
}

#ruler {
  background: #4c4c4c;
  height: 100%;
  left: 0;
  overflow: hidden;
  padding: 5px 0;
  position: absolute;
  top: 0;
}

#ruler p {
  color: #ccc;
  font-family: arial;
  font-weight: bold;
  margin: 0 0 15px 0;
  text-align: center;
}
<div id='ruler'></div>

<div id='content'>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque. Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac placerat dolor lectus quis orci. Phasellus consectetuer vestibulum elit. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Vestibulum fringilla pede sit amet augue. In turpis. Pellentesque posuere. Praesent turpis. Aenean posuere, tortor sed cursus feugiat, nunc augue blandit nunc, eu sollicitudin urna dolor sagittis lacus. Donec elit libero, sodales nec, volutpat a, suscipit non, turpis. Nullam sagittis. Suspendisse pulvinar, augue ac venenatis condimentum, sem libero volutpat nibh, nec pellentesque velit pede quis nunc. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce id purus. Ut varius tincidunt libero. Phasellus dolor. Maecenas vestibulum mollis diam. Pellentesque ut neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In dui magna, posuere eget, vestibulum et, tempor auctor, justo. In ac felis quis tortor malesuada pretium. Pellentesque auctor neque nec urna. Proin sapien ipsum, porta a, auctor quis, euismod ut, mi. Aenean viverra rhoncus pede. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut non enim eleifend felis pretium feugiat. Vivamus quis mi. Phasellus a est. Phasellus magna. In hac habitasse platea dictumst. Curabitur at lacus ac velit ornare lobortis. Curabitur a felis in nunc fringilla tristique. Morbi mattis ullamcorper velit. Phasellus gravida semper nisi. Nullam vel sem. Pellentesque libero tortor, tincidunt et, tincidunt eget, semper nec, quam. Sed hendrerit. Morbi ac felis. Nunc egestas, augue at pellentesque laoreet, felis eros vehicula leo, at malesuada velit leo quis pede. Donec interdum, metus et hendrerit aliquet, dolor diam sagittis ligula, eget egestas libero turpis vel mi. Nunc nulla. Fusce risus nisl, viverra et, tempor et, pretium in, sapien. Donec venenatis vulputate lorem. Morbi nec metus. Phasellus blandit leo ut odio. Maecenas ullamcorper, dui et placerat feugiat, eros pede varius nisi, condimentum viverra felis nunc et lorem. Sed magna purus, fermentum eu, tincidunt eu, varius ut, felis. In auctor lobortis lacus. Quisque libero metus, condimentum nec, tempor a, commodo mollis, magna. Vestibulum ullamcorper mauris at ligula. Fusce fermentum. Nullam cursus lacinia erat. Praesent blandit laoreet nibh. Fusce convallis metus id felis luctus adipiscing. Pellentesque egestas, neque sit amet convallis pulvinar, justo nulla eleifend augue, ac auctor orci leo non est. Quisque id mi. Ut tincidunt tincidunt erat. Etiam feugiat lorem non metus. Vestibulum dapibus nunc ac augue. Curabitur vestibulum aliquam leo. Praesent egestas neque eu enim. In hac habitasse platea dictumst. Fusce a quam. Etiam ut purus mattis mauris sodales aliquam. Curabitur nisi. Quisque malesuada placerat nisl. Nam ipsum risus, rutrum vitae, vestibulum eu, molestie vel, lacus. Sed augue ipsum, egestas nec, vestibulum et, malesuada adipiscing, dui. Vestibulum facilisis, purus nec pulvinar iaculis, ligula mi congue nunc, vitae euismod ligula urna in dolor. Mauris sollicitudin fermentum libero. Praesent nonummy mi in odio. Nunc interdum lacus sit amet orci. Vestibulum rutrum, mi nec elementum vehicula, eros quam gravida nisl, id fringilla neque ante vel mi. Morbi mollis tellus ac sapien. Phasellus volutpat, metus eget egestas mollis, lacus lacus blandit dui, id egestas quam mauris ut lacus. Fusce vel dui. Sed in libero ut nibh placerat accumsan. Proin faucibus arcu quis ante. In consectetuer turpis ut velit. Nulla sit amet est. Praesent metus tellus, elementum eu, semper a, adipiscing nec, purus. Cras risus ipsum, faucibus ut, ullamcorper id, varius ac, leo. Suspendisse feugiat. Suspendisse enim turpis, dictum sed, iaculis a, condimentum nec, nisi. Praesent nec nisl a purus blandit viverra. Praesent ac massa at ligula laoreet iaculis. Nulla neque dolor, sagittis eget, iaculis quis, molestie non, velit. Mauris turpis nunc, blandit et, volutpat molestie, porta ut, ligula. Fusce pharetra convallis urna. Quisque ut nisi. Donec mi odio, faucibus at, scelerisque quis</p> 
</div>

关于javascript - 自动递增左侧的编号列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58541598/

相关文章:

html - 如何让mat-chips checked unchecked?

javascript - 如何使用 Angular 或 JavaScript 连接两个具有相同 id 的数组

javascript - tinymce如何映射所见即所得的html代码和存储到数据库的textarea的输出?

javascript - 是否可以在循环中初始化多个 Javascript 数组?

javascript - 基于哪个onclick,如何使用JS使用 'srcset'更改img src?

javascript - 在表行上显示 div onmouseover,并使用行属性动态设置文本和 URL

javascript - 如何更改按类迭代的id值

html - 使用 html/css 设置滚动步长/跳跃/捕捉距离

html - 弃用的标签和属性?

css - Ruby on Rails Assets 管道似乎在添加不存在的样式