代码之家  ›  专栏  ›  技术社区  ›  Mr. Pyramid

自定义光标不在超链接上工作

  •  0
  • Mr. Pyramid  · 技术社区  · 8 年前

    我正在制作一个网站,在那里我需要一个自定义光标来代替我创建的默认光标,它正在检测鼠标移动,但当我尝试将其放置在超链接上时,什么都没有发生。这是我的密码

    HTML

    <div class='cursor'></div>
    <div class='cursor'></div>
    <h1>Custom cursor</h1>
    <a href="#">A link </a>
    

    CSS

    * {
      cursor: none;
    }
    .cursor {
      position: absolute;
      height: 10px;
      width: 10px;
      border-radius: 50%;
      transform: translateX(-50%) translateY(-50%);
      z-index: 99999;
    }
    .cursor:nth-child(1) {
      background-color: #3A1C71;
      z-index: 999999;
    }
    .cursor:nth-child(2) {
      background-color: #FFAF7B;
    }
    

    JS公司

    $(document)
      .mousemove(function(e) {
        $('.cursor')
          .eq(0)
          .css({
            left: e.pageX,
            top: e.pageY
          });
        setTimeout(function() {
          $('.cursor')
            .eq(1)
            .css({
              left: e.pageX,
              top: e.pageY
            });
        }, 100);
      })
    

    Codepen Demo

    我会感谢你的帮助

    谢谢:)

    1 回复  |  直到 8 年前
        1
  •  1
  •   Tigerware    8 年前

    对于自定义光标,无需手动执行所有这些操作。如果您有一个可以用作光标的图像,您可以这样引用它:

    cursor: url('path-to-image.png'), auto; 
    

    有关更多详细信息,请查看: MDN reference


    如果要使用当前解决方案:

    超链接没有反应,因为两个光标div一直在其上方,因此会阻止链接本身。

    可以为这些div禁用鼠标事件。然后,这些事件将影响参考底图元素:

    .cursor {
      pointer-events: none;
    }
    
    推荐文章