将类添加到SVG路径

时间:2017-12-15 06:39:27

标签: javascript jquery html css svg

我正在尝试向SVG Path元素添加一个新类,因为我只希望它在视口中可见时启动动画。然而它似乎没有用。我尝试使用$("#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');
&#13;
.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;
        }
    }
&#13;
<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"/>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

添加课程的单独功能以及.addClass()

这使我们不会有麻烦的旧班级#34;每一次。

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

对我没有错误:

&#13;
&#13;
//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');
&#13;
.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;
        }
    }
&#13;
<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"/>
&#13;
&#13;
&#13;