【发布时间】:2017-04-28 00:22:26
【问题描述】:
在我目前正在构建的网站中,我有一个 SVG 路径,它基本上是一条直线。当用户向下滚动页面时,此路径会向下绘制页面,直到用户点击页面底部。
我用于此的代码取自此页面https://www.w3schools.com/howto/howto_js_scrolldrawing.asp
可以在这里看到:
<svg style="min-height: 113.5%;" version="1.1" id="line_2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="4px" height="113.5%" xml:space="preserve">
<path style="max-height: 113.5%;" id="blue_line" fill="freeze" stroke-width="4" stroke="#3b7fdc" d="M0 0 v2884 400" />
</svg>
<script>
// Get the id of the <path> element and the length of <path>
var triangle = document.getElementById("blue_line");
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;
}
这确实很好用,但是,目前,当用户向上滚动页面时,SVG 路径会“反转”并使用它来备份页面。我想要的效果是 svg 只绘制页面并且在用户向上滚动页面时不反转。
有谁知道如何编辑上面的脚本来实现这种期望的行为?
【问题讨论】:
-
当用户向下滚动一半,然后滚动回到顶部,然后再次开始向下滚动时,您希望发生什么?
-
在这种情况下,如果可能的话,我希望 svg 路径在页面中途停止,不与用户反转,然后在用户滚动超过初始停止点时继续向下绘制(在这种情况下,在页面的一半处)。
标签: javascript jquery css svg