javascript - 如何在 html 5 中创建鼠标悬停高亮框?

标签 javascript html

如果我有一个 600 x 400 的网格,其中有 10 x 10 像素的正方形,如下所示:

/**
* draws grid to screen
*/
function drawgrid(context)
{
    for(var x = 0.5; x < 600; x += 10)
    {
        context.moveTo(x, 0);
        context.lineTo(x, 400);
    }

    for(var y = 0.5; y < 400; y += 10)
    {
        context.moveTo(0, y);
        context.lineTo(600, y);
    }

    context.strokeStyle = "#eee";
    context.stroke();
}


/**
* Creates a canvas element, loads images, adds events, and draws the canvas for the first time.
*/
function prepareCanvas()
{
        var context = document.getElementById('canvas').getContext("2d");

        drawgrid(context);

}

我如何将鼠标悬停在各个方 block 上并突出显示鼠标悬停的方 block 。就像制作一个红色框来突出显示鼠标悬停的网格正方形。

最佳答案

请参阅下面的代码。调整事件坐标

<body>
      <canvas id=canvas height=400 width=600 
         onmousemove="over()" style="cursor:crosshair">
      </canvas>
</body>
<script type="text/javascript">
<!--

    var grid;
    function prepareCanvasGrid()
    {
        var cnv = document.getElementById('canvas');
        grid = new CanvasGrid(cnv.getContext("2d"),cnv.offsetLeft, cnv.offsetTop);
    }

    function CanvasGrid(context,x,y) {
        this.sq = [];
        this.dirty = [];
        this.ctx = context;
        this.x = x;
        this.y = y;
        this.init = function(){
            for(var x = 0.5; x < 600; x += 50) {
                for(var y = 0.5; y < 400; y += 50) {
                    var s = new square(x,y, context);
                    this.sq.push(s);
                }
            }
        }

        this.draw = function(){
            this.ctx.clearRect(0,0,600,400);
            for(var i=0; i < this.sq.length; i++)
                this.sq[i].draw();
         }

        this.clean = function(){
            for(var i=0; i < this.dirty.length; i++)
                this.dirty[i].draw();
            this.dirty = [];
        }

        this.over = function(ex,ey){
            ex = ex - this.x;
            ey = ey - this.y;
            for(var i=0; i < this.sq.length; i++) {
                if(this.sq[i].eleAtPoint(ex,ey)){
                    this.clean(); // clean up
                    this.dirty.push(this.sq[i]);
                    this.sq[i].over();
                    break;
                }
             }
        }

        this.init();
        this.draw();
    }

    function square(x,y, ctx){
        this.ctx = ctx;
        this.x = x;
        this.y = y;
        this.h = 50;
        this.w = 50;

        this.draw = function(){
            this.ctx.strokeStyle = "#eee";
            this.ctx.strokeRect(this.x, this.y, this.w, this.w);
            this.ctx.fillStyle = "#fff";
            this.ctx.fillRect(this.x, this.y, this.w, this.w);
        }

        this.over = function() {
            this.ctx.fillStyle = "red";
            this.ctx.fillRect(this.x, this.y, this.w, this.w);
        }

        this.eleAtPoint = function(ex,ey){
            if(ex < this.x + this.w && ex > this.x 
                && ey > this.y && ey < this.y + this.h) 
                return true;
            return false;
        }
    }

    function over(){
        var e = window.event;
        grid.over(e.clientX  ,e.clientY);
    }

    prepareCanvasGrid();
     //-->

</script>

更新代码以获得更好的性能

关于javascript - 如何在 html 5 中创建鼠标悬停高亮框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7583482/

相关文章:

javascript - 为什么放置图像时 Canvas 中没有显示任何内容?

javascript - 如何用 ExtJS 监听单选按钮是否被选中?

javascript - 从 jQuery Guillotine 插件获取数据

javascript - 如何在 div 的每一侧创建 handle 以调整大小? -(没有 jquery)

html - 如何在我的 html 网站上安装自定义字体

php - AJAX 与 HTML?

javascript - 如何从静态 html 网页表单发送 javascript https post 或 webhook?

javascript - 在 React 的 render 方法中设置 CSS 变量是个好习惯吗?

javascript - 如何减少这个脚本

javascript - 为什么使用 Node JS 创建 REST API 和 MVC Web 应用程序