首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向SVG路径添加类

向SVG路径添加类
EN

Stack Overflow用户
提问于 2017-12-15 14:39:27
回答 1查看 1.1K关注 0票数 0

我正在尝试添加一个新的类到SVG路径元素,因为我只希望它在视口中可见时开始动画。然而,它似乎并没有起作用。我试过使用$("#item").attr("class", "oldclass newclass");,但它似乎不能很好地工作。请一定要帮帮我!谢谢!

代码语言:javascript
复制
//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');
代码语言:javascript
复制
.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;
        }
    }
代码语言:javascript
复制
<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"/>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-15 14:40:21

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

这使得我们不会每次都遇到“旧类新类”这样的麻烦。

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

我没有错误:

代码语言:javascript
复制
//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');
代码语言:javascript
复制
.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;
        }
    }
代码语言:javascript
复制
<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"/>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47827074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档