如何使 JavaScript 中的鼠标悬停功能减少资源密集型?我使用了一种方法,其中一个功能激活另一个(鼠标悬停),这个功能激活第一个(鼠标移出)。我使用了 Kinetic.js,但我也希望有另一个库的解决方案。
我想稍后将它与鼠标悬停功能的其他选项一起使用。
我的代码:
<head>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.0.0.js"></script>
<script>
var scolor = "green"
var ncolor = "red"
function changeCircle (circleLayer, nx, ny, nradius, nfill, nstroke, nstrokeWidth) {
var stage = new Kinetic.Stage("container", 578, 200);
var context = circleLayer.getContext();
circleLayer.clear();
var ncircle = new Kinetic.Circle({
x: nx ,
y: ny ,
radius: nradius ,
fill: nfill ,
stroke: nstroke ,
strokeWidth: nstrokeWidth
});
circleLayer.add(ncircle);
stage.add(circleLayer);
ncircle.on("mouseout", function(){
drawCircle (nx, ny, nradius, scolor, nstroke, nstrokeWidth);
});
}
function drawCircle(sx, sy, sradius, sfill, sstroke, sstrokeWidth) {
var stage = new Kinetic.Stage("container", 578, 200);
var circleLayer = new Kinetic.Layer();
circleLayer.clear();
//circle
var scircle = new Kinetic.Circle({
x: sx ,
y: sy ,
radius: sradius ,
fill: sfill ,
stroke: sstroke ,
strokeWidth: sstrokeWidth
});
scircle.on("mouseover", function() {
changeCircle(circleLayer, sx, sy, sradius, "red", sstroke, sstrokeWidth);
});
circleLayer.add(scircle);
stage.add(circleLayer);
}
window.onload = function(){
drawCircle (200, 100, 50, "green", "black", 3);
};
</script>
</head>
<body>
<div id="container">
</div>
</body>