【问题标题】:Div centered on cursor, acting as cursor for image-hover-zoom effectDiv 以光标为中心,作为图像悬停缩放效果的光标
【发布时间】:2016-09-30 21:09:03
【问题描述】:

所以我有一个目标 div(大矩形)作为目标图像。您将鼠标悬停在此上方,您会看到一个正方形,该正方形将光标替换为同一图像的更大/更高分辨率版本的一部分。

我已经向下跟踪,并且我已经偏移,以便 div 以光标位置为中心,但问题是,目标 div 不能再被拾取,直到光标离开覆盖层(超出方)。

我该如何解决这个问题?是否可以将光标替换为 div 并仍然使用光标?

我在想只使用大矩形的左上角坐标然后离开那个……我还是不知道。例如,您可以用画布对象替换光标吗?我可能仍然面临未检测到目标 div 的问题。我尝试了不起作用的 z-index。

编辑:我可能只是隐藏光标并转到覆盖方块的左角,这样看起来你就在你应该指向的任何地方的中心。

<script type="text/javascript">
      // declare variables
      var target   = document.getElementById("target-area"),// $("#target-area") doesn't work?
          cursWind = $("#cursor-window"),
          prevX    = 0,
          prevY    = 0,
          cursX    = 0,
          cursY    = 0;
      // tracking
      // client is using a mouse
      target.addEventListener("mouseover", function( event ) {
        // run the tracking only while mouse is over the sensor range area
        target.addEventListener("mousemove", function ( event ) {

          /* when the mouse goes over target, a window with the center
           * at the cursor's position, should appear, this follows
             the cursor */


          /* cursor coordinates */
          // get cursor's current X and Y coordinates
          cursX = event.clientX;
          cursY = event.clientY;
          var coordString = cursX + " x" + "," + cursY + " y";
          // empty cursor-coord-disp
          $("#cursor-coord-disp").empty();
          // append X and Y coordinates to cursor-coord-disp
          $("#cursor-coord-disp").append(coordString);

          /* see if cursor is increasing or decreasing in x/y coordinates */
          // x-coord
          function checkX() {
            if (cursX > prevX) {
              prevX = cursX; // update old x-coord
              return ((-1) * cursX);
            }
            else {
              prevX = cursX; // update old x-coord
              return cursX;
            }
          }

          function checkY() {
            if (cursY > prevY) {
              prevY = cursY; // update old x-coord
              return ((-1) * cursY);
            }
            else {
              prevY = cursY; // update old x-coord
              return cursY;
            }
          }

          /* window replaces and follows cursor position */
          // get X and Y top-left coordinates of target-area
          var targPos = $("#target-area").position(),
              targX   = targPos.left,
              targY   = targPos.top;
          // console.log(targX + " vs " + targY);
          // show the hovering window that follows cursor
          $("#cursor-window").show();
          // reposition to cursor with offset
          // console.log(checkX());
          var newXCoord   = (event.clientX),
              newYCoord   = (event.clientY);
          console.log(newXCoord + ' vs ' + newYCoord);
          // translate to cursor position
          $("#cursor-window").css({
            'top'  : newYCoord - 150,
            'left' : newXCoord - 150
          });

        }, false);
      }, false);
    </script>

【问题讨论】:

  • 您不想使用现有的实现吗?例如redopop.com/loupe
  • @YuriGor 你说得有道理。我只是想完成这件事。我可能会这样做。我开始接近画布。我只是不完全理解它。

标签: javascript


【解决方案1】:

您可以尝试使用以下代码和标记。位置的计算方法是从页面中的鼠标光标位置减去目标 div 的位置。光标窗口仅在鼠标位于目标 div 范围内时可见。

var $target = $("#target-area"),
    $cursorWindow = $("#cursor-window"),
    $coordsDisplay = $("#cursor-coord-disp");

var zoomFactor = 2;

// Copy the background image to the zoom window
$cursorWindow.css('background-image', $target.css('background-image'));
$cursorWindow.css('background-repeat', $target.css('background-repeat'));

$target.mousemove(function (e) {
    var $targetPosition = $target.position();
    var cursX = e.pageX - $targetPosition.left;
    var cursY = e.pageY - $targetPosition.top;
    var imgX, imgY, imgW, imgH;

    if (0 <= cursX && cursX <= $target.outerWidth() && 0 <= cursY && cursY <= $target.outerHeight()) {
        $cursorWindow.css({
            'left': cursX - $cursorWindow.outerWidth() / 2,
            'top': cursY - $cursorWindow.outerHeight() / 2
        });
        $cursorWindow.show();
        $coordsDisplay.text("x: " + cursX.toFixed(0) + ", y: " + cursY.toFixed(0));

        imgX = -(cursX * zoomFactor) + $cursorWindow.innerWidth() / 2;
        imgY = -(cursY * zoomFactor) + $cursorWindow.innerHeight() / 2;

        imgW = $target.innerWidth() * zoomFactor;
        imgH = $target.innerHeight() * zoomFactor;

        // Change the position and size of the image in the zoom window
        // to show a magnified view of the image content under the cursor
        $cursorWindow.css('background-position', imgX.toFixed(0) + 'px ' + imgY.toFixed(0) + 'px');
        $cursorWindow.css('background-size', imgW.toFixed(0) + 'px ' + imgH.toFixed(0) + 'px');
    } else {
        $cursorWindow.hide();
        $coordsDisplay.text("");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cursor-coord-disp" style="width: 140px; height: 24px; border: solid 1px black;"></div>
<div id="target-area" style="cursor: none; position: relative; left: 0px; top: 10px; width: 320px; height: 240px; background: url('http://loremflickr.com/320/240/dog') no-repeat; border: solid 1px black; ">
    <div id="cursor-window" style="display: none; cursor: none; position: absolute; left: 0px; top: 80px; width: 100px; height: 100px; border: solid 1px black;"></div>
</div>

【讨论】:

  • 感谢您的帮助。使用画布实际上更容易,它可以像我预期的那样工作,但使用 div 并没有那么多。
  • 使用画布可能更容易。为了完整起见,我在我的代码 sn-p 中添加了实际的图像缩放。您可以看到它如何与 div 一起使用。
  • 顺便说一下,每次我们运行代码 sn -p 时都会加载不同的图像。我担心缩放窗口中的图像可能与目标 div 中的图像不同,但我在测试中没有看到这个问题。
  • 哇,你真的做到了。惊人的。你用切片了吗?我将不得不分析你的代码。我选择了放大镜,一旦我弄清楚如何使用它,现在我的问题是我无法重置缩放图像的来源。甚至剖析了代码并手动重置了图像属性 src。嗯
  • 我修复了在更改源时遇到的放大镜问题,我必须针对放大镜类,然后是 img 子对象,然后在那里更改源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2012-04-11
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2016-03-31
相关资源
最近更新 更多