在 mousemove 上旋转多组形状
Rotate multiple groups of shapes on mousemove
我有一组对角线跟随鼠标移动。
如果我在页面上只有一个群组实例,但我需要有多个实例,这很好用。
当我复制 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
class 的每个元素。
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);
}
所做的更改是:
- 在函数内部,使用 class
遍历每个元素
- 计算每个元素的偏移量,而不是全部基于该元素的第一个实例class
我通过将所有项目的 center_x
和 center_y
设置为属性,提高了 的性能,因此不必每次都计算它:
var dash = $(".graphic-wave__dash");
if (dash.length > 0) {
dash.each(function() {
var offset = $(this).offset();
$(this).attr('center_x',(offset.left) + ($(this).width() / 2));
$(this).attr('center_y',(offset.top) + ($(this).height() / 2));
});
function mouse(evt) {
dash.each(function() {
var mouse_x = evt.pageX;
var mouse_y = evt.pageY;
var radians = Math.atan2(mouse_x - $(this).attr('center_x'), mouse_y - $(this).attr('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);
}
这里是fiddle:https://jsfiddle.net/7nxf07vo/6/
我有一组对角线跟随鼠标移动。
如果我在页面上只有一个群组实例,但我需要有多个实例,这很好用。
当我复制 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
class 的每个元素。
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);
}
所做的更改是:
- 在函数内部,使用 class 遍历每个元素
- 计算每个元素的偏移量,而不是全部基于该元素的第一个实例class
我通过将所有项目的 center_x
和 center_y
设置为属性,提高了
var dash = $(".graphic-wave__dash");
if (dash.length > 0) {
dash.each(function() {
var offset = $(this).offset();
$(this).attr('center_x',(offset.left) + ($(this).width() / 2));
$(this).attr('center_y',(offset.top) + ($(this).height() / 2));
});
function mouse(evt) {
dash.each(function() {
var mouse_x = evt.pageX;
var mouse_y = evt.pageY;
var radians = Math.atan2(mouse_x - $(this).attr('center_x'), mouse_y - $(this).attr('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);
}
这里是fiddle:https://jsfiddle.net/7nxf07vo/6/