我试图在悬停按钮时触发鼠标悬停更改以填充svg圆圈背景颜色。我无法做到这一点。
.position-orange-circle { position: absolute; margin-top: 100px; }
.position-btn { background-color: #ffbb11; height: 41px; width: 130px; border: none; border-radius: 8px; font-size: 1em; font-weight: 600; cursor: pointer; -webkit-transition-duration: 0.4s; transition-duration: 0.4s;}
.position-btn:hover svg circle { background-color: #231f20 !important; color: #ffbb11 !important; border: #9c7002 solid 1px; fill: #ffbb11;}
<svg height="150" width="150" class="position-orange-circle">
<circle cx="60" cy="60" r="50" stroke="#ffbb11" stroke-width="5" fill="white"/>
</svg>
<div class="btn-position">
<button class="position-btn">Apply now</button>
</div>
答案 0 :(得分:1)
您无法使用CSS向上浏览DOM。您只能访问将来的兄弟姐妹和孩子。
文档:https://www.w3schools.com/css/css_combinators.asp
.position-orange-circle { position: absolute; margin-top: 100px; left: 15px; }
.position-btn { background-color: #ffbb11; height: 41px; width: 130px; border: none; border-radius: 8px; font-size: 1em; font-weight: 600; cursor: pointer; -webkit-transition-duration: 0.4s; transition-duration: 0.4s;}
.position-btn:hover ~ .position-orange-circle > circle{ background-color: #231f20 !important; color: #ffbb11 !important; border: #9c7002 solid 1px; fill: #ffbb11;}
<div class="btn-position">
<button class="position-btn">Apply now</button>
<svg height="150" width="150" class="position-orange-circle">
<circle cx="60" cy="60" r="50" stroke="#ffbb11" stroke-width="5" fill="white"/>
</svg>
</div>
答案 1 :(得分:1)
这是仅CSS的解决方案。该结构非常不常规,因为SVG嵌套在按钮内部。它符合您的描述,但可能不适合您的用例:
<style>
svg {
position: absolute;
stroke: #ffbb11;
fill: none;
pointer-events: none;
}
button:hover svg {
fill: #ffbb11;
}
</style>
<button>
Hover
<svg width="200" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40"/>
</svg>
</button>
答案 2 :(得分:0)
根据@obsidian的回答,可能是这样的:
function chbg(color) {
document.getElementById(id).style.backgroundColor = color;
document.getElementById(id).style.backgroundColor.setAttribute("fill", "red")
}
更多chbg(color, id, td, fc)
function chbg(color, id, td, fc) {
document.getElementById(id).style.backgroundColor = color;
document.getElementById(id).style.textDecoration = td;
document.getElementById(id).style.color = fc;
}
答案 3 :(得分:0)
这可能对您有帮助!
function chbg(color) {
document.getElementById("whiteCircle").setAttribute("fill", color);
}
.position-orange-circle { position: absolute; margin-top: 100px; }
.position-btn { background-color: #ffbb11; height: 41px; width: 130px; border: none; border-radius: 8px; font-size: 1em; font-weight: 600; cursor: pointer; -webkit-transition-duration: 0.4s; transition-duration: 0.4s;}
.position-btn:hover svg circle { background-color: #231f20 !important; color: #ffbb11 !important; border: #9c7002 solid 1px; fill: #ffbb11;}
<svg height="150" width="150" class="position-orange-circle">
<circle id="whiteCircle" cx="60" cy="60" r="50" stroke="#ffbb11" stroke-width="5" fill="white"/>
</svg>
<div class="btn-position">
<button class="position-btn" onmouseover="chbg('red')" onmouseout="chbg('white')">Apply now</button>
</div>