我的脚本更改了一些值,这似乎没有理由

My script changes some values with what seems to be no reason

本文关键字:有理由 脚本 我的      更新时间:2023-09-26

所以我一直在研究一个在鼠标坐标处生成气泡的脚本。这是一个非常基本的脚本,可以计算一些东西,例如随机不透明度、随机大小等。

var transform = (function () { // This piece is to test whether transform should be prefixed or not
  var testEl = document.createElement('div')
  if (testEl.style.transform == null) {
    var vendors = ['Webkit', 'Moz', 'ms']
    for (var vendor in vendors) {
      if (testEl.style[ vendors[vendor] + 'Transform' ] !== undefined) {
        return vendors[vendor] + 'Transform'
      }
    }
  }
  return 'transform'
})()
var bubbles = {}
bubbles.chance = 0.08  // Chance for a bubble to spawn at mousemove
bubbles.delay = 50     // Should minimally be 10, otherwise the circles can't transition from small to big
bubbles.duration = 800
bubbles.minScale = 0.2 // The scale of the bubbles will be anywhere between 0.2 and 1 of the default size defined by the CSS
bubbles.minOpacity = 0.4
bubbles.maxOpacity = 0.7
document.getElementById('bubbles').addEventListener('mousemove', function (e) {
  if (Math.random() < bubbles.chance) {
    var $el = document.createElement('div')
    var size = Math.random() * (1 - bubbles.minScale) + bubbles.minScale
    var transition = Math.round(bubbles.duration * 0.9)
    $el.style.transition = transition + 'ms ease-in-out'
    $el.style.top = e.offsetY + 'px'  // Seems to undergo a modulo for some periods of time
    $el.style.left = e.offsetX + 'px' // This one too
    $el.style[transform] = 'translate(-50%, -50%) scale(0)'
    $el.style.opacity = Math.random() * (bubbles.maxOpacity - bubbles.minOpacity) + bubbles.minOpacity
    window.setTimeout(function () {
      $el.style[transform] = 'translate(-50%, -50%) scale(' + size + ')'
      window.setTimeout(function () {
        $el.style[transform] = 'translate(-50%, -50%) scale(0)'
        window.setTimeout(function () {
          $el.parentNode.removeChild($el)
        }, transition)
      }, transition + bubbles.duration)
    }, bubbles.delay)
    document.getElementById('bubbles').appendChild($el)
  }
})
html, body{height:100%}body{margin:0;background-color:#17C}
#bubbles{
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}
#bubbles > div{
  position: absolute;
  width: 12vw;
  height: 12vw;
  border-radius: 50%;
  background-color: #FFF;
}
<div id="bubbles"></div>

现在,由于某种原因,一些气泡没有放置在正确的坐标上。脚本应该在每次生成新气泡时e.offsetXe.offsetY,但有时它似乎对值应用了模数。

我认为应用某种模的原因是,当您只在水平线上移动时,所有位移的气泡也会形成一条水平线。垂直也是如此。

该脚本是原版JavaScript,似乎发生错误的部分在这里:

$el.style.top = e.offsetY + 'px'
$el.style.left = e.offsetX + 'px'

还有一件奇怪的事情需要注意,那就是位移不仅一次发生在一个气泡上,而是发生在短时间内的所有气泡上。

该错误发生在所有主要浏览器中。

感谢所有输入!

我明白为什么会发生这种情况。 如果您碰巧将鼠标悬停在气泡而不是蓝色背景上,它将使用气泡作为 offsetX 和 Y 的参考。 如果是整页应用,则可以改用e.clientXe.clientY。 否则,如果事件在#bubbles上触发,则只需侦听该事件。

它与模无关! offsetXoffsetY是根据e.target计算的,有时是bubblesdiv,但有时是生成的圆(因为它嵌套在里面(,这就是为什么你从鼠标位置到圆的偏移量很小,所以下一个圆圈得到的值很少。

换句话说,只要您将鼠标悬停在生成的圆圈上,问题就会重复出现。

可以使用clientXclientY来获取绝对值。

这是一个更新的小提琴:https://jsfiddle.net/dwhcoqzc/1/

此外,您还可以使用其他方法,例如 prevent bubblinguseCapture .

相关文章: