【问题标题】:Animate text on scroll svg, can't active both two shapes at the same time?滚动 svg 上的动画文本,不能同时激活两个形状?
【发布时间】:2021-04-27 17:34:24
【问题描述】:

我在滚动条上制作了这个动画文本,我想要一个心形和一个三角形。 首先我不能把它变成一个三角形。 其次,我可以让两种形状同时工作。当我删除其中一个 javascript 部分时,它可以工作。

把它做成了一个代码笔。希望有人可以帮助我。谢谢!

这是我到https://codepen.io/pen/bGwyKpM的链接。

否则也在这里:

HTML

<!-- Heart -->

  <div class="container1">
      <svg id="heart" data-name="heart" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">

          <path class="shape" id="shape" d="M-400 32 v-150 h200 a100,100 90 0,1 0,200 a100,100 90 0,1 -200,0 z" transform="rotate(225, 0, 0)"/>

          <text font-family="arial">
              <textPath xlink:href="#shape" id="textPath"> THE BLISS OF ADOPTION  • THE BLISS OF ADOPTION  • THE BLISS OF ADOPTION  • THE BLISS OF ADOPTION  • THE BLISS OF ADOPTION  • </textPath>
          </text>
      </svg>
  </div>
  <!-- Triangle -->
  <div class="container2">
      <svg id="hearttwo" data-name="hearttwo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">

          <path class="shape1" id="shape1" d="M-400 32 v-150 h200 a100,100 90 0,1 0,200 a100,100 90 0,1 -200,0 z" transform="rotate(225, 0, 0)"/>

          <text font-family="arial">
              <textPath xlink:href="#shape1" id="textPathB"> THE ULTIMATE FAIRYTALE • THE ULTIMATE FAIRYTALE • THE ULTIMATE FAIRYTALE • THE ULTIMATE FAIRYTALE • THE ULTIMATE FAIRYTALE • </textPath>
          </text>
      </svg>
  </div>

CSS


/* heart shape */
.shape {
  fill: none;
  /* stroke: white; */
}

#heart {
  width: 90vmin;
  height: auto;
  object-fit: contain;
  /* overflow: hidden; */
}

#textPath {
  /* The font-size is really hacky, sorry :( */
  font-family: 'Till-Normal';
  --sizefactor: 1.112;
  font-size: calc(1em * var(--sizefactor));
  letter-spacing: calc(1em * var(--sizefactor));
  fill: var(--purple-full);
  font-weight: 800
}

.container1 {
  width: 100%;
  height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  /* background-color: palevioletred; */
}

/* 2 heart shape */
.shape1 {
  fill: none;
  /* stroke: white; */
}

#hearttwo {
  width: 90vmin;
  height: auto;
  object-fit: contain;
  /* overflow: hidden; */
}

#textPathB {
  /* The font-size is really hacky, sorry :( */
  font-family: 'Till-Normal';
  --sizefactor: 1.112;
  font-size: calc(1em * var(--sizefactor));
  letter-spacing: calc(1em * var(--sizefactor));
  fill: var(--purple-full);
  font-weight: 800
}

.container2 {
  width: 100%;
  height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  /* background-color: palevioletred; */
}

Java


const textPath = document.querySelector("#textPath");

const a = document.documentElement,
  b = document.body,
  st = "scrollTop",
  sh = "scrollHeight",
  startOffset = 0;

const offsetOnScroll = (percent, startOffset) =>
  textPath.setAttribute("startOffset", percent * 10 + startOffset);

textPath.setAttribute("startOffset", startOffset);

document.addEventListener("scroll", (event) => {
  let percent = ((a[st] || b[st]) / ((a[sh] || b[sh]) - a.clientHeight)) * 100;
  window.requestAnimationFrame(() => offsetOnScroll(percent * -1, startOffset));
});


/* number two; */
const textPathB = document.querySelector("#textPathB");

const a = document.documentElement,
  b = document.body,
  st = "scrollTop",
  sh = "scrollHeight",
  startOffset = 0;

const offsetOnScroll = (percent, startOffset) =>
  textPathB.setAttribute("startOffset", percent * 10 + startOffset);

textPathB.setAttribute("startOffset", startOffset);

document.addEventListener("scroll", (event) => {
  let percent = ((a[st] || b[st]) / ((a[sh] || b[sh]) - a.clientHeight)) * 100;
  window.requestAnimationFrame(() => offsetOnScroll(percent * -1, startOffset));
});


【问题讨论】:

    标签: javascript css svg scroll svg-animate


    【解决方案1】:

    您重复了变量的名称

    把二号改成这样:

    /* number two; */
    const textPathB = document.querySelector("#textPathB");
    const aB = document.documentElement,
        bB = document.body,
        stB = "scrollTop",
        shB = "scrollHeight",
        startOffsetB = 0;
    const offsetOnScrollB = (percent, startOffsetB) =>
        textPathB.setAttribute("startOffset", percent * 10 + startOffset);
        textPathB.setAttribute("startOffset", startOffset);
    document.addEventListener("scroll", (event) => {
        let percent = ((aB[stB] || bB[stB]) / ((aB[shB] || bB[shB]) - aB.clientHeight)) * 100;
    window.requestAnimationFrame(() => offsetOnScrollB(percent * -1, startOffset));
    });
    

    【讨论】:

    • 感谢您的帮助!很好。您是否也知道如何将路径 d 更改为三角形而不是心形?
    • 这是 SVG 中三角形的路径:"M150 0 L75 200 L225 200 Z" 但根据您的 css,我认为您需要对 css 进行一些修改,尤其是高度跨度>
    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多