如何通过JavaScript将原点移动到一个合理的位置?

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

How to make the origin move to a reasonable position through javascript?

问题

我目前面临一个问题。我希望当进度值为10时,能够运行到第一个段落 "progress-bar-1" 的最后位置,当值为20时,能够运行到第二个段落 "progress-bar-2" 的最后位置。但我不知道如何合理地实现,因为这三行之间有一些间隙,阻止了他达到我想要的位置。我希望能在这里得到大家的帮助,谢谢。

// 通过点获取指定的位置值
var progress = 10;
if (progress == 10) {
    progress--;
} else if (progress == 20) {
    progress--;
} else if (progress == 30) {
    progress--;
}
// 计算点原点应该出现的位置
var dot = document.getElementById("dot");
var dotPosition = (progress / 30) * 100;
dot.style.left = `calc(${dotPosition}% - 20px)`;
body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

#progress-bar-container {
  width: 600px;
  height: 5px;
  background-color: #fff;
  position: relative;
  display: flex;
  flex-wrap: nowrap;
}

.progress-bar {
  width: 33.33%;
  height: 10px;
  background-color: #fff;
  margin right: 8px;
  border-radius: 20px;
}

#progress-bar-1 {
  background-color: #ddd;
}

#progress-bar-2 {
  background-color: #ddd;
}

#progress-bar-3 {
  background-color: #ddd;
  margin-right: 0;
}

#dot {
  width: 20px;
  height: 10px;
  border-radius: 5px;
  background-color: orange;
  position: absolute;
  top: 0px;
  left: 0;
  transition: left 0.2s ease-out;
  overflow: hidden;
}
<div id="progress-bar-container">
  <div class="progress-bar" id="progress-bar-1"></div>
  <div class="progress-bar" id="progress-bar-2"></div>
  <div class="progress-bar" id="progress-bar-3"></div>
  <div id="dot"></div>
</div>

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

英文:

I am currently facing a problem. I hope that when the progress value is 10, I can run to the last position of the first segment "progress-bar-1", and when the value is 20, I can run to the last position of the second segment progress-bar-2. But I don't know how to do it reasonably, because there are some gaps in the middle of these three lines, which prevents him from running to the position I want. I hope I can get everyone's help here, thank you.

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

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

// The specified position value obtained by dot
var progress = 10;
if(progress == 10){
    progress--
}else if(progress == 20){
    progress-- 
}else if(progress == 30){
    progress--
}
//Calculate where the dot origin should appea
var dot = document.getElementById(&quot;dot&quot;);
var dotPosition = (progress / 30) * 100;
dot.style.left = `calc(${dotPosition}% - 20px)`;

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

body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

#progress-bar-container {
  width: 600px;
  height: 5px;
  background-color: #fff;
  position: relative;
  display: flex;
  flex-wrap: nowrap;
}

.progress-bar {
  width: 33.33%;
  height: 10px;
  background-color: #fff;
  margin-right: 8px;
  border-radius: 20px;
}

#progress-bar-1 {
  background-color: #ddd;
}

#progress-bar-2 {
  background-color: #ddd;
}

#progress-bar-3 {
  background-color: #ddd;
  margin-right: 0;
}

#dot {
  width: 20px;
  height: 10px;
  border-radius: 5px;
  background-color: orange;
  position: absolute;
  top: 0px;
  left: 0;
  transition: left 0.2s ease-out;
  overflow: hidden;
}

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

&lt;div id=&quot;progress-bar-container&quot;&gt;
  &lt;div class=&quot;progress-bar&quot; id=&quot;progress-bar-1&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;progress-bar&quot; id=&quot;progress-bar-2&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;progress-bar&quot; id=&quot;progress-bar-3&quot;&gt;&lt;/div&gt;
  &lt;div id=&quot;dot&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->
如何通过JavaScript将原点移动到一个合理的位置?

答案1

得分: 1

我猜你应该将点的位置相对于使用element.getBoundingClientRect()获取的屏幕上给定进度条的绝对位置进行定位。因此,我想我会将点的位置映射到与百分比相关的进度条的位置。例如:如果值大于33.3%(大于10),但小于66.6%(小于20),则点的位置应该位于第二个进度条的起始位置坐标和这些相同坐标加上进度条的宽度之间的绝对位置。

希望这个简单的解释有用。

英文:

I guess you should give the position to the dot relative to the absolute position of the given progress bar in the screen taken with element.getBoundingClientRect().
So, I imagine I would map the position of the dot to the positions of the progress bars related to a %. For example: if the value is bigger than 33,3% (more than 10) but lesser than 66.6% (less than 20), the position of the dot shoud be in an absolute position between the starting position coordinates of the second progress bar and these same coordinates plus the width of the progress bar.
I hope this raw explanation could be useful.

huangapple
  • 本文由 发表于 2023年2月10日 17:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408891.html
匿名

发表评论

匿名网友

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

确定