Кнопка мыши и триггеры SVG круга - PullRequest
0 голосов
/ 11 сентября 2018

JSFIDDLE

Я пытаюсь вызвать изменение при наведении курсора мыши, чтобы заполнить цвет фона 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>

				

Ответы [ 4 ]

0 голосов
/ 12 сентября 2018

Вот решение только для 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>

См. https://codepen.io/MSCAU/pen/zJWaEa

0 голосов
/ 12 сентября 2018

Вы не можете перемещаться вверх по DOM с помощью CSS.Вы можете получить доступ только к будущим братьям и сестрам и детям.

Документация: 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>
0 голосов
/ 12 сентября 2018

Это может помочь вам!

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>

			

https://jsfiddle.net/mzb12pqs/

0 голосов
/ 12 сентября 2018

Согласно ответу @ 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;
}

Тест

...