简体   繁体   English

从反转停止SVG滚动动画

[英]Stop SVG scroll animation from reversing

In the site that I am currently building I have an SVG path which is basically a straight line. 在我正在建立的网站中,我有一条SVG路径,基本上是一条直线。 When the user scrolls down the page, this path draws down the page until the user hits the bottom of the page. 当用户向下滚动页面时,此路径将向下绘制页面,直到用户点击页面底部。

The code I am using for this is taken from this page https://www.w3schools.com/howto/howto_js_scrolldrawing.asp 我正在使用的代码来自此页面https://www.w3schools.com/howto/howto_js_scrolldrawing.asp

and can be seen here: 在这里可以看到:

       <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;
}

This works really well, however, currently, when the user scrolls back up the page, the SVG path 'reverses' back up the page with it. 这非常有效,但是,目前,当用户向上滚动页面时,SVG路径会“反转”使用它备份页面。 My desired effect is for the svg to only draw down the page and to not reverse when the user scrolls back up the page. 我想要的效果是让svg只绘制页面,并在用户向上滚动页面时不反转。

Does anyone know how to edit the script above to achieve this desired behaviour? 有谁知道如何编辑上面的脚本来实现这个期望的行为?

I've made a slight change to the script and it now does what you need... 我对脚本略有改动,它现在可以满足您的需求......

<script>
// 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);
var lastScrollpercent = 0;
function myFunction() {
var scrollpercent = (document.body.scrollTop + document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);

  if (scrollpercent < lastScrollpercent) return;
  lastScrollpercent = scrollpercent;

  var draw = length * scrollpercent;

  // Reverse the drawing (when scrolling upwards)
  triangle.style.strokeDashoffset = length - draw;
}
</script>

To summarise, I added lastScrollpercent and set it to 0. When the page is scrolled it works out the current percentage that it's been scrolled and only draws anything if that is currently more than lastScrollpercent , which it then updates (it's the value of scrollpercent last time anything was drawn). 总而言之,我添加了lastScrollpercent并将其设置为0.当页面滚动时,它lastScrollpercent滚动的当前百分比,并且只有当前当前超过lastScrollpercent才会绘制任何内容,然后它会更新(这是最后一次scrollpercent的值)什么都画了)。 If the current scroll percent is less then it just returns (does nothing). 如果当前滚动百分比小于那么它只返回(什么都不做)。 That way, if you scroll up then it stops drawing, and if you then scroll down again it will only continue from where it left off. 这样,如果向上滚动则会停止绘制,如果再向下滚动,则只会从停止的位置继续向下滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM