Apply hover from the cursor position

后端 未结 5 809
难免孤独
难免孤独 2021-02-14 04:23

I need get hover effect in a div from the cursor position.

I have this html and css

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-14 05:06

    To change position of inner circle you can use pageX and pageY on mousemove. To change size of inner circle you can create one class that will scale div and toggle that class on hover over .f.

    var s = $('.s')
    var f = $('.f')
    var oTop = f.offset().top + (s.height() / 2);
    var oLeft = f.offset().left + (s.width() / 2);
    
    f.hover(function() {
      s.toggleClass('change')
    })
    
    f.mousemove(function(e) {
      var x = e.pageY - oTop
      var y = e.pageX - oLeft
    
      s.css({
        top: x + 'px',
        left: y + 'px'
      })
    })
    .f {
      width: 200px;
      height: 200px;
      background-color: grey;
      position: fixed;
      overflow: hidden;
      border-radius: 100px;
    }
    .s {
      width: 50px;
      height: 50px;
      background-color: black;
      border-radius: 100px;
      position: absolute;
      pointer-events: none;
      opacity: 0;
      transition: transform 0.5s linear, opacity 0.3s linear;
    }
    .change {
      transform: scale(2);
      opacity: 1;
    }
    
    

提交回复
热议问题