javascript - 将类添加到 SVG 路径

标签 javascript jquery html css svg

我正在尝试向 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/

相关文章:

javascript - 为什么我会收到带有额外字符的响应文本(从 Servlet 到 javaScript,通过 Ajax)?

javascript - Schema.org 本体作为 json

javascript - Word 联机加载项 : Using objects across multiple contexts

javascript - 从 Javascript 中的日期字符串中获取确切的日期

javascript - 如何返回调用 javascript 的弹出窗口内网格的值?

PHP mySQL 搜索不工作

html - 从 HTML 输入控件创建列表或数组的正确方法

php - Symfony2 禁用选择

jquery - 我可以强制 Jquery Dirtyform 将表单视为脏表单吗?

php - 仅播放音频的 video/mp4 类型的 HTML5 视频