如何使用HTML设置进度条的最大值?


网站上的进度条用于表示已完成工作的百分比,例如从任何网站下载歌曲或电影的进度。您可以使用进度条来显示已下载和剩余的歌曲或电影数量。

在HTML中,我们可以使用<progress>元素在网站上显示进度条,以表示已完成和剩余的工作量。您可以通过使用元素本身定义属性来自行处理进度条的值。

现在让我们了解可以赋予<progress>元素的不同属性的用法。可以在progress标签内定义的属性列表如下所示并进行了解释:

  • value − value属性表示进度的当前值,即已完成的任务或工作的数量。

  • max − max属性用于设置进度条可以达到的最大值。您可以使用任何数字定义进度条可以达到的最大值。

  • position − position属性返回进度条的当前位置。

  • level − 此属性返回进度条的列表。

语法

下面的语法将向您展示如何在HTML文档中使用上面定义的属性以及progress元素:

<progress discussed attributes……> </progress>

注意 − 此元素不是自闭合标签,但这并不意味着您可以在其起始标签和结束标签之间传递任何类型的数值。如果您尝试在其内部传递任何值,它将忽略该值,并且不会将其显示在用户屏幕上。

让我们了解进度条元素的实际实现,并使用代码示例为其设置最大值。

示例

下面的示例将帮助您了解如何在HTML中为进度条设置最大值:

<!DOCTYPE html>
<html>
<body>
   <center>
      <h2>Set the maximum value of progress bar using HTML</h2>
      <p>The maximum value to the below bar is set using the max attribute and the current process completion value is set using the value attribute.</p>
      <p>The below progress bar shows the 50% completion of the process as the max value is set to 100 and the current value is set to 50. </p>
      <progress value = "50" max = "100"></progress>
   </center>
</body>
</html>

在上面的示例中,我们使用了带有value和max属性的progress元素。我们使用max属性设置进度条可以达到的最大值(在本例中为100),并使用value属性设置当前值(在本例中为50),这表示过程已完成50%。

让我们再看一个代码示例,以了解如何在HTML中为进度条设置最大值。

示例

此示例将说明progress元素的使用以及使用max属性设置其最大值。

<!DOCTYPE html>
<html>
<body>
   <center>
      <h2>Set the maximum value of progress bar using HTML</h2>
      <p>The maximum value to the below bar is set using the max attribute and the current process completion value is set using the value attribute.</p>
      <p>The below progress bar shows the 75% completion of the process as the max value is set to 100 and the current value is set to 75.</p>
      <progress value = "75" max = "100"></progress>
   </center>
</body>
</html>

在此代码示例中,我们将进度条的最大值设置为200,当前值设置为150,这使得进度条显示正在进行的过程已完成75%。

让我们再看一个代码示例,以深入了解使用max属性为进度条设置最大值的原理。

示例

下面的示例将显示如果max和value属性的值相同,进度条将如何显示过程的进度:

<!DOCTYPE html>
<html>
<body>
   <center>
      <h2>Setting the maximum value of progress bar using HTML</h2>
      <p>The maximum value to the below bar is set using the max attribute and the current process completion value is set using the value attribute.</p>
      <p>The below progress bar shows the 100% completion of the process as the max value is set to 100 and the current value is set to 100. </p>
      <progress value = "100" max = "100"></progress>
   </center>
</body>
</html>

在此示例中,我们使用了具有相同值的max属性和value属性,这将使progress元素显示过程已完全完成,并显示完整的蓝色进度条,因为过程已完成。

结论

在本文中,我们学习了如何在HTML中为progress元素设置最大值。我们借助三个不同的代码示例(为max属性赋予了不同的值,以便在每个代码示例中设置不同的最大值)详细讨论并理解了此过程。

更新于:2023年11月20日

浏览量:183

启动您的职业生涯

完成课程后获得认证

开始学习
广告
© . All rights reserved.