代码之家  ›  专栏  ›  技术社区  ›  kristheman

操作svg元素的可见性

  •  1
  • kristheman  · 技术社区  · 8 年前

    我正在制作一个JavaWeb项目,我想用一些JavaScript操作一些前端SVG元素。

    我正在绘制一个相当简单的车库图,其中有一个来自jsp页面的shed元素。

    我想要一个javascript函数,on/off按钮,其中的元素可以显示而不是隐藏。

    这是我目前的代码:

    ....
    <line x1="<%= Math.abs(wid+20) %>"  y1="0" x2="<%= Math.abs(wid+20) %>" y2="<%= len %>"
    style="stroke:#c40d0d;
    marker-start: url(#beginArrow);
    marker-end: url(#endArrow);"/>  
    
    
    //this is the start of the part that I want to be toggled on and off with a javascript function
    <% if(shed){ %>
    
    <rect x="<%= Math.abs(wid-200) %>" y="0" height="<%= len %>" width="200"
        style="stroke:#000000; stroke-width: 3; fill: none"/>
    
    
    <rect x="<%= Math.abs(wid-200) %>" y="0" height="10" width="10"
        style="stroke:#000000; fill: #101111"/>
    <rect x="<%= Math.abs(wid-10) %>" y="0" height="10" width="10"
        style="stroke:#000000; fill: #101111"/>
    <rect x="<%= Math.abs(wid-200) %>" y="<%= Math.abs(len-10) %>" height="10" width="10"
        style="stroke:#000000; fill: #101111"/>
    <rect x="<%= Math.abs(wid-10) %>" y="<%= Math.abs(len-10) %>" height="10" width="10"
        style="stroke:#000000; fill: #101111"/>
    
     <text x="<%= Math.abs((wid-200)+(wid/8)) %>" y="<%= Math.abs(len+35) %>" fill="Red"> Skur: 200.0 cm </text>
    
    <line x1="<%= Math.abs(wid-200) %>"  y1="<%= Math.abs(len+20) %>" x2=" 
    <%= wid %>" y2="<%= Math.abs(len+20) %>" 
    style="stroke:#c40d0d;
    marker-start: url(#beginArrow);
        marker-end: url(#endArrow);"/>  
    
    <% } %>
    
    </SVG>
    <form action="FrontController?command=DynamicCarportSide" name="order" method="POST">
        <input type="hidden" name="length" value="<%= len %>">
        <input type="hidden" name="width" value="<%= wid %>">
        <input type="hidden" name="shed" value="<%= shed %>">
        <input type="submit" value="Side tegning">
    </form>
          <%@include file="footer.jsp" %>
    </body>
    </html>
    

    我在jsp中使用if语句来显示是否有shed,但是在页面呈现之后可以使用click按钮来显示它吗?

    我在想这样的事。

    function myFunction() {
    
    var SVGcode ="//insert html code here";
    
    document.getElementById("demo").innerHTML = SVGcode;
    }
    

    问题是svg代码太多了,所以我不知道该怎么做

    编辑:

    这里有一张图片展示了它的外观,到目前为止,我只有一个函数,当按下按钮时,它会写hello,但是我希望有一个开关打开按钮,它会添加额外的svg代码

    Here is an example of an SVG-drawing without the dynamic data, only dummy data

    1 回复  |  直到 8 年前
        1
  •  2
  •   Ivan Jeffrey Zhao    8 年前

    切换给定元素的可见性

    您需要一个函数来隐藏和显示svg元素的某些部分。我使用的方法实际上适用于任何给定的html元素(对象的 HTMLElement )你需要:

    1. 提供要隐藏/显示类名的元素 ,例如:

      <line class="segments" x1="55" y1="55" ... stroke-dasharray: 10 5"/>
      <line class="segments" x1="550" y1="55" ... stroke-dasharray: 10 5"/>
      
    2. 用于切换给定元素可见性的函数 :

      • 我们将创建一个函数 hide() 隐藏 HTMLElement公司

        const hide = e => e.style.display = 'none'
        
      • 和A show() 函数来显示它…

        const show = e => e.style.display = ''
        
      • 最后,处理切换的函数:

        const toggleHide = function(selector) {
          [...document.querySelectorAll(selector)].forEach(e => e.style.display ? show(e) : hide(e))
        }
        
    3. 最后 调用函数的触发器 ,我们可以使用 button :

      <button onclick="toggleHide('.frame')">Toggle frame</button>
      

      将函数设置为调用 onclick 按钮的属性,并将选择器作为参数(此处 .frame )要将其链接到的元素的。也就是说,这个按钮将用类名切换所有元素的可见性 框架


    演示您的 svg 绘画

    在下面的演示中,我添加了两个链接到 静止无功发生器 元素。点击 显示代码段 gt; 运行代码段 gt; 整页 预览:

    const hide = e => e.style.display = 'none'
    const show = e => e.style.display = ''
    const toggleHide = function(selector) {
      [...document.querySelectorAll(selector)].forEach(e => e.style.display ? show(e) : hide(e))
    }
    <html>
    
    <body>
      <SVG width="780" height="600">
    <rect x="0" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="55" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="110" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="165" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="220" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="275" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="330" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="385" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="440" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="495" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="550" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="605" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="660" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="715" y="0" height="600" width="55"
            style="stroke:#000000; fill: #ffffff"/>
    <rect x="770" y="0" height="600" width="55"
            style="stroke:#000000; fill: none"/>
    <rect x="0" y="50" height="15" width="825"
            style="stroke:#000000; fill: none"/>
    <rect x="0" y="535" height="15" width="825"
            style="stroke:#000000; fill: none"/>
    
    <rect class="frame" x="550" y="50" height="500" width="230"
            style="stroke:#000000; stroke-width: 5; fill: none"/>
    <rect x="440" y="0" height="600" width="1"
            style="stroke:#000000; stroke-width: 3; fill: none"/>
    
    <rect x="550" y="50" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="550" y="535" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="765" y="50" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="765" y="535" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="550" y="300" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="765" y="300" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="110" y="50" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="110" y="535" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="408" y="50" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
    <rect x="408" y="535" height="15" width="15"
            style="stroke:#000000; fill: #101111"/>
      <line class="segments" x1="55" y1="55" x2="550" y2="550"
          style="stroke: #000000; fill:none;
          stroke-width: 6px;
          stroke-dasharray: 10 5"/>
      <line class="segments" x1="550" y1="55" x2="55" y2="550"
          style="stroke: #000000; fill:none;
          stroke-width: 6px;
          stroke-dasharray: 10 5"/>
      
      <text x="400" y="620" fill="Red">7,8 m</text>
      <text x="835" y="300" fill="Red">6 m</text>
      
      <defs>
        <marker id="beginArrow" 
        	markerWidth="9" markerHeight="9" 
        	refX="0" refY="4" 
        	orient="auto">
            <path d="M0,4 L8,0 L8,8 L0,4" style="fill: #c40d0d;" />
        </marker>
        <marker id="endArrow" 
        	markerWidth="9" markerHeight="9" 
        	refX="8" refY="4" 
        	orient="auto">
            <path d="M0,0 L8,4 L0,8 L0,0" style="fill: #c40d0d;" />
        </marker>
    </defs>
    <line x1="0"  y1="630" x2="825"   y2="630" 
    	style="stroke:#c40d0d;
    	marker-start: url(#beginArrow);
       marker-end: url(#endArrow);"/>
    <defs>
        <marker id="beginArrow2" 
        	markerWidth="9" markerHeight="9" 
        	refX="0" refY="4" 
        	orient="auto">
            <path d="M0,4 L8,0 L8,8 L0,4" style="fill: #c40d0d;" />
        </marker>
        <marker id="endArrow2" 
        	markerWidth="9" markerHeight="9" 
        	refX="8" refY="4" 
        	orient="auto">
            <path d="M0,0 L8,4 L0,8 L0,0" style="fill: #c40d0d;" />
        </marker>
    </defs>
    <line x1="865"  y1="0" x2="865"   y2="600" 
    	style="stroke:#c40d0d;
    	marker-start: url(#beginArrow);
       marker-end: url(#endArrow);"/>
       
        
    </SVG>
    
      <p>click on the buttons to remove some parts of the shed</p>
    
      <button onclick="toggleHide('.segments')">Toggle bars</button>
      <button onclick="toggleHide('.frame')">Toggle frame</button>
    
    </body>
    
    </html>