gpt4 book ai didi

javascript - 当我滚动 HTML 页面时,工具提示从鼠标悬停位置移得更远

转载 作者:行者123 更新时间:2023-12-04 13:28:43 25 4
gpt4 key购买 nike

我正在将 JQuery 鼠标悬停和 CSS 与 "position: absolute;" 一起使用将工具提示放置在鼠标指向的特定矩阵 (x, y) 位置旁边。
但是当我向下滚动时,工具提示会远离鼠标指针! :(
这是两张图片:

  • Before scrolling down
    enter image description here
  • After scrolling down
    enter image description here

  • 我的 fiddle : http://jsfiddle.net/kjx9z1uy/22/
    我的代码片段:

    var colours = ['#F5F5F5', '#F5F5F5', '#F5F5F5', '#f4cd42', '#F5F5F5'];
    var n = colours.length;
    var w = 15;
    var h = w * 4;

    // create an offsreen canvas containing the desired coloured circles
    var off = document.createElement('canvas');
    off.width = n * w;
    off.height = h;
    var ctx = off.getContext('2d');

    // request mousemove events
    var tipCanvas = document.getElementById("tip");
    var tipCtx = tipCanvas.getContext("2d");
    $("#canvas").mousemove(function (e) {
    handleMouseMove(e);
    });

    for (var i = 0; i < n; ++i) {
    ctx.beginPath();
    ctx.rect(i*w, 0, w, h)
    // Fill
    ctx.globalAlpha = "0.8"; // Opacity
    ctx.fillStyle = colours[i];
    ctx.fill();
    // Stroke
    ctx.globalAlpha = "1.0"; // Opacity reset
    //ctx.lineWidth = "1";
    ctx.strokeStyle = "black";
    ctx.stroke();
    }

    // get handles to the on-screen canvas
    var canvas = document.getElementById('canvas');
    var ctx2 = canvas.getContext('2d');

    // create 3000 random points
    var points = [];
    var num_boxes = Math.floor(canvas.width/w)
    * Math.floor(canvas.height/h)
    console.log("# boxes: " + num_boxes)
    for (var i = 0; i < Math.floor(canvas.width/w); ++i) {
    for (var j = 0; j < Math.floor(canvas.height/h); ++j) {
    var point_left = Math.floor(i*w);
    var point_top = Math.floor(j*h);
    var point_right = point_left + w;
    var point_bottom = point_top + h;
    points.push({
    c: i % n,
    x: point_left,
    y: point_top,
    w: w,
    h: h,
    r: point_right,
    b: point_bottom,
    text: "Data: (" + i + ", " + j + ") at (" + point_left + ", " + point_top + ", " + point_right + ", " + point_bottom + ")"
    });
    }
    }

    // copy points from the offscreen canvas into the on-page canvas
    var t0 = Date.now();
    ctx2.font = "bold 15px sans-serif";
    ctx2.fillStyle = "black";
    for (var i = 0; i < points.length; ++i) {
    var p = points[i];
    ctx2.drawImage(off, p.c * p.w, 0, p.w, p.h, p.x, p.y, p.w, p.h);
    ctx2.fillText(p.c, p.x, p.y+p.h);
    //console.log("RECT:", p.x, p.y, p.w, p.h)
    }
    var t1 = Date.now();
    console.log("Elapsed time: " + (t1 - t0) + "ms");

    // show tooltip when mouse hovers over dot
    function handleMouseMove(e) {
    mouseX = parseInt(e.clientX);
    mouseY = parseInt(e.clientY);
    var hit = false;
    var adjustY = 15 + tipCanvas.height
    for (var i = 0; i < points.length; ++i) {
    var p = points[i];
    var withinX = (mouseX >= p.x) && (mouseX < p.r);
    var withinY = (mouseY >= p.y) && (mouseY < p.b);
    if (withinX && withinY) {
    if ((mouseX + tipCanvas.width) < canvas.width) {
    tipCanvas.style.left = mouseX + "px";
    }
    else {
    if (tipCanvas.width < canvas.width) {
    tipCanvas.style.left = (mouseX - tipCanvas.width) + "px";
    }
    else {
    tipCanvas.style.left = mouseX + "px";
    }
    }
    if (mouseY > adjustY) {
    tipCanvas.style.top = (mouseY - adjustY) + "px";
    }
    else {
    tipCanvas.style.top = (mouseY + adjustY) + "px";
    }
    tipCtx.clearRect(0, 0, tipCanvas.width, tipCanvas.height);
    tipCtx.fillText(p.text, 5, 15);
    tipCtx.fillText('Mouse: (' + mouseX + ', ' + mouseY + ")", 5, 30);
    hit = true;
    }
    }
    if (!hit) {
    tipCanvas.style.left = 0 - tipCanvas.width - 40 + "px";
    }
    }
    #tip {
    background-color: white;
    border: 1px solid blue;
    position: absolute;
    left: -200px;
    top: -100px;
    }
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <canvas id="tip" width=200 height=40></canvas>
    <canvas id="canvas" width="1000" height="15000"> </canvas>

    最佳答案

    因为您没有添加滚动高度。
    在 handleMouseMove() 函数中,我将 window.scrollY 添加到 Y 轴值。

    var colours = ['#F5F5F5', '#F5F5F5', '#F5F5F5', '#f4cd42', '#F5F5F5'];
    var n = colours.length;
    var w = 15;
    var h = w * 4;

    // create an offsreen canvas containing the desired coloured circles
    var off = document.createElement('canvas');
    off.width = n * w;
    off.height = h;
    var ctx = off.getContext('2d');

    // request mousemove events
    var tipCanvas = document.getElementById("tip");
    var tipCtx = tipCanvas.getContext("2d");
    $("#canvas").mousemove(function (e) {
    handleMouseMove(e);
    });

    for (var i = 0; i < n; ++i) {
    ctx.beginPath();
    ctx.rect(i*w, 0, w, h)
    // Fill
    ctx.globalAlpha = "0.8"; // Opacity
    ctx.fillStyle = colours[i];
    ctx.fill();
    // Stroke
    ctx.globalAlpha = "1.0"; // Opacity reset
    //ctx.lineWidth = "1";
    ctx.strokeStyle = "black";
    ctx.stroke();
    }

    // get handles to the on-screen canvas
    var canvas = document.getElementById('canvas');
    var ctx2 = canvas.getContext('2d');

    // create 3000 random points
    var points = [];
    var num_boxes = Math.floor(canvas.width/w)
    * Math.floor(canvas.height/h)
    console.log("# boxes: " + num_boxes)
    for (var i = 0; i < Math.floor(canvas.width/w); ++i) {
    for (var j = 0; j < Math.floor(canvas.height/h); ++j) {
    var point_left = Math.floor(i*w);
    var point_top = Math.floor(j*h);
    var point_right = point_left + w;
    var point_bottom = point_top + h;
    points.push({
    c: i % n,
    x: point_left,
    y: point_top,
    w: w,
    h: h,
    r: point_right,
    b: point_bottom,
    text: "Data: (" + i + ", " + j + ") at (" + point_left + ", " + point_top + ", " + point_right + ", " + point_bottom + ")"
    });
    }
    }

    // copy points from the offscreen canvas into the on-page canvas
    var t0 = Date.now();
    ctx2.font = "bold 15px sans-serif";
    ctx2.fillStyle = "black";
    for (var i = 0; i < points.length; ++i) {
    var p = points[i];
    ctx2.drawImage(off, p.c * p.w, 0, p.w, p.h, p.x, p.y, p.w, p.h);
    ctx2.fillText(p.c, p.x, p.y+p.h);
    //console.log("RECT:", p.x, p.y, p.w, p.h)
    }
    var t1 = Date.now();
    console.log("Elapsed time: " + (t1 - t0) + "ms");

    // show tooltip when mouse hovers over dot
    function handleMouseMove(e) {
    mouseX = parseInt(e.clientX);
    mouseY = parseInt(e.clientY);
    var hit = false;
    var adjustY = 15 + tipCanvas.height;
    var scrollY = window.scrollY;
    for (var i = 0; i < points.length; ++i) {
    var p = points[i];
    var withinX = (mouseX >= p.x) && (mouseX < p.r);
    var withinY = (mouseY >= p.y) && (mouseY < p.b);
    if (withinX && withinY) {
    if ((mouseX + tipCanvas.width) < canvas.width) {
    tipCanvas.style.left = mouseX + "px";
    }
    else {
    if (tipCanvas.width < canvas.width) {
    tipCanvas.style.left = (mouseX - tipCanvas.width) + "px";
    }
    else {
    tipCanvas.style.left = mouseX + "px";
    }
    }
    if (mouseY > adjustY) {
    tipCanvas.style.top = (mouseY - adjustY + scrollY) + "px";
    }
    else {
    tipCanvas.style.top = (mouseY + adjustY + scrollY) + "px";
    }
    tipCtx.clearRect(0, 0, tipCanvas.width, tipCanvas.height);
    tipCtx.fillText(p.text, 5, 15);
    tipCtx.fillText('Mouse: (' + mouseX + ', ' + mouseY + ")", 5, 30);
    hit = true;
    }
    }
    if (!hit) {
    tipCanvas.style.left = 0 - tipCanvas.width - 40 + "px";
    }
    }
    #tip {
    background-color: white;
    border: 1px solid blue;
    position: absolute;
    left: -200px;
    top: -100px;
    }
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <canvas id="tip" width=200 height=40></canvas>
    <canvas id="canvas" width="1000" height="15000"> </canvas>

    关于javascript - 当我滚动 HTML 页面时,工具提示从鼠标悬停位置移得更远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66523191/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com