简体   繁体   English

如何根据单独的 HTML 元素的 hover 更改 SVG 元素的填充?

[英]How to change fill of an SVG element based on hover of an separate HTML element?

I have a simple SVG graphic (basically 4 squares) in one div.我在一个 div 中有一个简单的 SVG 图形(基本上是 4 个正方形)。 Next to that div is another div with 4 HTML buttons.在那个 div 旁边是另一个带有 4 个 HTML 按钮的 div。 I want that on hover of button 1, one of the SVG squares changes its background color.我希望在按钮 1 的 hover 上,SVG 方块之一更改其背景颜色。 And on hover of button 2, another SVG square changes its background color, same for the next 2 buttons and SVG squares.在按钮 2 的 hover 上,另一个 SVG 方块改变了它的背景颜色,接下来的 2 个按钮和 SVG 方块也是如此。 (and when hovering out of the respective button, the SVG square goes back to no fill.) (当鼠标悬停在相应的按钮外时,SVG 方块变回无填充状态。)

I have been researching this a lot but have not found a solution.我一直在研究这个问题,但还没有找到解决方案。 From what I am understanding I can't do this with CSS but is this possible with jQuery or javascript?据我了解,我无法使用 CSS 执行此操作,但可以使用 jQuery 或 javascript 执行此操作吗?

For context;对于上下文; I am trying to understand how an interactive graphic like on this page is working: https://the-jay.ch/wohnungen (scroll down to "WOHNUNGSANGEBOT")我试图了解此页面上的交互式图形是如何工作的: https://the-jay.ch/wohnungen (向下滚动到“WOHNUNGSANGEBOT”)

 .cls-1 { fill: none; stroke: #231f20; stroke-miterlimit: 10; stroke-width: 35px; }.svg_section { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 100%; height: 500px; }.svg_wrapper { overflow: hidden; width: 50%; height: 100%; }.html-embed { width: 100%; height: 100%; }.svg_trigger_div { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 50%; height: 100%; -webkit-box-orient: vertical; -webkit-box-direction: normal; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #e4b5b5; }.button1 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #973535; }.button2 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #5aac6c; }.button3 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #559bbb; }.button4 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: rgba(162, 71, 153, 0.72); }.text-block { line-height: 14px; }
 <div class="svg_section"> <div class="svg_wrapper"> <div class="html-embed w-embed"> <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 1035 1035.92"> <defs></defs> <rect id="rec4" class="cls-1" x="17.5" y="17.5" width="500" height="500"></rect> <rect id="rec3" class="cls-1" x="517.5" y="17.5" width="500" height="500"></rect> <rect id="rec2" class="cls-1" x="17.5" y="518.42" width="500" height="500"></rect> <rect id="rec1" class="cls-1" x="517.5" y="518.42" width="500" height="500"></rect> </svg> </div> </div> <div class="svg_trigger_div"> <a href="#" class="button1 w-inline-block"> <div class="text-block">Button 1</div> </a> <a href="#" class="button2 w-inline-block"> <div class="text-block">Button 2</div> </a> <a href="#" class="button3 w-inline-block"> <div class="text-block">Button 3</div> </a> <a href="#" class="button4 w-inline-block"> <div class="text-block">Button 4</div> </a> </div> </div>

The way you coded it, you can't just by using css. You'll need to use Javascript with a MouseOver or a MouseEnter EventListener你编码的方式,你不能只使用 css。你需要使用 Javascript 和 MouseOver 或 MouseEnter EventListener

 var allClasses = [], theSVG = null; function changeSvgClasslist(add){ if( theSVG ){ allClasses.forEach( oneClass => { theSVG.classList.remove(oneClass); } ) if(add) theSVG.classList.add(add); console.log(theSVG.classList); } } function btnOver(evt){ let btnClass = evt.target.getAttribute('data-class'); changeSvgClasslist(btnClass); } function btnOut(evt){ changeSvgClasslist(null); } document.addEventListener('DOMContentLoaded', () => { theSVG = document.querySelector('#Layer_1'); let theButtons = document.querySelectorAll('#svg_trigger_div.w-inline-block'); console.log("I see %d buttons.", theButtons.length); theButtons.forEach( (btn,idx) => { let curClass = 'btnHover'+ (idx+1); allClasses.push( curClass ); btn.setAttribute('data-class', curClass); btn.addEventListener('mouseenter', btnOver); btn.addEventListener('mouseleave', btnOut); } ) });
 .cls-1{ fill:none; stroke:#231f20; stroke-miterlimit:10; stroke-width:35px; }.svg_section { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 100%; height: 500px; }.svg_wrapper { overflow: hidden; width: 50%; height: 100%; }.html-embed { width: 100%; height: 100%; } #svg_trigger_div { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 50%; height: 100%; -webkit-box-orient: vertical; -webkit-box-direction: normal; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #e4b5b5; }.button1 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #973535; }.button2 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #5aac6c; }.button3 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: #559bbb; }.button4 { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; width: 200px; height: 30px; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; background-color: rgba(162, 71, 153, 0.72); }.text-block { line-height: 14px; }.btnHover1 #rec1{ fill: red; }.btnHover2 #rec2{ fill: red; }.btnHover3 #rec3{ fill: red; }.btnHover4 #rec4{ fill: red; }
 <div class="svg_section"> <div class="svg_wrapper"> <div class="html-embed w-embed"><svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 1035 1035.92"> <defs></defs> <rect id="rec4" class="cls-1" x="17.5" y="17.5" width="500" height="500"></rect> <rect id="rec3" class="cls-1" x="517.5" y="17.5" width="500" height="500"></rect> <rect id="rec2" class="cls-1" x="17.5" y="518.42" width="500" height="500"></rect> <rect id="rec1" class="cls-1" x="517.5" y="518.42" width="500" height="500"></rect> </svg></div> </div> <div id="svg_trigger_div"> <a href="#" class="button1 w-inline-block"> <div class="text-block">Button 1</div> </a> <a href="#" class="button2 w-inline-block"> <div class="text-block">Button 2</div> </a> <a href="#" class="button3 w-inline-block"> <div class="text-block">Button 3</div> </a> <a href="#" class="button4 w-inline-block"> <div class="text-block">Button 4</div> </a> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM