gpt4 book ai didi

javascript - 在 mousemove 上旋转多组形状

转载 作者:行者123 更新时间:2023-11-30 08:24:51 25 4
gpt4 key购买 nike

我有一组对 Angular 线跟随鼠标移动。

如果我在页面上只有一个组实例但我需要有多个实例,这会很好用。

当我复制 HTML 时,它们不会彼此独立工作,也不会产生预期的效果。

jQuery:

var dash = $(".graphic-wave__dash");
if(dash.length > 0){
var offset = dash.offset();
function mouse(evt){
var center_x = (offset.left) + (dash.width()/2);
var center_y = (offset.top) + (dash.height()/2);
var mouse_x = evt.pageX; var mouse_y = evt.pageY;
var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
var degree = (radians * (180 / Math.PI) * -1) + 90;
dash.css('-moz-transform', 'rotate('+degree+'deg)');
dash.css('-webkit-transform', 'rotate('+degree+'deg)');
dash.css('-o-transform', 'rotate('+degree+'deg)');
dash.css('-ms-transform', 'rotate('+degree+'deg)');
dash.removeClass('original');
}
$(document).mousemove(mouse);
}

HTML:

<div class="graphic-wave">
<div class="graphic-wave__row">
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
</div>
<div class="graphic-wave__row">
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
</div>
<div class="graphic-wave__row">
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
</div>
</div>

<div class="graphic-wave" style="left: 400px;">
<div class="graphic-wave__row">
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
</div>
<div class="graphic-wave__row">
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
</div>
<div class="graphic-wave__row">
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
<div class="graphic-wave__dash original"></div>
</div>
</div>

https://jsfiddle.net/ob80/7nxf07vo/3/

有人能帮忙吗?

最佳答案

您需要分别计算每条线的旋转,目前您正在制定单一旋转样式并将其应用到具有 graphic-wave__dash 类的每个元素。

Here's a quick example我没有做任何优化,这可能是非常低效的。 (我没有把它放到一个片段中,因为它似乎不支持 SCSS)

var dash = $(".graphic-wave__dash");
if (dash.length > 0) {

function mouse(evt) {
dash.each(function() { // Calculate per element, not just once
var offset = $(this).offset(); // Workout the offset per element
var center_x = (offset.left) + ($(this).width() / 2); // Use $(this) to reference the current element
var center_y = (offset.top) + ($(this).height() / 2);
var mouse_x = evt.pageX;
var mouse_y = evt.pageY;
var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
var degree = (radians * (180 / Math.PI) * -1) + 90;
$(this).css('-moz-transform', 'rotate(' + degree + 'deg)');
$(this).css('-webkit-transform', 'rotate(' + degree + 'deg)');
$(this).css('-o-transform', 'rotate(' + degree + 'deg)');
$(this).css('-ms-transform', 'rotate(' + degree + 'deg)');
$(this).removeClass('original');
});
}
$(document).mousemove(mouse);
}

所做的更改是:

  • 在函数内部,用你的类遍历每个元素
  • 计算每个元素的偏移量,而不是全部基于该类的第一个实例

关于javascript - 在 mousemove 上旋转多组形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com