admin管理员组文章数量:1278952
Is there any event which is triggered when the mouse cursor changes as it hovers over different page elements ?
Ideally an event like this:
window.onmousecursorchange = function(e) {
// e would contain information about the cursor
// eg. e.type could contain 'text','pointer', etc..
}
Note: The solution should not involve jQuery or other libraries
Update:
The 'possible duplicate' question is tagged with jQuery infact all the answers (none of which solve the problem) are based on jQuery. I am looking for a pure JavaScript solution. If the moderators believe this is not enough reason to keep this question open feel free to close.
Is there any event which is triggered when the mouse cursor changes as it hovers over different page elements ?
Ideally an event like this:
window.onmousecursorchange = function(e) {
// e would contain information about the cursor
// eg. e.type could contain 'text','pointer', etc..
}
Note: The solution should not involve jQuery or other libraries
Update:
The 'possible duplicate' question is tagged with jQuery infact all the answers (none of which solve the problem) are based on jQuery. I am looking for a pure JavaScript solution. If the moderators believe this is not enough reason to keep this question open feel free to close.
Share Improve this question edited Feb 3, 2023 at 3:47 Mayank Kumar Chaudhari 18.7k13 gold badges67 silver badges153 bronze badges asked Jan 26, 2013 at 17:02 lostsourcelostsource 21.8k9 gold badges70 silver badges89 bronze badges 1- 2 See detect cursor type – Antony Commented Jan 26, 2013 at 17:09
4 Answers
Reset to default 3You can try this:
document.addEventListener('mouseover',function(e){
var cursor = e.target.style.cursor;
console.log(cursor);
});
It uses event bubbling to increase performance and save code.
Yes with event onmouseenter
$('*').mouseenter(function(){
var currentCursor = $(this).css('cursor') ;
console.log( currentCursor );
});
$(function(){
$('*').hover(function(){
$(this).data('hover',1); //store in that element that the mouse is over it
},
function(){
$(this).data('hover',0); //store in that element that the mouse is no longer over it
});
window.isHovering = function (selector) {
return $(selector).data('hover')?true:false; //check element for hover property
}
});
@Lickson's solution here works only for inline styles. Not for cursor defined in css files. Thus, you need to getComputedStyle
document.addEventListener('mouseover',function(e){
var cursor = getComputedStyle(e.target).cursor;
console.log(cursor);
});
本文标签: javascriptDetecting mouse cursor type change in a webpageStack Overflow
版权声明:本文标题:javascript - Detecting mouse cursor type change in a webpage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741261720a2367746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论