Tipp: jQuery mousemove anstatt mouseover oder hover verwenden!

Tipp: jQuery mousemove anstatt mouseover oder hover verwenden!

In einem aktuellen Projekt war ein vertikales Sliding-Menü umzusetzen.
Wenn der Benutzer über eine Überschrift fährt, sollten sich die Subelemente öffnen und andere Subelemente wieder schließen.

Zunächst entschied ich mich gegen das jQuery "hover" Event, da dies auch eine "mouseout"-Funktion implementiert, die ich nicht benötige. Also wurde das jQuery "mouseover"-Event verwendet.

Soweit so gut, die Funktionalität schnell implementiert - alles läuft! Aber es gab einen doofen Effekt!
Wenn der Benutzer während der Animation bereits auf einen anderen Menüpunkt fährt, reagiert dieser danach nicht. Die Maus befindet sich also über einem Element, das sich nun eigentlich öffnen sollte!

Dass dies nicht parallel passiert ist OK und gewünscht - aber das "mouseover"-Event löst gar nicht erst aus. Auch nicht, wenn die Maus danach auf dem Element bewegt wird. Dieses Verhalten ist man eher nicht gewohnt.

Lösung: Durch Verwendung des "mousemove"-Events bleibt die Schaltfläche reaktiv, sprich das Event feuert spätestens dann, wenn der Benutzer die Maus minimal bewegt!
Ein super Verhalten, wie man es erwarten würde!

Wichtiger Hinweis: Da dieses Event natürlich sehr oft feuert, sollte direkt als erstes eine if-Bedingung eingefügt werden, welche die Aktion nur ausführen lässt, wenn dies nötig ist (hier: Das Menü nicht schon geöffnet ist). Dann sollte es auch keine Performance-Bedenken mehr geben.

Viel Spaß mit diesem Tipp!

Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <smarty>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Textual smileys will be replaced with graphical ones.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.