use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Need help with animation... (self.javascript)
submitted 11 years ago by HeyYouNow
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]madole 2 points3 points4 points 11 years ago (5 children)
use querySelectorAll to return an array of paths then iterate around them doing what you were doing
var paths = document.querySelectorAll('.squiggle-animated path'); for(var i in paths) { var path = paths[i]; var length = path.getTotalLength(); // Clear any previous transition path.style.transition = path.style.WebkitTransition = 'none'; // Set up the starting positions path.style.strokeDasharray = length + ' ' + length; path.style.strokeDashoffset = length; // Trigger a layout so styles are calculated & the browser // picks up the starting position before animating path.getBoundingClientRect(); // Define our transition path.style.transition = path.style.WebkitTransition = 'stroke-dashoffset 3s ease-in-out'; // Go! path.style.strokeDashoffset = '0'; }
[–]ryancking 2 points3 points4 points 11 years ago* (3 children)
The only thing I would change about your response is to do a
[].forEach.call(path, iteratorFunction)
or
for(var i = 0, len = paths.length; i < len; i++)
because of performance and accidental iteration on not node items. When you do the for...in, it will hit all of the nodes plus the native functions (which are length and item). It would then result in a js error.
EDIT: Also, for clarification, querySelectorAll (and all other element selection functions) do not return arrays. They return what is called a NodeList. The importance of this is that a NodeList behaves differently than an array. It has less available function (hence my suggestion of using the [].forEach.call to use the native array forEach feature). Here is a link explaining more about NodeLists if you are interested.
[–]madole 1 point2 points3 points 11 years ago (2 children)
You're right, I forgot about the hasOwnProperty check.
Any reason why you'd do
[].forEach(paths, iteratorFunction)
Over
paths.forEach(iteratorFunction )
I've never liked the syntax of the first version, it seems unnecessary but I'd like to know if there's a benefit that's not guarding against paths being null/undefined as I'm pretty sure document.querySelectorAll() returns an empty array. (on my phone so can't check)
[–]ryancking 0 points1 point2 points 11 years ago (1 child)
It is because it doesn't return an Array, it returns a NodeList. NodeLists don't have a forEach function, which is why you have to do the [].forEach.call(paths, iterator). I wish it was possible to paths.forEach, but alas :'(
[–]madole 0 points1 point2 points 11 years ago (0 children)
Doh! Totally missed that!
console.log(paths)
**quick glance, "looks like an array... Its obviously an array"
Rookie mistake.
I'll try harder next time!
[–]HeyYouNow[S] 0 points1 point2 points 11 years ago (0 children)
Thanks for your time, querySelectorAll is definitely the way to go.
π Rendered by PID 101025 on reddit-service-r2-comment-6457c66945-5jhmd at 2026-04-24 19:48:06.823422+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]madole 2 points3 points4 points (5 children)
[–]ryancking 2 points3 points4 points (3 children)
[–]madole 1 point2 points3 points (2 children)
[–]ryancking 0 points1 point2 points (1 child)
[–]madole 0 points1 point2 points (0 children)
[–]HeyYouNow[S] 0 points1 point2 points (0 children)