移动鼠标后,我有一组对角线。
如果我在页面上只有一个组实例但我需要有多个实例,这样可以正常工作。
当我复制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/
有人可以帮忙吗?
答案 0 :(得分: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);
}
所做的更改是:
答案 1 :(得分:1)
我将所有项目的center_x
和center_y
设置为属性,从而提高了DBS answer的效果,因此无需每次都计算:
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);
}