如何使用SVG在滚动条上绘制文字?

时间:2019-04-02 06:52:28

标签: html5 svg

我希望每当用户滚动我的网页时,都应绘制“ Live in Bliss”。我设法画了L,但不确定如何处理其他元素。我尝试使用多个svg,但未提供所需的输出。 以下是我的代码:-

// Get the id of the <path> element and the length of <path>
    var triangle = document.getElementById("triangle");
    var length = triangle.getTotalLength();
    
    // The start position of the drawing
    triangle.style.strokeDasharray = length;
    
    // Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw
    triangle.style.strokeDashoffset = length;
    
    // Find scroll percentage on scroll (using cross-browser properties), and offset dash same amount as percentage scrolled
    window.addEventListener("scroll", myFunction);
    
    function myFunction() {
    var scrollpercent = (document.body.scrollTop + document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);
    
      var draw = length * scrollpercent;
      
      // Reverse the drawing (when scrolling upwards)
      triangle.style.strokeDashoffset = length - draw;
    }
#mySVG {
      position: fixed;
      top: 20%;
      width: 400px;
      height: 210px;
      margin-left: 20%;
    }
    
    body {
  height: 2000px;
  background: #f1f1f1;
}
<html>
<body>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<h2>Scroll down this window to draw a triangle.</h2>
<p>Scroll back up to reverse the drawing.</p>


    <svg id="mySVG">
      <path fill="none" stroke="red" stroke-width="3" id="triangle" d="M75 20 L75 200 L160 200 0 Z" />
      Sorry, your browser does not support inline SVG.
    </svg>
    
</body>
</html>

谢谢

1 个答案:

答案 0 :(得分:0)

这是来自MDN:

Element.scrollTop属性获取或设置元素内容垂直滚动的像素数。

Element.scrollHeight只读属性是元素内容高度的度量,其中包括由于溢出而无法在屏幕上显示的内容。

对于没有CSS或内联布局框的元素,Element.clientHeight只读属性为零;否则,它是元素的内部高度(以像素为单位)。它包括填充,但不包括边框,边距和水平滚动条(如果有)。

我正在使用所有这些属性来计算路径的dashoffset,以便动画在滚动结束时结束。

我希望这是您所需要的。

// Get the id of the <path> element and the length of <path>
var triangle = document.getElementById("triangle");
var length = triangle.getTotalLength();
var dasharray = length;
var dashoffset = length;

// The start position of the drawing
triangle.style.strokeDasharray = dasharray;

// Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw
triangle.style.strokeDashoffset = dashoffset;



scrollingContent.addEventListener(
  "scroll",
  function(e) {

    dashoffset =
      length -
      e.target.scrollTop /
      ((e.target.scrollHeight - scrollingContent.clientHeight) / length);
    triangle.style.strokeDashoffset = dashoffset;
  },
  false
);
#mySVG {
  position: fixed;
  top: 5%;
  width: 400px;
  height: 210px;
  margin-left: 10%;
  border:1px solid;
  background:rgba(255,255,255,.85);
  
  pointer-events:none;
}

#scrollingContent{display:block;width:20em; max-height:100vh; overflow:scroll}
<svg id="mySVG">
  <path fill="none" stroke="red" stroke-width="3" id="triangle" d="M75 20 L75 200 L160 200" />
  Sorry, your browser does not support inline SVG.
</svg>
<p id="scrollingContent">
I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:-0
I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:-0
I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:-0
I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:I want that whenever a user scrolls my web page, "Live in Bliss" should be drawn. I have managed to draw L, but am not sure how can I go with other elements. I tried using multiple svgs, but it wasn't giving the desired output. Following is my code:-
</p>