简体   繁体   English

使用JavaScript在HTML中显示CSS属性的值

[英]Display the value of CSS property in HTML using JavaScript

I implemented a progress bar using HTML and CSS. 我使用HTML和CSS实现了一个进度条。 The value of the progress bar depends on the width of the inner class (in this code: bar-fill). 进度条的值取决于内部类的宽度(在此代码中:bar-fill)。 I want to access this width property in my styling and display it next to the progress bar. 我想在我的样式中访问此width属性并将其显示在进度条旁边。

Working Code: https://jsfiddle.net/nvarun123/h5xgfyrp/3/ 工作代码: https//jsfiddle.net/nvarun123/h5xgfyrp/3/

HTML code: HTML代码:

<div class="container">
<div class="bar">
<span class="bar-fill"></span>
</div>
</div>

CSS code: CSS代码:

.container{
  width:300px;
}
.bar{
  width:100%;
  background-color: #E3E3E3;
  border-radius:10px;
}

.bar-fill{
  height:15px;
  display:block;
  background:#0073CF;
  width:60%;
  border-radius:7.5px;
  -webkit-transition: width 0.8s ease;
  transition: width 0.8s ease;
}

▶ First, you need to add an HTML element that will show the progress: ▶首先,您需要添加一个显示进度的HTML元素:

<div id = "progress"></div>

▶ Then, you can use the following JavaScript code: ▶然后,您可以使用以下JavaScript代码:

var
    /* The elements */
    bar = document.getElementsByClassName("bar")[0],
    barFill = document.getElementsByClassName("bar-fill")[0],
    progress = document.getElementById("progress"),

    /* The bar's total width */
    barWidth = window.getComputedStyle(bar, null).getPropertyValue("width"),

    /* How much of the bar is filled */
    barFillWidth = window.getComputedStyle(barFill, null).getPropertyValue("width"),

    /* Create the percentage */
    pct = 100 * (parseFloat(barFillWidth) / parseFloat(barWidth)) + "%";

/* Set the innerHTML of our progress element */
progress.innerHTML = pct;

Check out this fiddle or the following snippet for a visual representation. 查看此小提琴或以下片段以获得直观表示。

Snippet: 片段:

 (function() { var bar = getByClass("bar"), barFill = getByClass("bar-fill"), progress = document.getElementById("progress"), barWidth = getWidth(bar), barFillWidth = getWidth(barFill), pct = 100 * barFillWidth / barWidth + "%"; progress.innerHTML = pct; function getWidth(element) { return parseFloat(window.getComputedStyle(element, null).getPropertyValue("width")); }; function getByClass(className) { return document.getElementsByClassName(className)[0]; } })(); 
 .container { display: inline-block; width: 300px; } .bar { width: 100%; background-color: #E3E3E3; border-radius: 10px; } .bar-fill { height: 15px; display: block; background: #0073CF; width: 60%; border-radius: 7.5px; -webkit-transition: width 0.8s ease; transition: width 0.8s ease; } #progress { position: relative; bottom: 3px; display: inline-block; } 
 <div class="container"> <div class="bar"> <span class="bar-fill"></span> </div> </div> <div id="progress"></div> 


To position the label next to the progressbar , there are a lot of things you can do. 要将标签放在进度条旁边,您可以执行许多操作。 I'm mentioning two of them below: 我在下面提到其中两个:

  1. You can put position: absolute to #progress to get it out of the normal flow, so that it can be 'inline' with .bar as the bar currently occupies 100% of the container's width. 您可以将position: absolute放到#progress以使其脱离正常流程,以便它可以与.bar 'inline' ,因为#progress当前占据容器宽度的100%。
  2. You can put #progress outside of the container as a sibling and put display: inline-block to both of them. 您可以将#progress放在#progress外部作为兄弟,并将display: inline-block放到它们中。

You can calculate the percent width of the .bar-fill by dividing it's width and the width of .bar 你可以计算的百分比宽度.bar-fill除以它的宽度和宽度.bar

var percent = ($('.bar-fill').width() / $('.bar').width()) * 100

See: https://jsfiddle.net/fve4qszo/ 请参阅: https//jsfiddle.net/fve4qszo/

Yes you can achieve what you are looking for, but there could be some trouble when you say: 是的,你可以实现你想要的,但是当你说:

and display it next to the progress bar. 并将其显示在进度条旁边。

Because if you display the percentage next to the progress bar, then the width of the progress bar is no longer a true 60%.. it goes down a few percentage points to allow you to display the percentage of the bar next to it. 因为如果您显示进度条旁边的百分比,则进度条的宽度不再是真正的 60%..它会下降几个百分点,以允许您显示其旁边的栏的百分比。 So, with that said please be careful and be mindful of what you are trying to accomplish or else this could become frustrating. 因此,请注意,请注意并注意您要完成的任务,否则这可能会令人沮丧。

Here is the formula that is used to get the width percentage: 以下是用于获取宽度百分比的公式:

var n = $('.bar-fill').width() / $('.bar-fill').parent().width() * 100;

Here is a JSfiddle . 这是一个JSfiddle

Let me know if this is what you are looking for. 如果这是您正在寻找的,请告诉我。

您可以使用获取宽度值

document.querySelector('.bar-fill').offsetWidth

First add the jquery script link in your html: 首先在html中添加jquery脚本链接:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<div class="container">
<div class="bar">
<span class="bar-fill"></span> 
</div>
</div>
Test

In your javascript ypu can get the width of bar-fill class by following: 在你的javascript中,ypu可以通过以下方式获得bar-fill类的宽度:

var width = $(".bar-fill").css('width');
$(".container").prepend(width);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM