Friday 30 October 2020

How to get dashed line svg animation on accordingly scroll?

enter image description here

Whenever i apply on scroll animation on dashed line svg than it converted into a simple line without dashed.

I want same animation which is currently working but line would be dashed-line as shown as Svg Before animation in below example.

// Get the id of the <path> element and the length of <path>
var triangle = document.getElementById("dashed-path");
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;
}
.height-div{
        height: 500px; width: 100%; background:#eeeeee; 
    }
    .desktop-pattern-wrap{display: inline-block;vertical-align: top;width: 100%;}
    .desktop-pattern-wrap > div{float: left;width: 50%;}
<div class="desktop-pattern-wrap">
        <div class="desktop-pattern">
            <h2>Svg after animation</h2>
            <svg width="198px" height="1458px" viewBox="0 0 198 1458" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
                <defs>
                    <linearGradient x1="50%" y1="7.06935325%" x2="50%" y2="100%" id="linearGradient-1">
                        <stop stop-color="#DE1652" offset="0%"></stop>
                        <stop stop-color="#F37121" offset="50.2239948%"></stop>
                        <stop stop-color="#FBAB26" offset="100%"></stop>
                    </linearGradient>
                </defs>
                <g id="Homepage" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-dasharray="12,16" stroke-linejoin="round">
                    <g id="Desktop-Homepage-1" transform="translate(-646.000000, -825.000000)" stroke="url(#linearGradient-1)" stroke-width="4">
                        <g id="content" transform="translate(0.000000, 560.000000)">
                            <path d="M702,266 C682,424 795.064639,474.307498 716,600 C599,786 769,821 688,988 C548.560405,1275.48657 822.815807,1223 840.843207,1373 C858.870608,1523 605.485477,1528 687.610302,1728" id="dashed-path"></path>
                        </g>
                    </g>
                </g>
            </svg>
        </div>
        <div class="desktop-pattern-right">
            <h2>Svg Before animation</h2>
            <svg width="198px" height="1458px" viewBox="0 0 198 1458" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
                <defs>
                    <linearGradient x1="50%" y1="7.06935325%" x2="50%" y2="100%" id="linearGradient-1">
                        <stop stop-color="#DE1652" offset="0%"></stop>
                        <stop stop-color="#F37121" offset="50.2239948%"></stop>
                        <stop stop-color="#FBAB26" offset="100%"></stop>
                    </linearGradient>
                </defs>
                <g id="Homepage" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-dasharray="12,16" stroke-linejoin="round">
                    <g id="Desktop-Homepage-1" transform="translate(-646.000000, -825.000000)" stroke="url(#linearGradient-1)" stroke-width="4">
                        <g id="content" transform="translate(0.000000, 560.000000)">
                            <path d="M702,266 C682,424 795.064639,474.307498 716,600 C599,786 769,821 688,988 C548.560405,1275.48657 822.815807,1223 840.843207,1373 C858.870608,1523 605.485477,1528 687.610302,1728" id="dashed-path"></path>
                        </g>
                    </g>
                </g>
            </svg>
        </div>
    </div>
    <div class="height-div">
        
    </div>


from How to get dashed line svg animation on accordingly scroll?

No comments:

Post a Comment