【问题标题】:SVG expand line verticalySVG垂直扩展线
【发布时间】:2016-05-12 17:19:57
【问题描述】:

问题是如何在没有 stroke-dasharray 的情况下垂直扩展行。不知道怎么做才对。这是我的例子:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_3" x="0px" y="0px" width="286px" height="286px" viewBox="0 0 286 286" enable-background="new 0 0 286 286" xml:space="preserve">
        <line fill="none" stroke="#3AB0F3" id="stroke-dasharray" stroke-width="244" stroke-miterlimit="10" x1="143" y1="0" x2="143" y2="286"/><animate
                attributeName="stroke-dasharray"
                from="0, 0" to="143, 143"
                dur="3s"
                repeatCount="1"/></line>
    </svg>

这方面的主要问题是如何在 SVG 上设置属性以在没有 stroke-dasharray 的情况下将其垂直扩展。

还有一些来自 d3 图表的 jQuery 代码。

var mySVG1 = $('#Layer_3').drawsvg({
                duration: 3000
            });

mySVG1.drawsvg('animate');

【问题讨论】:

    标签: javascript jquery css d3.js svg


    【解决方案1】:

    这是我在 d3 中的做法:

    d3.select('#lineSelect').transition().duration(1000).attr('y2',700)
    

    这会将您的高度从 286 更改为 700。但是看到这里我已将原始高度更改为 2,因为输出窗口很小。

    但要真正看到这一点,您需要它的容器,即调整高度的 svg。暂时我只改成window.innerHeight,但是可以和line一样更新。

    var height = window.innerHeight;
    
    d3.select('svg').attr('height', height)
    
    d3.select('#lineSelect').transition().delay(500).duration(1000).attr('y2',700) //transition height after half a second
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" width="286px" height="286px" viewBox="0 0 286 286" enable-background="new 0 0 286 286" xml:space="preserve">
      <line id='lineSelect' fill="none" stroke="#3AB0F3" stroke-width="244" stroke-miterlimit="10" x1="143" y1="0" x2="143" y2="2" />
    </svg>

    小提琴:https://jsfiddle.net/thatOneGuy/27u7g3dt/2/

    【讨论】:

    • 请注意,我在该行中添加了一个 'lineSelect' 的 id。如果您不能添加它,那么无论如何都可以选择该行,例如 d3.select('line'),记住如果有多行这将不起作用,则必须选择哪一行的索引你想要
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2011-05-30
    • 2019-05-05
    相关资源
    最近更新 更多