【问题标题】:javascript animate border of a card to show progressjavascript动画卡片的边框以显示进度
【发布时间】:2017-11-30 16:38:36
【问题描述】:

您好,我想使用矩形卡片的周长作为进度条来创建类似卡片的元素(如离子)。 我知道使用 svg 路径我可以使用自定义形状为进度条设置动画。

有没有办法让路径响应卡片的宽度和高度? (欢迎使用 svg 或非 svg 解决方案)

谢谢!

【问题讨论】:

标签: javascript typescript svg ionic-framework progress-bar


【解决方案1】:

这可以工作:

let prog = 0;

setInterval(function(){ 
  if (prog > 100) {
    return;
  }
  let loader = document.getElementById('loader');
  loader.style.height = "" + prog + "%";
  prog += 1;
}, 100);
.card {
  position: relative;
  float: left;
  background-color: lightgrey;
  width: 100px;
  height: 100px;
  z-index: 0;
}

#loader {
  position: relative;
  background-color: orange;
  height: 0px;
  width: 3px;
  float: right;
  z-index: 1;
}
<div class="card">
  <div id="loader">
  </div>
</div>

编辑1:

您可以像这样生成覆盖给定 DOM 元素 (rectangular) 的 SVG:

let cardRect = document.getElementById('card').getBoundingClientRect();
let svgRect = document.getElementById('rect');
svgRect.parentElement.setAttribute("height", cardRect.height);
svgRect.parentElement.setAttribute("width", cardRect.width);
svgRect.setAttribute("height", cardRect.height);
svgRect.setAttribute("width", cardRect.width);
#card {
  position: relative;
  float: left;
  background-color: lightgrey;
  width: 100px;
  height: 100px;
  z-index: 0;
}
<div id="card">
  <svg>
    <rect id="rect" style="fill:none;stroke-width:3;stroke:orangered"/>
  </svg>
</div>

然后你可以使用类似Progressbar.js的东西来动画它

编辑2:
如果您出于某种原因需要 svg 作为路径元素 (而不是 rect 元素) this code 可以工作。 (它必须在 JSfiddle 上,由于某种原因它不适用于 SO)

【讨论】:

  • 感谢您的回复,但这只会为一个边框设置动画。我可以分别为 4 个边框设置动画并一起控制它们,但我虽然使用 svg 路径可能更优雅?
  • 哦,我以为你的意思是你不想使用 svg。
  • @HanChe所以你想使用SVG,但是你想根据某个DOM元素的形状生成自定义的SVG路径?
  • jep,我的意思是它将是一张矩形卡片,但我需要的是 svg 路径在移动视图中响应,卡片的宽度和高度因设备而异
  • @HanChe 我已经改变了我的答案。如果这没有帮助,那么恐怕这超出了我的知识范围。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 2015-02-13
  • 2018-02-28
  • 2013-04-08
  • 2021-02-07
相关资源
最近更新 更多