将文本放在javascript形状中?

时间:2017-03-26 17:48:09

标签: javascript jquery html css

我想知道是否有人可以帮助我。我想在我的网站上使用下面的JS小提琴,但我想在心形中添加一些文字,有没有办法可以做到这一点?

https://jsfiddle.net/kimmobrunfeldt/dnLLgm5o/

HTML:

<div id="container">
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" viewBox="0 0 100 100">
                  <path fill-opacity="0" stroke-width="1" stroke="#bbb" d="M81.495,13.923c-11.368-5.261-26.234-0.311-31.489,11.032C44.74,13.612,29.879,8.657,18.511,13.923  C6.402,19.539,0.613,33.883,10.175,50.804c6.792,12.04,18.826,21.111,39.831,37.379c20.993-16.268,33.033-25.344,39.819-37.379  C99.387,33.883,93.598,19.539,81.495,13.923z"/>
                  <path id="heart-path" fill-opacity="0" stroke-width="3" stroke="#ED6A5A" d="M81.495,13.923c-11.368-5.261-26.234-0.311-31.489,11.032C44.74,13.612,29.879,8.657,18.511,13.923  C6.402,19.539,0.613,33.883,10.175,50.804c6.792,12.04,18.826,21.111,39.831,37.379c20.993-16.268,33.033-25.344,39.819-37.379  C99.387,33.883,93.598,19.539,81.495,13.923z"/>
              </svg>
</div>

使用Javascript:

// progressbar.js@1.0.0 version is used
// Docs: http://progressbarjs.readthedocs.org/en/1.0.0/

var bar = new ProgressBar.Path('#heart-path', {
  easing: 'easeInOut',
  duration: 1400
});

bar.set(0);
bar.animate(1.0);  // Number from 0.0 to 1.0

CSS:

#container {
  margin: 20px;
  width: 200px;
  height: 200px;
}

2 个答案:

答案 0 :(得分:1)

在svg下添加文本,将位置设置为绝对,将父位置设置为relative,并固定文本的位置。请参阅js小提琴:https://jsfiddle.net/dnLLgm5o/1738/

CSS:

#container {
  position: relative;
  margin: 20px;
  width: 200px;
  height: 200px;
}

#text {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 50px;
  left: 0;
  text-align:center;
}

HTML:

<div id="container">
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" viewBox="0 0 100 100">
                  <path fill-opacity="0" stroke-width="1" stroke="#bbb" d="M81.495,13.923c-11.368-5.261-26.234-0.311-31.489,11.032C44.74,13.612,29.879,8.657,18.511,13.923  C6.402,19.539,0.613,33.883,10.175,50.804c6.792,12.04,18.826,21.111,39.831,37.379c20.993-16.268,33.033-25.344,39.819-37.379  C99.387,33.883,93.598,19.539,81.495,13.923z"/>
                  <path id="heart-path" fill-opacity="0" stroke-width="3" stroke="#ED6A5A" d="M81.495,13.923c-11.368-5.261-26.234-0.311-31.489,11.032C44.74,13.612,29.879,8.657,18.511,13.923  C6.402,19.539,0.613,33.883,10.175,50.804c6.792,12.04,18.826,21.111,39.831,37.379c20.993-16.268,33.033-25.344,39.819-37.379  C99.387,33.883,93.598,19.539,81.495,13.923z"/>
  </svg>
  <p id="text">Loading...</p>
</div>

答案 1 :(得分:1)

您可以像这样使用svg文本

<text x="32" y="50" fill="red">Love</text>

Fiddle

x&amp; y是文本位置