Created
August 15, 2014 11:11
-
-
Save yratof/b85f16f2d787d822b629 to your computer and use it in GitHub Desktop.
SVG lines draw when scrolling. Only works with paths at the moment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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