美文网首页
css互动--鼠标光标渐变跟踪

css互动--鼠标光标渐变跟踪

作者: 怪兽别跑biubiubi | 来源:发表于2018-03-23 15:20 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>鼠标光标渐变跟踪</title>
      <style>
        button {
          position: relative;
          background: #7983ff;
          padding: 1rem 2rem;
          color: #fff;
          outline: none;
          overflow: hidden;
          cursor: pointer;
          border: none;
          font-size: 15px;
        }
        span {
          position: relative;
        }
        button::before {
          --size: 0;
          content: '';
          position: absolute;
          left: var(--x);
          top: var(--y);
          width: var(--size);
          height: var(--size);
          background: radial-gradient(circle closest-side, pink, transparent);
          transform: translate(-50%, -50%);
          transition: width 0.2s ease, height 0.2s ease;
        }
        button:hover::before {
          --size: 200px;
        }
      </style>
    </head>
    <body>
      <button class="btn">
        <span>悬停</span>
        <!-- 如果元素的父级具有定位上下文(position: relative ),您还需要减去它的偏移量。
        var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
        var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop -->
      </button>
    
      <script>
        var btn = document.querySelector('.btn');
        btn.onmousemove = function(e) {
          var x = e.pageX - btn.offsetLeft
          var y = e.pageY - btn.offsetTop
          btn.style.setProperty('--x', x + 'px')
          btn.style.setProperty('--y', y + 'px')
        }
      </script>
    </body>
    </html>

    相关文章

      网友评论

          本文标题:css互动--鼠标光标渐变跟踪

          本文链接:https://www.haomeiwen.com/subject/hfdxcftx.html