javascript - CSS js on mousemove重画而不消失

标签 javascript html css onmousemove

我通过鼠标位置在窗口上用 CSS 绘制圆圈。然后我慢慢地移动光标,很好地重画,但是如果我更快地移动光标,我可以看到圆圈是如何消失的。如何才能顺利重画而不消失?

我在下面尝试的代码:

.circle {
    position: absolute;
    width:100px;
    height:100px;
    border-radius:50px;
    border: 1px solid #000
}

<div class="circle"> </div>

$(window).mousemove(function(e) {
    var x = e.clientX;
    var y = e.clientY;

    $(".circle").css("left", x-50); //-50 for center because circle width = 100
    $(".circle").css("top", y-50); //-50 for center because circle height = 100
});

http://jsfiddle.net/rkEMR/3996/

最佳答案

两项优化可以解决您的问题:

  1. 在鼠标移动之前将 $(".circle") 缓存在变量中
  2. 使用变换:translate3d(x,y,z)而不是left+top

参见:http://jsfiddle.net/rkEMR/3998/

关于javascript - CSS js on mousemove重画而不消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213795/

相关文章:

javascript - 如何向我的网页添加交互式通知徽章?

javascript - 显示高分辨率图像

javascript - Chromium 内部 postMessage 事件

javascript - 我正在尝试在 javascript 循环中发送获取或发布请求,但 .click() 仅处理一次

html - 如何在 visual studio 中为 less 和 sass 使用自动编译器

html - 通过 css 将 div 移入屏幕

html - 为什么 flex 元素不缩小过去的内容大小?

javascript - 页面不断自动刷新

javascript - Jquery 动画内容更改?

jquery - TableSort 插件问题 asp.net