【问题标题】:How to change the fill color of an svg by clicking on it如何通过单击更改 svg 的填充颜色
【发布时间】:2020-12-01 18:29:50
【问题描述】:

我对 Web 开发和 javascript 完全陌生,(但我对编程有基本的了解) 我想创建一堆具有不同颜色的按钮,并让用户能够单击一个按钮来选择一种颜色,然后在 svg 图像中填充一个区域(路径),我的问题是我创建了一个单击按钮时获取颜色值的变量,我使用它为 svg 图像上的路径着色,当我从按钮中选择不同的颜色时,svg 图像中的颜色会发生变化,而无需单击它。我希望能够在 svg 图像上保留以前的颜色,直到我再次单击它进行更改。请有人帮助并为长消息感到抱歉。 这是 HTML

<!DOCTYPE html PUBLIC>
<html>
<head>
<link rel="stylesheet" href="pathcolors3.css">
</head>
<body>
<div class="swatches">
    <button style="background: rgb(153,153,0)"></button>
    <button style="background: rgb(103,103,0)"></button>
    <button style="background: rgb(100,100,0)"></button>
    <button style="background: rgb(10,20,100)"></button>
    <button style="background: rgb(26,75,100)"></button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


 <svg>
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:cc="http://creativecommons.org/ns#"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:svg="http://www.w3.org/2000/svg"
 xmlns="http://www.w3.org/2000/svg"
 viewBox="0 0 793.70667 1122.52"
 height="1122.52"
 width="793.70667"
 xml:space="preserve"
 id="svg2"
 version="1.1"><metadata
 id="metadata8"><rdf:RDF><cc:Work
     rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
       rdf:resource="http://purl.org/dc/dcmitype/StillImage" /></cc:Work></rdf:RDF></metadata><defs
 id="defs6" /><g
 transform="matrix(1.3333333,0,0,-1.3333333,0,1122.52)"
 id="g10"><path class="zone1"
 id="path12" 
   fill="";stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke- 
 miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
   d="M 584.447,109.821 H 17.518 V 676.75 h 566.929 z" /><path class="zone2"
   id="path14"
   fill="";stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke- 
miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
   d="M 242.566,109.741 H 129.18 V 676.67 h 113.386 z" /><path class="zone3"
   id="path16"
   fill="";stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke- 
 miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
   d="M 471.022,109.894 H 357.636 v 566.929 h 113.386 z" /></g></svg>



<script src="main3.js">
</script>



</body>
</html>

这是 CSS

:root{
    --Lawn: rgb(100,100,100); /* this is the variable */
  }

  path {
 fill: grey;
 cursor: pointer;
 stroke: black;
 stroke-width: 1px;
 stroke-linejoin: round;;
 }


 .selected1{
 fill: var(--Lawn);
 }

 .selected2{
  fill: var(--Lawn);
 }

 .selected3{
 fill: var(--Lawn);
}



.swatches button{
 display: inline-block;
 width: 100px;
 height:100px; 
 cursor: pointer;
 }

这是javascript

 $('#path12').on("click", function() {
 $('#path12.selected1').attr("class", "");
 $(this).attr("class", "selected1");
 });

 $('#path14').on("click", function() {
 $('#path14.selected2').attr("class", "");
 $(this).attr("class", "selected2");
 });

$('#path16').on("click", function() {
$('#path16.selected3').attr("class", "");
$(this).attr("class", "selected3");
});

    var root=document.querySelector(':root');
    var swatches = document.querySelectorAll('.swatches button');

    
    swatches.forEach((swatch)=>{
        swatch.addEventListener('click',(e)=>{
            root.style.setProperty('--Lawn',e.target.style.background);
            
        })
    })

【问题讨论】:

    标签: javascript svg


    【解决方案1】:

    selected1selected2selected3 三个类都设置为相同的东西:

    fill: var(--Lawn);
    

    所以如果你更新那个 CSS 变量,所有三个类都会改变。 var(--Lawn) 是对 --Lawn 变量的引用。它不是值的副本。

    所以使用 CSS 变量是错误的方法。请改用 JS 变量。

    另一个问题是你的 SVG 坏了。 &lt;path&gt; 元素中的属性存在问题。您是否手动编辑了文件?

     <svg>
     xmlns:dc="http://purl.org/dc/elements/1.1/"
    
    ..snip..
    
    <path class="zone1"
     id="path12" 
       fill="";stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke- 
     miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
    

    应该是这样的:

     <svg
     xmlns:dc="http://purl.org/dc/elements/1.1/"
    
    ..snip..
    
    <path class="zone1"
     id="path12"
       fill="none" 
       style="stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
    

    不管怎样,这里有一个我可以做的例子。

    // Holds the currently selected colour
    // Initialised to the background colour of the button we have marked as id="defaultColour"
    var  selectedColour = $("#defaultColour").css("backgroundColor");
    
    // Click handler for buttons
    $('.swatches button').on("click", function(event) {
      // Set selectedColour to the background colour of the button that the user clicked on
      selectedColour = $(event.target).css("backgroundColor");
    });
    
    // Click handler for SVG paths
    $('#svg2 path').on("click", function(event) {
      // Set the path's fill colour to the currently selected colur
      $(event.target).css("fill", selectedColour);
    });
    path {
      fill: grey;
      cursor: pointer;
      stroke: black;
      stroke-width: 1px;
      stroke-linejoin: round;;
    }
    
    .swatches button {
      display: inline-block;
      width: 100px;
      height:100px; 
      cursor: pointer;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="swatches">
        <button style="background: rgb(153,153,0)" id="defaultColour"></button>
        <button style="background: rgb(103,103,0)"></button>
        <button style="background: rgb(100,100,0)"></button>
        <button style="background: rgb(10,20,100)"></button>
        <button style="background: rgb(26,75,100)"></button>
    </div>
    
    <svg
     xmlns="http://www.w3.org/2000/svg"
     viewBox="0 0 793.70667 1122.52"
     height="1122.52"
     width="793.70667"
     xml:space="preserve"
     id="svg2">
     
     <g transform="matrix(1.3333333,0,0,-1.3333333,0,1122.52)" id="g10">
       <path class="zone1"
     id="path12"
         fill="none"
         style="stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
         d="M 584.447,109.821 H 17.518 V 676.75 h 566.929 z" />
       <path class="zone2"
         id="path14"
         fill="none"
         style="stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
         d="M 242.566,109.741 H 129.18 V 676.67 h 113.386 z" />
       <path class="zone3"
         id="path16"
         fill="none"
         style="stroke:#1d1d1b;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
         d="M 471.022,109.894 H 357.636 v 566.929 h 113.386 z" />
      </g>
    </svg>

    【讨论】:

    • 非常感谢!,它运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多