javascript - jQuery - About the is(':hover') -
is(':hover')
quite useful, not work.
in google chrome, open image page(i'm writing chrome extension, content script involves jquery), not work, in normal web page does.
how know if works?
should extend function, saving $.fn.is
this, , deal mousein
, mouseout
event ?
any suggestions?
edit
looks adeneo's answer good, dose not work.
about yckart's answer, think time node not got mouseleave
event, break hover state, need condition here.
my jquery version 1.10
e.g.
i use tag input,when move in input area show tag selector(a div shows list of tag), when move out input area hide tag selector. when leave input area, have check mouse in selector area (add delay)? if is, not hide selector. when leave selector have check mouse in input area(add delay)? if not, hide selector.
add delay make sure mouse enter area , can not put selector in input area(if can, don't have check mouse leave or enter in both area), sub node.
:hover pseudo-selectors in sizzle documention, don't think bad.
to keep code, think should add $.expr[':'].hover
jquery plugin
demo here jsfiddle
i add :hovered
expression plugin,you can use is(':hovered')
directly.
the first time return false, can call trackhover
before use is(':hovered')
.
the best way should this:
// add class if hover element $('*').on('mouseenter mouseleave', function () { $(this).toggleclass('hover'); }); // later check 'hover'-class // instead of $('node').is(':hover'); $('node').is('.hover');
update
i made simple expression filter using matchesselector
:
(function ($) { // https://gist.github.com/jonathantneal/3062955 var html = document.documentelement; var matches = html.matchesselector || html.omatchesselector || html.msmatchesselector || html.mozmatchesselector || html.webkitmatchesselector || function (selector) { var nodes = (this.parentnode || this.document).queryselectorall(selector), = -1; while (nodes[++i] && nodes[i] != this); return !nodes[i]; }; $.expr.filters.hover = function (elem) { return matches.call(elem, ':hover'); }; }(jquery)); $('div').hover(function () { console.log( $(this).is(':hover') ); });
Comments
Post a Comment