前提・実現したいこと
svg画像がうねうね動くコンテンツを複数作成したいのです。
コンテンツが1つの時はうまく動いたのですが
コンテンツを複数に増やすと2個目以降は上手くうねうね動きません・・・。
元々コンテンツ1つの時は
<svg>のなかの<path>タグの名前は id="js-svg-body"のようにidで指定していましたが
複数に適用させるためclassに変更しています。
どのように指定すれば2つ目以降も同様のjsを適用できるでしょうか??
codepenにもソースコードを載せています。
https://codepen.io/lisaaaan/pen/poJdxqZ
該当のソースコード
【複数で2個目以降が動かないバージョン】
html
1 2<div class="wrap"> 3<svg id="test" width="750" height="400"> 4 <path x="0" y="0" class="js-svg-body" d="M653.452 394.933c-18.731 4.276-60.67-5.921-84.289-11.759-88.179-21.8-236.886-19.2-338.194 6.414-67.639 17.1-98.764 8.775-132.156 5.345C67.6 391.726.157 369.306 13.484 317.965c20.812-80.175 5.657-125.213-3.121-151.8C3.259 144.656-2.359 111.177 1 98.821c12.487-45.967 54.55-60.744 103.019-80.175 42.665-17.1 106.141-21.38 148.806-17.1 84.9 8.509 192.541 51.993 262.23 21.38C558.855 3.68 646.078 5.773 675.3 20.784c18.731 9.621 74.923 35.277 63.477 94.072-5.211 26.763-9.366 78.037 1.04 103.692 8.5 20.969 12.931 75.144 8.325 94.072-12.483 51.312-56.763 73.655-94.69 82.313z"/> 5</div> 6 7<div class="wrap"> 8<svg id="test" width="750" height="400"> 9 <path x="0" y="0" class="js-svg-body" d="M653.452 394.933c-18.731 4.276-60.67-5.921-84.289-11.759-88.179-21.8-236.886-19.2-338.194 6.414-67.639 17.1-98.764 8.775-132.156 5.345C67.6 391.726.157 369.306 13.484 317.965c20.812-80.175 5.657-125.213-3.121-151.8C3.259 144.656-2.359 111.177 1 98.821c12.487-45.967 54.55-60.744 103.019-80.175 42.665-17.1 106.141-21.38 148.806-17.1 84.9 8.509 192.541 51.993 262.23 21.38C558.855 3.68 646.078 5.773 675.3 20.784c18.731 9.621 74.923 35.277 63.477 94.072-5.211 26.763-9.366 78.037 1.04 103.692 8.5 20.969 12.931 75.144 8.325 94.072-12.483 51.312-56.763 73.655-94.69 82.313z"/> 10</div> 11 12<div class="wrap"> 13<svg id="test" width="750" height="400"> 14 <path x="0" y="0" class="js-svg-body" d="M653.452 394.933c-18.731 4.276-60.67-5.921-84.289-11.759-88.179-21.8-236.886-19.2-338.194 6.414-67.639 17.1-98.764 8.775-132.156 5.345C67.6 391.726.157 369.306 13.484 317.965c20.812-80.175 5.657-125.213-3.121-151.8C3.259 144.656-2.359 111.177 1 98.821c12.487-45.967 54.55-60.744 103.019-80.175 42.665-17.1 106.141-21.38 148.806-17.1 84.9 8.509 192.541 51.993 262.23 21.38C558.855 3.68 646.078 5.773 675.3 20.784c18.731 9.621 74.923 35.277 63.477 94.072-5.211 26.763-9.366 78.037 1.04 103.692 8.5 20.969 12.931 75.144 8.325 94.072-12.483 51.312-56.763 73.655-94.69 82.313z"/> 15</div>
js
1let $svgBody = Snap('.js-svg-body'), 2 path =['M653.452 394.933c-18.731 4.276-60.67-5.921-84.289-11.759-88.179-21.8-236.886-19.2-338.194 6.414-67.639 17.1-98.764 8.775-132.156 5.345C67.6 391.726.157 369.306 13.484 317.965c20.812-80.175 5.657-125.213-3.121-151.8C3.259 144.656-2.359 111.177 1 98.821c12.487-45.967 54.55-60.744 103.019-80.175 42.665-17.1 106.141-21.38 148.806-17.1 84.9 8.509 192.541 51.993 262.23 21.38C558.855 3.68 646.078 5.773 675.3 20.784c18.731 9.621 74.923 35.277 63.477 94.072-5.211 26.763-9.366 78.037 1.04 103.692 8.5 20.969 12.931 75.144 8.325 94.072-12.483 51.312-56.763 73.655-94.69 82.313z', 3 4'M646.924 390.1c-53.039-5.959-83.193-4.684-107.079 0-85.225 16.715-190.888 8.9-293.215 1-59.013-4.557-87.085-4.1-148.3 5.892-30.836 5.033-86.8-23.782-85.392-76.446 1.517-56.58-13.494-93.329-3.124-150.77C13.817 147.625-2.91 90.5.449 78.23 12.945 32.574 51.9 15.319 103.541 23.251c54.023 8.3 112.066 1.344 180.116-17.527C372.881-19.019 475.77 45.83 548.852 23.6 610.9 4.731 673.718 6.706 702.965 21.616c18.745 9.555 25.5 18.161 33.025 48.668C748 118.952 750 159.674 750 203.376c0 64.123-1.289 78.037-10.007 122.167-10.008 50.657-54.385 68.907-93.069 64.557z', 5 6'M653.667 391.333c-52.723 8.487-79.008 8.146-114.167.716-85.046-17.972-190.864-6.4-293.248 1.012-88.639 6.418-146.646 9.473-181.65 4.382-30.933-4.5-56.006-18.33-52.063-76.228 3.633-53.342 25.516-86.246 2.058-140.674C4.777 157.757.446 87.413.044 74.463-1.4 27.793 33.058 24.889 84.6 20.665 138.623 16.237 215.619-3.775 283.284.629c92.412 6.014 194.364 13.926 265.224 18.2 64.745 3.911 81.191-4.5 136.163-9.371 20.963-1.857 61.017 25.576 58.006 59.063-4.57 50.833-30 90.653-10 131.363 28.693 58.4 17 124.235-1 145.619-33.608 39.927-41.93 40.021-78.01 45.83z'], 7 8 SPEED =1500, 9 EASING = mina.linear, 10 i = 0; 11 12 13 14 15function AnimationSvg(){ 16 17 $svgBody.animate({d: path[i] },SPEED,EASING,AnimationSvg); 18 19 i++; 20 21 if(i === path.length){ 22 i = 0; 23 } 24} 25 26AnimationSvg(); 27 28
【1つで動くバージョン】
html
1<div class="wrap"> 2<svg id="test" width="750" height="400"> 3 <path x="0" y="0" id="js-svg-body" d="M653.452 394.933c-18.731 4.276-60.67-5.921-84.289-11.759-88.179-21.8-236.886-19.2-338.194 6.414-67.639 17.1-98.764 8.775-132.156 5.345C67.6 391.726.157 369.306 13.484 317.965c20.812-80.175 5.657-125.213-3.121-151.8C3.259 144.656-2.359 111.177 1 98.821c12.487-45.967 54.55-60.744 103.019-80.175 42.665-17.1 106.141-21.38 148.806-17.1 84.9 8.509 192.541 51.993 262.23 21.38C558.855 3.68 646.078 5.773 675.3 20.784c18.731 9.621 74.923 35.277 63.477 94.072-5.211 26.763-9.366 78.037 1.04 103.692 8.5 20.969 12.931 75.144 8.325 94.072-12.483 51.312-56.763 73.655-94.69 82.313z"/> 4</svg> 5</div> 6
js
1let $svgBody = Snap('#js-svg-body'), 2 path =['M653.452 394.933c-18.731 4.276-60.67-5.921-84.289-11.759-88.179-21.8-236.886-19.2-338.194 6.414-67.639 17.1-98.764 8.775-132.156 5.345C67.6 391.726.157 369.306 13.484 317.965c20.812-80.175 5.657-125.213-3.121-151.8C3.259 144.656-2.359 111.177 1 98.821c12.487-45.967 54.55-60.744 103.019-80.175 42.665-17.1 106.141-21.38 148.806-17.1 84.9 8.509 192.541 51.993 262.23 21.38C558.855 3.68 646.078 5.773 675.3 20.784c18.731 9.621 74.923 35.277 63.477 94.072-5.211 26.763-9.366 78.037 1.04 103.692 8.5 20.969 12.931 75.144 8.325 94.072-12.483 51.312-56.763 73.655-94.69 82.313z', 3 4'M646.924 390.1c-53.039-5.959-83.193-4.684-107.079 0-85.225 16.715-190.888 8.9-293.215 1-59.013-4.557-87.085-4.1-148.3 5.892-30.836 5.033-86.8-23.782-85.392-76.446 1.517-56.58-13.494-93.329-3.124-150.77C13.817 147.625-2.91 90.5.449 78.23 12.945 32.574 51.9 15.319 103.541 23.251c54.023 8.3 112.066 1.344 180.116-17.527C372.881-19.019 475.77 45.83 548.852 23.6 610.9 4.731 673.718 6.706 702.965 21.616c18.745 9.555 25.5 18.161 33.025 48.668C748 118.952 750 159.674 750 203.376c0 64.123-1.289 78.037-10.007 122.167-10.008 50.657-54.385 68.907-93.069 64.557z', 5 6'M653.667 391.333c-52.723 8.487-79.008 8.146-114.167.716-85.046-17.972-190.864-6.4-293.248 1.012-88.639 6.418-146.646 9.473-181.65 4.382-30.933-4.5-56.006-18.33-52.063-76.228 3.633-53.342 25.516-86.246 2.058-140.674C4.777 157.757.446 87.413.044 74.463-1.4 27.793 33.058 24.889 84.6 20.665 138.623 16.237 215.619-3.775 283.284.629c92.412 6.014 194.364 13.926 265.224 18.2 64.745 3.911 81.191-4.5 136.163-9.371 20.963-1.857 61.017 25.576 58.006 59.063-4.57 50.833-30 90.653-10 131.363 28.693 58.4 17 124.235-1 145.619-33.608 39.927-41.93 40.021-78.01 45.83z'], 7 8 SPEED =1500, 9 EASING = mina.linear, 10 i = 0; 11 12function AnimationSvg(){ 13 14 $svgBody.animate({d: path[i] },SPEED,EASING,AnimationSvg); 15 16 i++; 17 18 if(i === path.length){ 19 i = 0; 20 } 21} 22 23 24AnimationSvg(); 25 26
回答3件
あなたの回答
tips
プレビュー