【问题标题】:Apply hover from the cursor position从光标位置应用悬停
【发布时间】:2017-03-07 20:32:16
【问题描述】:

我需要从光标位置在div 中获得悬停效果。

我有这个html 和css

.f {
  width: 200px;
  height: 200px;
  background-color: grey;
  position: fixed;
  border-radius: 100px;
}

.s {
  width: 50px;
  height: 50px;
  background-color: black;
  border-radius: 100px;
  margin: 75px 0px 0px 75px;
  transition: width 1s, height 1s, margin 1s;
}

.s:hover {
  width: 100px;
  height: 100px;
  background-color: black;
  margin: 50px 0px 0px 50px;
}
<div class="f">
  <div class="s"></div>
</div>

我需要这样的东西:

我对 js 或 jquery 解决方案持开放态度。

编辑

我有一个 jquery 解决方案:

$("div.f").mousemove(function(e) {
  $('div.s').css({
    left: e.clientX - 28,
    top: e.clientY - 24
  });
});
.f {
  width: 200px;
  height: 200px;
  background-color: grey;
  position: fixed;
  border-radius: 100px;
  /* comment or remove the overflow if necessary */
  overflow: hidden;
}

.s {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: black;
  border-radius: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="f">
  <div class="s"></div>
</div>

但我需要圆圈像第一个 sn-p 一样制作过度动画。

原问题here

【问题讨论】:

  • 您是否正在尝试定位黑色圆圈以使其“跟随”光标?
  • 不完全是,我尝试当我进入 div 时,悬停效果出现在光标位置
  • 您可能需要使用 javascript 来执行此操作,因为 CSS 中的悬停事件只能应用静态样式。
  • Here is something similar。这个方法是用hitboxes完成的,只有css,添加许多div来检测光标的位置。更好的方法是使用javascript,这里是one example
  • 如果你对 js 解决方案持开放态度,你应该用 js 标记你的问题

标签: javascript jquery html css


【解决方案1】:

要更改内圈的位置,您可以在 mousemove 上使用 pageX 和 pageY。要更改内圈的大小,您可以创建一个将 scale div 的类并将该类悬停在 .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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="f">
  <div class="s"></div>
</div>

【讨论】:

  • 只删除溢出:隐藏。
  • 当我这样做时,我没有像第一个sn-p那样得到悬停效果,只有圆圈可以出现在大圆圈之外
  • 我不知道你的意思是什么。
  • 对不起,我的英语很差。当我删除 overflow:hidden 时,我没有得到预期的效果。我需要像悬停在我的问题第一个狙击手中一样获得效果(黑色圆圈变大)。
  • 是的,没关系。你可以做到这个效果,但只在灰色圆圈中显示黑色圆圈(就像我的第二个 sn-p)。
【解决方案2】:

这是一个 jQuery 解决方案。

$("div.f").mousemove(function(e) {
  $('div.s').css({
    left: e.clientX - 28,
    top: e.clientY - 24
  });
});
.f {
  width: 200px;
  height: 200px;
  background-color: grey;
  position: fixed;
  border-radius: 100px;
  /* comment or remove the overflow if necessary */
  overflow: hidden;
}

.s {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: black;
  border-radius: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="f">
  <div class="s"></div>
</div>

【讨论】:

    【解决方案3】:

    $('.f').on('mousemove', function(e){
    var par = $(this);
      if((e.pageX <= par.width() && e.pageX >= 0) && e.pageY <= par.height() && e.pageY >= 0){
        $('.s').css({
           position: 'relative',
           left:  e.pageX - (par.width() / 2),
           top:   e.pageY - (par.height() / 2)
        });
        } else {
        $('.s').css({
          position: 'initial'
        });
        }
    });
    .f {
      width: 200px;
      height: 200px;
      background-color: grey;
      position: fixed;
      border-radius: 100px;
    }
    
    .s {
      width: 50px;
      height: 50px;
      background-color: black;
      border-radius: 100px;
      margin: 75px 0px 0px 75px;
      transition: width 1s, height 1s, margin 1s;
    }
    
    .s:hover {
      width: 100px;
      height: 100px;
      background-color: black;
      margin: 50px 0px 0px 50px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="f">
      <div class="s"></div>
    </div>

    【讨论】:

      【解决方案4】:

      function moveInner(e)
      {
        var inner = document.getElementById('inner');
       
        inner.style.top = (e.clientY-100)+"px";
        inner.style.left= (e.clientX-100)+"px";
      }
      .f {
        width: 200px;
        height: 200px;
        background-color: grey;
        position: fixed;
        border-radius: 100px;
      }
      
      .s {
        width: 100px;
        height: 100px;
        background-color: black;
        border-radius: 100px;
        margin: 75px 0px 0px 75px;
      
        position: absolute;
      }
      
      
      Please put the inner div outside the parent div
      And set the onmouseover for parent div to change inner div's position
      <div class="f" id="parent" onmousemove="moveInner(event)">
        
      </div><div class="s" id="inner"></div>

      【讨论】:

      • 可以,请稍等
      【解决方案5】:

      var ol_x= null;
      var ol_y= null;
      function moveInner(me, e)
      {
      if(ol_x!=null)
      {
      var ctx = me.getContext("2d");
       ctx.arc(ol_x, ol_y, 42, 0, 2 * Math.PI, false);
       ctx.fillStyle='grey';
          ctx.fill();
          ctx.restore();
      }
      ol_x = e.clientX+20;
      ol_y = e.clientY+20;
        var ctx = me.getContext("2d");
      ctx.beginPath();
      ctx.arc(ol_x, ol_y, 40, 0, 2*Math.PI, false);
      ctx.fillStyle ='black';
      ctx.fill();
      ctx.stroke();
      
      }
      .f {
        width: 200px;
        height: 200px;
        background-color: grey;
        position: fixed;
        border-radius: 100px;
      }
      
      Hi this is my solution for EDIT<BR>
      I use 2D context to draw inner DIV inside parent DIV
      <canvas class="f" id="parent" onmousemove="moveInner(this, event)">
        
      </canvas>

      【讨论】:

        猜你喜欢
        • 2020-02-19
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 2017-08-14
        • 2015-10-05
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        相关资源
        最近更新 更多