Vanilla JS悬停在SVG路径上以显示DIV?

时间:2018-11-01 00:43:28

标签: javascript html svg

我正在尝试寻找一种方法来将SVG悬停在div上,但是似乎使用SVG与使用其他元素有点不同。我已经通过使用一些CSS:hover事件并将它们与JS连接进行了尝试,但这没有用。我也需要为此项目避免使用jQuery。我将包括我的HTML和JS以供参考。

HTML

<svg viewBox="0 40 100 40">
  <defs><path id="textPath"/></defs>
  <g id="testGroupC" >
       <style type="text/css">
        <![CDATA[
          @import url('https://fonts.googleapis.com/css?family=Raleway:thin');
          text{
            font-size: 4pt;
              fill: #ffffff;
              text-anchor: middle;
              font-family: 'Raleway';
              z-index: 2;
          }
         ]]> 
  </style>
      <path onclick="window.location='index.html';" id="testPath"/>
       <text>
         <textpath xlink:href="#textPath" startOffset="53%" >
            <tspan x="0" y="-5">Home</tspan>
      </textpath>
    </text>   
  </g>
</svg>

<div id="showMe"></div>

JS

const rad = Math.PI / 180;

let cx = 50, cy = 100, R = 50, r = 35, A = 40 , a = 5, o=4;
// o for offset
testGroupC2.setAttributeNS(null, "transform", `rotate(${-90 -(A / 2) - a} ${cx} ${cy})`)


// control points for the quadratic Bézier
let px1 = cx + R * Math.cos(0);
let py1 = cy + R * Math.sin(0);
let px2 = cx + R * Math.cos((2*a + A)*rad);
let py2 = cy + R * Math.sin((2*a + A)*rad);
let px3 = cx + r * Math.cos((2*a + A)*rad);
let py3 = cy + r * Math.sin((2*a + A)*rad);
let px4 = cx + r * Math.cos(0);
let py4 = cy + r * Math.sin(0);

// points used to draw the shape
let x11 = cx + (R-o) * Math.cos(0);
let y11 = cy + (R-o) * Math.sin(0);

let x1 = cx + R * Math.cos(a*rad);
let y1 = cy + R * Math.sin(a*rad);

let x2 = cx + R * Math.cos((a + A)*rad);
let y2 = cy + R * Math.sin((a + A)*rad);

let x21 = cx + (R-o) * Math.cos((2*a + A)*rad);
let y21 = cy + (R-o) * Math.sin((2*a + A)*rad);

let x31 = cx + (r+o) * Math.cos((2*a + A)*rad);
let y31 = cy + (r+o) * Math.sin((2*a + A)*rad);

let x3 = cx + r * Math.cos((a + A)*rad);
let y3 = cy + r * Math.sin((a + A)*rad);

let x4 = cx + r * Math.cos(a*rad);
let y4 = cy + r * Math.sin(a*rad);

let x41 = cx + (r+o) * Math.cos(0);
let y41 = cy + (r+o) * Math.sin(0);

/*
No rounded corners
let d = `M${x1},${y1} A${R},${R},0 0,1 ${x2},${y2}
         L${x3},${y3} A${r},${r},0 0,0 ${x4},${y4}
         L${x1},${y1}Z`;*/

/*
Beveled corners
let d = `M${x1},${y1} 
         A${R},${R},0 0,1 ${x2},${y2}
         L${x21},${y21} 
         L${x31},${y31}
         L${x3},${y3}
         A${r},${r},0 0,0 ${x4},${y4}
         L${x41},${y41}
         L${x11},${y11}
         L${x1},${y1}Z`;*/

// Rounded corners with quadratic Bézier curves
    d = `M${x1},${y1} 
         A${R},${R},0 0,1 ${x2},${y2}
         Q${px2},${py2} ${x21},${y21} 
         L${x31},${y31}
         Q${px3},${py3} ${x3},${y3}
         A${r},${r},0 0,0 ${x4},${y4}
         Q${px4},${py4} ${x41},${y41}
         L${x11},${y11}
         Q${px1},${py1} ${x1},${y1}Z`;

testPath2.setAttributeNS(null,"d",d);

1 个答案:

答案 0 :(得分:1)

好吧,我想您正在尝试做类似的事情:

const div = document.querySelector('#message');

const svg = document.querySelector('svg');

const showMessage = () => {
	div.style.display = 'block';
}

const hideMessage = () => {
	div.style.display = 'none';
}

svg.addEventListener('mouseover', showMessage);

svg.addEventListener('mouseout', hideMessage);
#message {
  display: none;
}
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

<div id="message">
Hello!!
</div>