Skip to content

Instantly share code, notes, and snippets.

@yratof
Created August 15, 2014 11:11
Show Gist options
  • Save yratof/b85f16f2d787d822b629 to your computer and use it in GitHub Desktop.
Save yratof/b85f16f2d787d822b629 to your computer and use it in GitHub Desktop.
SVG lines draw when scrolling. Only works with paths at the moment
<script>
jQuery(document).ready( function( $ ){
//On scroll call the draw function
$(window).scroll(function() {
drawLines();
});
//If you have more than one SVG per page this will pick it up
function drawLines(){
$.each($("path"), function(i, val){
var line = val;
drawLine($(this), line);
});
}
//draw the line
function drawLine(container, line){
var length = 0;
var pathLength = line.getTotalLength();
var distanceFromTop = container.offset().top - $(window).scrollTop();
var percentDone = 1 - (distanceFromTop / $(window).height());
length = percentDone * pathLength;
line.style.strokeDasharray = [length,pathLength].join(' ');
console.log("strokeDasharray: "+[length,pathLength].join(' '));
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment