gpt4 book ai didi

javascript - 使 SVG 图形在进入窗口时播放(滚动)

转载 作者:行者123 更新时间:2023-12-03 07:08:01 25 4
gpt4 key购买 nike

我有一个棘手的问题!我的一些页面上有几个 SVG 图形,它们在页面加载时播放。但是因为他们中的许多人都在首屏之下,所以我希望他们在滚动到 View 中时开始播放(并播放一次)。

注意:我到处寻找解决方案,甚至聘请了一位无法使其工作的自由职业者。我认为我的 SVG 的不同之处在于它们使用 @keyframe 并且有不止一个按顺序播放的路径,这可能是其他解决方案不起作用的原因?路径不止一个的原因是它是一个书法箭头形状,当 SVG 蒙版显示时会重叠。

我的 SVG 之一的 HTML(在 DIV 中以使其响应):

<div id="arrow-1id" class="arrow-1 leftarrows">
<!--START ARROW 1-->
<svg id="arrow1svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 436.55 256.959">

<mask id="arrow1-mask1" maskUnits="userSpaceOnUse">
<path d="M4.537,54.288
c96.333-80.667,213.667,135.333,90.667,96.333"/>
</mask>

<mask id="arrow1-mask2" maskUnits="userSpaceOnUse">
<path d="M100.87,156.955
c-29.667-100.667,29.666-152,78.333-146.667s151.667,255.667,29.667,234"/>
</mask>

<mask id="arrow1-mask3" maskUnits="userSpaceOnUse">
<path d="M214.87,248.956
c-53.667-43.667,15.334-337.334,195.667-169.667"/>
</mask>

<mask id="arrow1-mask4" maskUnits="userSpaceOnUse">
<path d="M322.203,53.955c108.667,43,117,69.09,68-53.955"/>
</mask>

<path mask="url(#arrow1-mask1)" fill="#42A8FC" d="M98.168,50.639C66.146,29.84,27.535,21.405,0.142,44.781c-1.983,1.692,17.501,16.275,21.915,12.509
c21.17-18.066,49.736-15.119,72.854-0.936c11.126,6.518,19.094,15.658,19.094,15.658c8.426,9.078,14.961,19.84,18.385,31.416
c4.701,15.891,0.705,30.535-10.91,41.153c-0.855,0.782-1.803,1.409-2.783,1.981c-0.01,0.004-6.326,4.56-16.162,2.465l-6.889,7.466
c9.913,8.681,22.827,13.119,36.498,7.999c43.123-16.156,23.759-65.003-14.488-98.967C117.654,65.526,108.286,57.122,98.168,50.639z"/>

<path mask="url(#arrow1-mask2)" fill="#42A8FC" d="M231.959,50.614c-17.516-25.197-43.061-49.605-73.761-50.592c-20.618-0.664-34.757,13.81-44.931,27.774
c-5.189,7.124-10.349,14.789-15.1,22.843l-3.258,5.715C84.467,75.328,76.75,96.273,76.766,117.7
c0.01,13.138,7.45,28.785,18.879,38.794l6.889-7.466c-0.658-1.355-1.329-2.721-1.771-4.061
c-7.573-22.907,0.716-49.699,13.241-72.955l3.65-6.486c7.376-12.557,15.713-23.818,23.057-32.426
c8.87-10.398,23.658-23.112,40.648-19.129c14.01,3.285,24.33,19.391,31.584,30.21c3.548,5.288,7.105,11.241,10.578,17.655
l12.133,25.446c22.227,53.15,32.844,122.106-8.789,151.369c-3.873,2.722-8.496,4.636-12.935,6.029l-3.878,7.245
c25.657,15.113,52.242-6.228,60.439-31.938c14.138-44.338-1.676-101.902-24.266-145.678
C246.227,74.311,238.632,60.523,231.959,50.614z"/>

<path mask="url(#arrow1-mask3)" fill="#42A8FC" d="M406.734,68.147c-17.148-17.841-37.951-33.412-59.953-44.124c-50.594-24.632-88.095-5.991-114.822,26.591
l-8.438,11.223c-3.395,4.914-6.599,10.027-9.592,15.281c-17.29,30.335-29.316,64.641-31.375,100.486
c-1.105,19.22-1.053,44.238,13.145,61.586c4.664,5.7,9.492,9.873,14.354,12.736l3.878-7.245c-3.323-4.796-5.046-11.189-6.378-16.149
c-5.317-19.783-3.871-40.435-0.658-59.469c4.553-26.978,14.076-56.253,28.76-81.78l9.521-14.934
c8.408-11.894,18.098-22.568,29.177-31.183c41.766-32.474,90.141-8.049,122.342,30.429
C404.036,81.622,413.766,75.922,406.734,68.147z"/>

<path mask="url(#arrow1-mask4)" fill="#42A8FC" d="M404.413,71.956c0,0-3.166,0-12.344-3.026c-17.545-5.735-35.092-11.471-52.635-17.206
c-15.207-4.971-15.507,16.587-1.976,21.011c27.503,8.99,55.004,17.981,82.506,26.972c9.534,3.115,13.706-5.336,11.287-12.662
c-8.52-25.797-17.039-51.594-25.558-77.392c-4.546-13.767-25.338-10.047-20.597,4.315c5.671,17.17,11.341,34.342,17.013,51.514
L404.413,71.956z"/>

</svg>

<style>
#arrow1-mask1 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 265.809 265.809;
stroke-dashoffset: 0;
animation: brush1a 2s linear;
animation-fill-mode: forwards;
}

#arrow1-mask2 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 499.444 499.444;
stroke-dashoffset: 0;
animation: brush1b 2s linear;
animation-fill-mode: forwards;
}

#arrow1-mask3 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 396.144 396.144;
stroke-dashoffset: 0;
animation: brush1c 2s linear;
animation-fill-mode: forwards;
}

#arrow1-mask4 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 195.971 195.971;
stroke-dashoffset: 0;
animation: brush1d 2s linear;
animation-fill-mode: forwards;
}

@keyframes brush1a {
0% {
stroke-dashoffset: 265.809;
}
1% {
stroke-dashoffset: 265.809;
}
20% {
stroke-dashoffset: 0;
}
100% {
stroke-dashoffset: 0;
}
}

@keyframes brush1b {
0% {
stroke-dashoffset: 499.444;
}
20% {
stroke-dashoffset: 499.444;
}
60% {
stroke-dashoffset: 0;
}
100% {
stroke-dashoffset: 0;
}
}

@keyframes brush1c {
0% {
stroke-dashoffset: 396.144;
}
60% {
stroke-dashoffset: 396.144;
}
80% {
stroke-dashoffset: 0;
}
100% {
stroke-dashoffset: 0;
}
}

@keyframes brush1d {
0% {
stroke-dashoffset: 195.971;
}
80% {
stroke-dashoffset: 195.971;
}
100% {
stroke-dashoffset: 0;
}
}
</style>
<!--END ARROW 1-->
</div>

我没有发布任何 JS,因为我试过的代码都不起作用。

最佳答案

如果容器 #arrow-1id 作为特定类(比方说 .play),您可以通过运行动画来实现这一点。然后,当容器出现在屏幕上时,您只需使用 javascript 进行检查。在这种情况下,只需将 .play 类添加到容器中即可:

var body = document.getElementsByTagName("body")[0];
var div = document.getElementById("arrow-1id");

body.onscroll = function() {
if (document.documentElement.scrollTop >= div.offsetTop - 200) {
div.classList.add("play");
}
};
body {
height: 2000px;
}

#arrow-1id {
margin-top: 400px;
background-color: lightgrey;
}

.play #arrow1-mask1 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 265.809 265.809;
stroke-dashoffset: 0;
animation: brush1a 2s linear;
animation-fill-mode: forwards;
}

.play #arrow1-mask2 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 499.444 499.444;
stroke-dashoffset: 0;
animation: brush1b 2s linear;
animation-fill-mode: forwards;
}

.play #arrow1-mask3 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 396.144 396.144;
stroke-dashoffset: 0;
animation: brush1c 2s linear;
animation-fill-mode: forwards;
}

.play #arrow1-mask4 path {
fill: none;
stroke: white;
stroke-width: 39;
stroke-dasharray: 195.971 195.971;
stroke-dashoffset: 0;
animation: brush1d 2s linear;
animation-fill-mode: forwards;
}

@keyframes brush1a {
0% {
stroke-dashoffset: 265.809;
}
1% {
stroke-dashoffset: 265.809;
}
20% {
stroke-dashoffset: 0;
}
100% {
stroke-dashoffset: 0;
}
}

@keyframes brush1b {
0% {
stroke-dashoffset: 499.444;
}
20% {
stroke-dashoffset: 499.444;
}
60% {
stroke-dashoffset: 0;
}
100% {
stroke-dashoffset: 0;
}
}

@keyframes brush1c {
0% {
stroke-dashoffset: 396.144;
}
60% {
stroke-dashoffset: 396.144;
}
80% {
stroke-dashoffset: 0;
}
100% {
stroke-dashoffset: 0;
}
}

@keyframes brush1d {
0% {
stroke-dashoffset: 195.971;
}
80% {
stroke-dashoffset: 195.971;
}
100% {
stroke-dashoffset: 0;
}
}
<div id="arrow-1id" class="arrow-1 leftarrows">
<!--START ARROW 1-->
<svg id="arrow1svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 436.55 256.959">

<mask id="arrow1-mask1" maskUnits="userSpaceOnUse">
<path d="M4.537,54.288
c96.333-80.667,213.667,135.333,90.667,96.333" />
</mask>

<mask id="arrow1-mask2" maskUnits="userSpaceOnUse">
<path d="M100.87,156.955
c-29.667-100.667,29.666-152,78.333-146.667s151.667,255.667,29.667,234" />
</mask>

<mask id="arrow1-mask3" maskUnits="userSpaceOnUse">
<path d="M214.87,248.956
c-53.667-43.667,15.334-337.334,195.667-169.667" />
</mask>

<mask id="arrow1-mask4" maskUnits="userSpaceOnUse">
<path d="M322.203,53.955c108.667,43,117,69.09,68-53.955" />
</mask>

<path mask="url(#arrow1-mask1)" fill="#42A8FC" d="M98.168,50.639C66.146,29.84,27.535,21.405,0.142,44.781c-1.983,1.692,17.501,16.275,21.915,12.509
c21.17-18.066,49.736-15.119,72.854-0.936c11.126,6.518,19.094,15.658,19.094,15.658c8.426,9.078,14.961,19.84,18.385,31.416
c4.701,15.891,0.705,30.535-10.91,41.153c-0.855,0.782-1.803,1.409-2.783,1.981c-0.01,0.004-6.326,4.56-16.162,2.465l-6.889,7.466
c9.913,8.681,22.827,13.119,36.498,7.999c43.123-16.156,23.759-65.003-14.488-98.967C117.654,65.526,108.286,57.122,98.168,50.639z" />

<path mask="url(#arrow1-mask2)" fill="#42A8FC" d="M231.959,50.614c-17.516-25.197-43.061-49.605-73.761-50.592c-20.618-0.664-34.757,13.81-44.931,27.774
c-5.189,7.124-10.349,14.789-15.1,22.843l-3.258,5.715C84.467,75.328,76.75,96.273,76.766,117.7
c0.01,13.138,7.45,28.785,18.879,38.794l6.889-7.466c-0.658-1.355-1.329-2.721-1.771-4.061
c-7.573-22.907,0.716-49.699,13.241-72.955l3.65-6.486c7.376-12.557,15.713-23.818,23.057-32.426
c8.87-10.398,23.658-23.112,40.648-19.129c14.01,3.285,24.33,19.391,31.584,30.21c3.548,5.288,7.105,11.241,10.578,17.655
l12.133,25.446c22.227,53.15,32.844,122.106-8.789,151.369c-3.873,2.722-8.496,4.636-12.935,6.029l-3.878,7.245
c25.657,15.113,52.242-6.228,60.439-31.938c14.138-44.338-1.676-101.902-24.266-145.678
C246.227,74.311,238.632,60.523,231.959,50.614z" />

<path mask="url(#arrow1-mask3)" fill="#42A8FC" d="M406.734,68.147c-17.148-17.841-37.951-33.412-59.953-44.124c-50.594-24.632-88.095-5.991-114.822,26.591
l-8.438,11.223c-3.395,4.914-6.599,10.027-9.592,15.281c-17.29,30.335-29.316,64.641-31.375,100.486
c-1.105,19.22-1.053,44.238,13.145,61.586c4.664,5.7,9.492,9.873,14.354,12.736l3.878-7.245c-3.323-4.796-5.046-11.189-6.378-16.149
c-5.317-19.783-3.871-40.435-0.658-59.469c4.553-26.978,14.076-56.253,28.76-81.78l9.521-14.934
c8.408-11.894,18.098-22.568,29.177-31.183c41.766-32.474,90.141-8.049,122.342,30.429
C404.036,81.622,413.766,75.922,406.734,68.147z" />

<path mask="url(#arrow1-mask4)" fill="#42A8FC" d="M404.413,71.956c0,0-3.166,0-12.344-3.026c-17.545-5.735-35.092-11.471-52.635-17.206
c-15.207-4.971-15.507,16.587-1.976,21.011c27.503,8.99,55.004,17.981,82.506,26.972c9.534,3.115,13.706-5.336,11.287-12.662
c-8.52-25.797-17.039-51.594-25.558-77.392c-4.546-13.767-25.338-10.047-20.597,4.315c5.671,17.17,11.341,34.342,17.013,51.514
L404.413,71.956z" />

</svg>


<!--END ARROW 1-->
</div>

关于javascript - 使 SVG 图形在进入窗口时播放(滚动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64117120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com