[英]rotate on mouse hover
I have this function but there are two issues I can't solve, first one is that the mouse action is set on all the screen and I want it to be when hover over the dive "ehab" only, not when I move mouse over screen, the other issue is that when I have more than one div , the function works only on the first one ... kindly advise me 我具有此功能,但有两个问题我无法解决,第一个问题是在所有屏幕上都设置了鼠标动作,并且我希望它仅在将鼠标悬停在“ ehab”上时出现,而不是在将鼠标悬停在上面时屏幕上,另一个问题是,当我有多个div时,该功能仅在第一个div上起作用。
/*
By : Ofelquis
Twitter: @felquis
Blog : tutsmais.com.br
Simples implementação ;)
*/
!(function ($doc, $win) {
var screenWidth = $win.screen.width / 2,
screenHeight = $win.screen.height / 2,
$ehab = $doc.querySelectorAll('#ehab')[0],
validPropertyPrefix = '',
otherProperty = 'perspective(600px)';
if(typeof $ehab.style.webkitTransform == 'string') {
validPropertyPrefix = 'webkitTransform';
} else {
if (typeof $ehab.style.MozTransform == 'string') {
validPropertyPrefix = 'MozTransform';
}
}
$doc.addEventListener('mousemove', function (e) {
// vars
var centroX = e.clientX - screenWidth,
centroY = screenHeight - (e.clientY + 13),
degX = centroX * 0.1,
degY = centroY * 0.1
// Seta o rotate do elemento
$ehab.style[validPropertyPrefix] = otherProperty + 'rotateY('+ degX +'deg) rotateX('+ degY +'deg)';
});
})(document, window);
The first problem, ( I want it to be when hover the div "#ehab" ), you need the attach the mousemove
event to your div: 第一个问题是,( 我希望将其悬停在div“ #ehab”上 ),您需要将mousemove
事件附加到div上:
$ehab.addEventListener('mousemove', function (e) {
//You code here.
});
The second problem, ( when I have more than one div , the function works only on the first one ), you can't have duplicated IDs on your DOM tree, change the selector to a class, for example, .ehab
then you have to loop through the matched elements, please try this code: 第二个问题( 当我有多个div时,该功能仅在第一个上起作用 ),您不能在DOM树上具有重复的ID,将选择器更改为一个类,例如.ehab
那么您就有了要遍历匹配的元素,请尝试以下代码:
/* By : Ofelquis Twitter: @felquis Blog : tutsmais.com.br Simples implementação ;) */ !(function($doc, $win) { var $ehabDIVs = $doc.querySelectorAll('.ehab'), otherProperty = 'perspective(600px)'; for (var i = 0; i < $ehabDIVs.length; ++i) { var $ehab = $ehabDIVs[i]; $ehab.addEventListener('mousemove', function(e) { // vars var centroX = (e.pageX - this.offsetLeft) - this.offsetWidth/2, centroY = this.offsetHeight/2 - (e.pageY-this.offsetTop), degX = centroX * 0.1, degY = centroY * 0.1 if(this._leave) clearInterval(this._leave); // Seta o rotate do elemento this.style.transform = otherProperty + 'rotateY(' + degX + 'deg) rotateX(' + degY + 'deg)'; }); $ehab.addEventListener('mouseleave', function(e) { var self = this; this._leave = setTimeout(function() { self.style.transform = 'rotateY(0deg) rotateX(0deg)'; }, 250); }); } })(document, window);
.ehab { width:300px; height:300px; background-color:red; margin:15px; float:left; transition: transform 0.15s ease; }
<div class="ehab">First Div</div><div class="ehab">Second Div</div>
Good Luck Ismaiel. 祝你好运Ismaiel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.