gpt4 book ai didi

javascript - 将类添加到 SVG 路径

转载 作者:太空宇宙 更新时间:2023-11-04 07:50:16 25 4
gpt4 key购买 nike

我正在尝试向 SVG 路径元素添加一个新类,因为我只希望它在视口(viewport)中可见时开始动画。但是它似乎没有用。我尝试使用 $("#item").attr("class", "oldclass newclass"); 但它似乎并没有起作用。请帮帮我!谢谢!

//window and animation items
var st7 = $.find('.st7');
var web_window = $(window);

//check to see if any animation containers are currently in view
function check_if_in_view() {
//get current window information
var window_height = web_window.height();
var window_top_position = web_window.scrollTop();
var window_bottom_position = (window_top_position + window_height);

//iterate through elements to see if its in view
$.each(st7, function() {
//get the element sinformation
var element = $(this);
var element_height = $(element).outerHeight();
var element_top_position = $(element).offset().top;
var element_bottom_position = (element_top_position + element_height);

//check to see if this current container is visible (its viewable if it exists between the viewable space of the viewport)
if ((element_bottom_position >= window_top_position) && (element_top_position <= window_bottom_position)) {
element.setAttribute("class", "triggeredCSS3");
} else {
element.removeClass('in-view');
}
});
}

//on or scroll, detect elements in view
$(window).on('scroll resize', function() {
check_if_in_view()
})

//trigger our scroll event on initial load
$(window).trigger('scroll');
.st7 {
fill: none;
stroke: #fff;
stroke-miterlimit: 10;
stroke-dasharray: 5000;
stroke-dashoffset: 5000;
/*animation: draw1 8s linear forwards;*/
stroke-width: 4;
}

.st7.triggeredCSS3 {
animation: draw1 8s linear forwards;
}

@keyframes draw1{
to {
stroke-dashoffset: 0;
}
}
@keyframes draw2{
to {
stroke-dashoffset: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg class="line-drawing" width="110%" height="700" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 600">
<path d="m 119.21227,317.3823" class="st7"/>

最佳答案

有一个单独的函数用于添加类,它是 .addClass() .

这让我们不必每次都遇到“旧类新类”的麻烦。

使用$("#item").addClass("newclass");

我没有错误:

//window and animation items
var st7 = $.find('.st7');
var web_window = $(window);

//check to see if any animation containers are currently in view
function check_if_in_view() {
//get current window information
var window_height = web_window.height();
var window_top_position = web_window.scrollTop();
var window_bottom_position = (window_top_position + window_height);

//iterate through elements to see if its in view
$.each(st7, function() {
//get the element sinformation
var element = $(this);
var element_height = $(element).outerHeight();
var element_top_position = $(element).offset().top;
var element_bottom_position = (element_top_position + element_height);

//check to see if this current container is visible (its viewable if it exists between the viewable space of the viewport)
if ((element_bottom_position >= window_top_position) && (element_top_position <= window_bottom_position)) {
element.addClass("triggeredCSS3");
} else {
element.removeClass('in-view');
}
});
}

//on or scroll, detect elements in view
$(window).on('scroll resize', function() {
check_if_in_view()
})

//trigger our scroll event on initial load
$(window).trigger('scroll');
.st7 {
fill: none;
stroke: #fff;
stroke-miterlimit: 10;
stroke-dasharray: 5000;
stroke-dashoffset: 5000;
/*animation: draw1 8s linear forwards;*/
stroke-width: 4;
}

.st7.triggeredCSS3 {
animation: draw1 8s linear forwards;
}

@keyframes draw1{
to {
stroke-dashoffset: 0;
}
}
@keyframes draw2{
to {
stroke-dashoffset: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg class="line-drawing" width="110%" height="700" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 600">
<path d="m 119.21227,317.3823" class="st7"/>

关于javascript - 将类添加到 SVG 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827074/

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