mul*_*sen 43 javascript jquery hover
我见过这个jQuery语法:
if($(element).is(':hover')) { do something}
Run Code Online (Sandbox Code Playgroud)
由于我不使用jQuery,我正在寻找在纯JavaScript中执行此操作的最佳方法.
我知道我可以保持一个全局变量,设置/它通过取消设置mouseover和mouseout,但我不知道是否有某种方式通过DOM,而不是检查元素的本地属性?也许是这样的:
if(element.style.className.hovered === true) {do something}
Run Code Online (Sandbox Code Playgroud)
此外,它必须是跨浏览器兼容的.
zb'*_*zb' 48
您可以使用querySelector for IE> = 8
function isHover(e) {
return (e.parentElement.querySelector(':hover') === e);
}
Run Code Online (Sandbox Code Playgroud)
function isHover(e) {
return (e.parentElement.querySelector(':hover') === e);
}
var myDiv = document.getElementById('mydiv');;
document.addEventListener('mousemove', function checkHover() {
var hovered = isHover(myDiv);
if (hovered !== checkHover.hovered) {
console.log(hovered ? 'hovered' : 'not hovered');
checkHover.hovered = hovered;
}
});Run Code Online (Sandbox Code Playgroud)
.whyToCheckMe {position: absolute;left: 100px;top: 50px;}Run Code Online (Sandbox Code Playgroud)
<div id="mydiv">HoverMe
<div class="whyToCheckMe">Do I need to be checked too ?</div>
</div>Run Code Online (Sandbox Code Playgroud)
我认为这是好的@Kolink回答.
Hug*_*eth 27
简单地使用element.matches(':hover')似乎对我来说效果很好,你也可以在旧版浏览器中使用全面的polyfill:https://developer.mozilla.org/en-US/docs/Web/API/Element/matches
首先,您需要跟踪正在徘徊的元素.这是一种方法:
(function() {
var matchfunc = null, prefixes = ["","ms","moz","webkit","o"], i, m;
for(i=0; i<prefixes.length; i++) {
m = prefixes[i]+(prefixes[i] ? "Matches" : "matches");
if( document.documentElement[m]) {matchfunc = m; break;}
m += "Selector";
if( document.documentElement[m]) {matchfunc = m; break;}
}
if( matchfunc) window.isHover = function(elem) {return elem[matchfunc](":hover");};
else {
window.onmouseover = function(e) {
e = e || window.event;
var t = e.srcElement || e.target;
while(t) {
t.hovering = true;
t = t.parentNode;
}
};
window.onmouseout = function(e) {
e = e || window.event;
var t = e.srcElement || e.target;
while(t) {
t.hovering = false;
t = t.parentNode;
}
};
window.isHover = function(elem) {return elem.hovering;};
}
})();
Run Code Online (Sandbox Code Playgroud)
在我看来,检查元素是否悬停的一种方法是在css中设置一个未使用的属性:hover,然后检查javascript中是否存在该属性.它不是问题的正确解决方案,因为它没有使用dom-native悬停属性,但它是我能想到的最接近且最小的解决方案.
<html>
<head>
<style type="text/css">
#hover_el
{
border: 0px solid blue;
height: 100px;
width: 100px;
background-color: blue;
}
#hover_el:hover
{
border: 0px dashed blue;
}
</style>
<script type='text/javascript'>
window.onload = function() {check_for_hover()};
function check_for_hover() {
var hover_element = document.getElementById('hover_el');
var hover_status = (getStyle(hover_element, 'border-style') === 'dashed') ? true : false;
document.getElementById('display').innerHTML = 'you are' + (hover_status ? '' : ' not') + ' hovering';
setTimeout(check_for_hover, 1000);
};
function getStyle(oElm, strCssRule) {
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle) {
strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
}
else if(oElm.currentStyle) {
strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1) {
return p1.toUpperCase();
});
strValue = oElm.currentStyle[strCssRule];
}
return strValue;
};
</script>
</head>
<body>
<div id='hover_el'>hover here</div>
<div id='display'></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
(功能getStyle感谢JavaScript get Styles)
如果有人能想到一个更好的CSS属性用作旗帜,solid/dashed请告诉我.优选地,该属性是很少使用且不能遗传的属性.