【问题标题】:Mousemove giving wrong coordinates鼠标移动给出错误的坐标
【发布时间】:2020-05-15 17:19:02
【问题描述】:

美好的一天!我的小项目有问题。我希望实现的是,每当我将鼠标悬停在钓鱼区时,都会出现一个指示器。但是, tt 似乎在 mousemove 事件上给出了错误的坐标,或者我错了。我这里有我的codepen。

HTML

<div class="game-content">
  <div class="pond">
    <div id="circle" class="circle"></div>
    <div id="pond__fishing-area" class="pond__fishing-area"></div>
  </div>
</div>

JS

new Vue({
  el: '#app',
  mounted() {
    let circle = document.getElementById("circle");
    let circleRect = circle.getBoundingClientRect();
    let wrapper = document.getElementById("pond__fishing-area");
    let wrapperRect = wrapper.getBoundingClientRect();

    function moveCircle(e) {
      gsap.to(circle, 0.3, {
        css: {
          left: e.clientX,
          top: e.clientY
        }
      });
    }
    wrapper.addEventListener("mouseover", function() {
      gsap.to(circle, 0.4, { scale: 1, autoAlpha: 1 });
      wrapper.addEventListener("mousemove", moveCircle);
    });

    wrapper.addEventListener("mouseout", function() {
      gsap.to(circle, 0.4, { scale: 0.1, autoAlpha: 0 });
    });
  }
})

【问题讨论】:

  • onpond 类,position: relative; 计算错误位置。你能把它删掉吗?
  • 好的先生,让我试试
  • 我已经更新了 @hasan05 先生,但是球在移动时失去了不透明性。
  • 您可以设置mouseout 事件,@anyman morsy 的回答也可以。

标签: javascript css vue.js sass gsap


【解决方案1】:

基于@hasan05 执行此操作,它将解决您的问题:

  • 从班级.pond 删除 position: relative;
  • 上课.circle 添加 pointer-events: none;

【讨论】:

  • 先生,相对位置消失了,如何将池塘基地的孩子定位在池塘上?
【解决方案2】:

由于 pond 是 相对 定位的,所以 circle 定位不正确,因此在 moveCircle() 中定位 circle 时。我们需要将 circle 相对 定位到 池塘。即我们需要从客户的头寸中扣除池塘的头寸。 moveCircle() 将如下所示:

  function moveCircle(e) {
        let position=document.getElementById("circle").parentElement.getBoundingClientRect();
// position will contain the position of pond
          gsap.to(circle, 1, {
            css: {
              left: e.clientX-position.x,// giving relative position to circle
              top: e.clientY-position.y
            }
          });
        }

new Vue({
  el: '#app',
  mounted() {
    let circle = document.getElementById("circle");
    let circleRect = circle.getBoundingClientRect();
    let wrapper = document.getElementById("pond__fishing-area");
    let wrapperRect = wrapper.getBoundingClientRect();

    function moveCircle(e) {
    let position=document.getElementById("circle").parentElement.getBoundingClientRect();
// position will contain the position of pond

      gsap.to(circle, 1, {
        css: {
          left: e.clientX-position.x, // giving relative position to circle
          top: e.clientY-position.y
        }
      });
    }
    wrapper.addEventListener("mouseover", function() {
      gsap.to(circle, 0.4, { scale: 1, autoAlpha: 1 });
      wrapper.addEventListener("mousemove", moveCircle);
    });

    wrapper.addEventListener("mouseout", function() {
      gsap.to(circle, 0.4, { scale: 0.1, autoAlpha: 0 });
    });
  }
})
@import url("https://fonts.googleapis.com/css?family=Roboto&display=swap");
* {
  font-family: 'Roboto', sans-serif;
}

.game-content {
  display: -webkit-box;
  display: flex;
  -webkit-box-pack: center;
          justify-content: center;
  width: 100%;
  height: 100%;
}
.game-content .circle {
  height: 50px;
  width: 50px;
  background-color: red;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
  position: absolute;
  z-index: 3;
  pointer-events:none;
}
.game-content .pond {
  width: 700px;
  height: 400px;
  background-color: #fff;
  display: -webkit-box;
  display: flex;
  -webkit-box-pack: center;
          justify-content: center;
          position:relative;
}
.game-content .pond .pond__fishing-area {
  position: absolute;
  width: 600px;
  height: 200px;
  background: blue;
  bottom: 20px;
  border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.0.5/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div class="game-content">
    <div class="pond">
      <div id="circle" class="circle"></div>
      <div id="pond__fishing-area" class="pond__fishing-area"></div>
    </div>
  </div>

【讨论】:

  • 一个问题,当你慢慢移动圆圈时,我认为@Ayman Morsy 给出了一个很好的解决方案
  • @hasan05,是的,我注意到它卡在慢动作上。
【解决方案3】:

你的池塘的 css 属性 position: relative 给你错误的坐标,删除它。

【讨论】:

    【解决方案4】:

    position: relative; 计算错误。

    删除它并以其他方式设计。

    也改变这个事件,

           wrapper.addEventListener("mouseout", function(event) {      
            var e = event.toElement || event.relatedTarget;
              if(e == circle) {
                return;
              }
              gsap.to(circle, 0.4, { scale: 0.1, autoAlpha: 0 });
            });
    

    【讨论】:

    • 顺便说一句,相对位置消失了,我无法再将我的 div.pond_fishing-area 定位在池塘内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2014-10-18
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多