1 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
三個(gè)類selected1,selected2和selected3都設(shè)置為相同的內(nèi)容:
fill: var(--Lawn);
因此,如果您更新該 CSS 變量,所有三個(gè)類都會(huì)發(fā)生變化。 var(--Lawn)是對變量的引用--Lawn。它不是值的副本。
所以使用 CSS 變量是錯(cuò)誤的做法。請改用 JS 變量。
另一個(gè)問題是您的 SVG 已損壞。您的元素中的屬性有問題<path>。您是否手動(dòng)編輯了該文件?
<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"
應(yīng)該是這樣的:
<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"
無論如何,這是我如何做到這一點(diǎn)的一個(gè)例子。
// 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>
添加回答
舉報(bào)