1

如何使 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>
4

1 回答 1

2

Before mouseover and mouseout, clear div(container) Tag.

    <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: "container", width: 578, height: 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() {
                    document.getElementById("container").innerHTML = "";
                    drawCircle(nx, ny, nradius, scolor, nstroke, nstrokeWidth);
                });
            }
            function drawCircle(sx, sy, sradius, sfill, sstroke, sstrokeWidth) {
                var stage = new Kinetic.Stage({ container: "container", width: 578, height: 200 });
                var circleLayer = new Kinetic.Layer();
                //circle
                var scircle = new Kinetic.Circle({
                    x: sx,
                    y: sy,
                    radius: sradius,
                    fill: sfill,
                    stroke: sstroke,
                    strokeWidth: sstrokeWidth
                });
                scircle.on("mouseover", function() {
                    document.getElementById("container").innerHTML = "";
                    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>

OR use mouseout event in same function

<head>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.0.0.js"></script>
    <script>
        var scolor = "green"
        var ncolor = "red"

        function drawCircle(sx, sy, sradius, sfill, sstroke, sstrokeWidth) {
            var stage = new Kinetic.Stage({ container: "container", width: 578, height: 200 });
            var circleLayer = new Kinetic.Layer();
            //circle
            var scircle = new Kinetic.Circle({
                x: sx,
                y: sy,
                radius: sradius,
                fill: sfill,
                stroke: sstroke,
                strokeWidth: sstrokeWidth
            });
            scircle.on("mouseover", function() {
                scircle.attrs.fill = "red";
                circleLayer.draw();
            });
            scircle.on("mouseout", function() {
                scircle.attrs.fill = "green";
                circleLayer.draw();
            });
            circleLayer.add(scircle);
            stage.add(circleLayer);
        }
        window.onload = function() {
            drawCircle(200, 100, 50, "green", "black", 3);
        };
    </script>
</head>
<body>
    <div id="container">
    </div>
</body>
于 2012-08-24T14:31:10.210 回答