I've a page structure like this:
<body>
<svg>...<svg>
<div><!-- font-awsome circle is here with id='node' --></div>
</body>
These div
is positioned absolute with highest z-index thus they are floating over svg image. I want to print console.log(something) when mouse over the circle.
What I've tried:
$('#node').hover(()=>{console.log('mouse in')}, ()=>{console.log('mouse out')})
<!-- not working -->
$('#node').mouseenter(()=>{console.log('mouse in')}).mouseleave(()=>{console.log('mouse out')})
<!-- not working -->
let test = document.getElementById('node');
test.addEventListener(
"mouseenter",
function (event) {
console.log("mouse enter node");
},
false
);
<!-- Not working -->
Note that these syntax are working with all other elements on the page except than those lying in or over svg portion(I've tested on a dummy button outside svg on which it was working)
Looking for some help...
Update 2:(More detailed) The below is a short version of code
<!-- graph.html -->
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./graphStyles.css" />
<!--FontAwsome-->
<script
src="https://kit.fontawesome.com/e51e1b526a.js"
crossorigin="anonymous"
></script>
<title>graph</title>
</head>
<body>
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
class="graph"
></svg>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="graph.js" charset="utf-8"></script>
</body>
</html>
// graph.js
function createNode() {
$("body").append(
`<div id=C1 class="fa-stack graph-node fa-1.4rem">
<i class="fas fa-circle fa-stack-2x"></i>
<p class="fas fa-stack-1x">C1</p>
</div>`
);
$(".graph").append(`<line id="l1-l2" class="graph-edge" ></line>`);
$("#l1-l2").attr("x1", 100).attr("y1", 0).attr("x2", 200).attr("y2", 200);
/* Code-Previous: If kept before label: mylabel
$(`#C1`).hover(
() => {
console.log(`mouse in`);
},
() => {
console.log(`mouse out`);
}
);
*/
$("body").html($("body").html()); // label: mylabel
// Code-Later: If kept after label: mylabel
$(`#C1`).hover(
() => {
console.log(`mouse in`);
},
() => {
console.log(`mouse out`);
}
);
}
$(document).ready(createNode);
/* graphStyles.css */
body {
background-color: #32c6f8;
position: relative;
padding: 0;
margin: 0;
}
svg {
background-color: #32c6f8;
position: absolute;
left: 0;
top: 0;
margin: 0;
width: 100%;
height: 100%;
/* pointer-events: none !important; */
}
.graph-node {
color: white;
display: inline-block;
font-size: 2rem;
position: absolute;
z-index: 1000;
}
.graph-node p {
color: black;
font-family: monospace;
font-weight: 900;
font-size: 1.5rem;
width: 60%;
left: 0;
right: 0;
margin: auto;
}
.graph-edge {
position: absolute;
stroke: white;
stroke-width: 0.5rem;
z-index: 100;
}
In graph.js
there are two sections named as Code-Previous
and Code-Later
. The mouse hover event is working as expected in the shown situation of graph.js
. However if I remove the Code-Later
and enable the Code-Previous
, hover event doesn't works. Also, I can't remove $("body").html($("body").html());
otherwise svg-line inserted in ".graph" will not render.
Why such behaviour is happening with?