如何在由JavaScript操作的进度条末尾添加SVG。

huangapple go评论58阅读模式
英文:

How to add svg end of a progress bar manipuleted by javascript

问题

I am coding a progress bar, and this progress is increasing or decreasing according to the degree from JavaScript. It's okay if the progress bar doesn't work, but I want to add a triangle SVG to the end of this bar and make it arrow-shaped.

The progress bar I made looks like this:
我的实现

but I want it to look like this:
我想要制作的样子

The HTML structure of the progress bar is as follows:

<div class="progress-container">
    <div class="progress__fill"></div>
</div>

Here is the progress bar JavaScript code:

<script>
    function updateProgressBar(progressBar) {
        progressBar.querySelector(".progress__fill").style.width = VALUE;
    }
    const myProgressBar = document.querySelector(".progress-container");
    updateProgressBar(myProgressBar);
</script>

As you can see, it changes according to the progress bar value variable, so I didn't know how to add the SVG in the form of a triangle at the end.

英文:

I am coding a porgress bar and this progress is increasing or decreasing according to the degree from javascript. It's okay if the progress bar doesn't work, but I want to add a triangle svg to the end of this bar and make it arrow shaped.

The progress bar I made looks like this:
my ipmpementation

but I want it to look like this:
how i want to make

The html structure of the progress bar is as follows:

&lt;div class=&quot;progress-container&quot;&gt;
     &lt;div class=&quot;progress__fill&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

Here is the progress bar javascript code:

&lt;script &gt;
    function updateProgressBar(progressBar) {
        progressBar.querySelector(&quot;.progress__fill&quot;).style.width = VALUE;
    }
const myProgressBar = document.querySelector(&quot;.progress-container&quot;);
updateProgressBar(myProgressBar); &lt;/script&gt;

As you can see, it changes according to the progress bar value variable, so I didn't know how to add the svg in the form of triangle at the end.

答案1

得分: 0

为了准确回答您的问题,请检查以下代码片段。现在您可以使用CSS伪类中的三角形来执行相同的操作。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
let VALUE = '50%';

function updateProgressBar(progressBar) {
  progressBar.querySelector(".progress__fill").style.width = VALUE;
}
const myProgressBar = document.querySelector(".progress-container");
updateProgressBar(myProgressBar);

<!-- 语言: lang-css -->
.progress-container {
  width: 100%;
  height: 24px;
  background-color: darkgray;
  border-radius: 10px;
}

.progress__fill {
  float: left;
  background-color: red;
  width: 25%;
  height: 100%;
  border-radius: 10px;
}

.progress-container svg {
  float: left;
  width: 48px;
  height: 48px;
  margin-top: -12px;
  margin-left: -10px;
}

<!-- 语言: lang-html -->
<br>
<div class="progress-container">
  <div class="progress__fill"></div>
  <svg id="svgTriangle" fill="#ff0000" viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg">
        <path d="M 0,48 V 0 l 48,24 z" />
    </svg>
</div>

<!-- 结束代码片段 -->

希望这能帮助您解决问题。

英文:

to answer exactly your question, check snippet.
Now you have another way to do that with css triangle in after pseudo class

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let VALUE = &#39;50%&#39;;

function updateProgressBar(progressBar) {
  progressBar.querySelector(&quot;.progress__fill&quot;).style.width = VALUE;
}
const myProgressBar = document.querySelector(&quot;.progress-container&quot;);
updateProgressBar(myProgressBar);

<!-- language: lang-css -->

.progress-container {
  width: 100%;
  height: 24px;
  background-color: darkgray;
  border-radius: 10px;
}

.progress__fill {
  float: left;
  background-color: red;
  width: 25%;
  height: 100%;
  border-radius: 10px;
}

.progress-container svg {
  float: left;
  width: 48px;
  height: 48px;
  margin-top: -12px;
  margin-left: -10px;
}

<!-- language: lang-html -->

&lt;br&gt;
&lt;div class=&quot;progress-container&quot;&gt;
  &lt;div class=&quot;progress__fill&quot;&gt;&lt;/div&gt;
  &lt;svg id=&quot;svgTriangle&quot; fill=&quot;#ff0000&quot; viewBox=&quot;0 0 48 48&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
        &lt;path d=&quot;M 0,48 V 0 l 48,24 z&quot; /&gt;
    &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 15:03:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定