Mouse Event delay Switch plugin _javascript skill

Source: Internet
Author: User
Tags unique id
The principle is simple:
onmouseover, onmouseout Execute business code when the use of settimeout for delay, the second trigger when the first clear off the front of the settimeout.
principle
Copy Code code as follows:

var timer;
document.getElementById (' Test '). onmouseover = function () {
Cleartimeout (timer);
Timer = settimeout (function () {
Alert (' over ')
}, 150);
};
document.getElementById (' Test '). onmouseout = function () {
Cleartimeout (timer);
Timer = settimeout (function () {
Alert (' Out ')
}, 150);
};

The code above can see that the Timer return value (unique ID) is saved by the timer, and onmouseover and Onmouserout both can clear the timer that is not executed and prevent repeat execution. Here timer let onmouseover and Onmouserout have a "group" concept, we can also allow more elements to access to the "group", such as the insertion of the Drop-down menu and tips and other trigger elements and pop-up layer need to share the same timer, This does not cause the layer to be closed because the mouse is left (as long as the pointer is still on the layer).
Encapsulating events
Copy Code code as follows:

/*!
* Hoverdelay.js
* http://www.planeArt.cn
* Copyright, Tangbin
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function (pluginname) {
var id = 0, data = {},
addevent = function (Elem, type, callback) {
if (Elem.addeventlistener) {
Elem.addeventlistener (Type, callback, false);
} else {
Elem.attachevent (' on ' + type, function () {Callback.call (Elem)});
};
};
This[pluginname] = function (Elem, over, out, group, speed) {
ID + +;
if (Arguments.length = = 0) return ID;
if (typeof arguments[1]!== ' function ') return cleartimeout (data[arguments[1)]);
if (typeof elem = = = ' String ') Elem = document.getElementById (elem);
Group = Group | | Elem[pluginname] | | Id
Speed = Speed | | 150;
Elem[pluginname] = group;
Addevent (Elem, ' mouseover ', function () {
var elem = this,
fn = function () {Over.call (elem)};
Cleartimeout (Data[group]);
Data[group] = settimeout (FN, speed);
});
Addevent (Elem, ' mouseout ', function () {
var elem = this,
fn = function () {Out.call (elem)};
Cleartimeout (Data[group]);
Data[group] = settimeout (FN, speed);
});
};
}) (' Hoverdelay ');

Data is responsible for saving a custom "group" under the same "group" that can even suspend the mouseout callback function execution, which enables nested nested operations.

Interface description

Method Parameters function
Hoverdelay (Elem, over, out, group) element, the mouse is near the callback function, the mouse left the callback function, set the delay group name [optional] Set the delay trigger effect
Hoverdelay (Elem, Group) element, delay group name Stop the mouse from executing the callback function
Hoverdelay () No Get unique deferred group name
2011-01-22 Update
I noticed a description of the hover event in the jquery API:
is accompanied by a detection of whether the mouse is still in a particular element (for example, an image in a div) and, if so, continues to remain in the hover state without triggering the move out event (fixed a common error using the Mouseout event).
Mouseout have bugs? It reminds me of a time when I worked to make a mouse trigger to display a business card (like Tencent Weibo's avatar) is often incorrectly executed by the Mouseout event. Then I looked up the jquery hover source how to solve this problem, found that it is using "MouseEnter" and "MouseLeave" instead of "mouseover" and "Mouseout", "MouseEnter" and " MouseLeave "is IE (6, 7, 8) of the special event, the standard browser does not support the need for simulation, the final version:
Copy Code code as follows:

/*!
* Hoverdelay.js v1.1
* http://www.planeArt.cn
* Copyright, Tangbin
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function (pluginname) {
var id = 0, data = {},
addevent = function (Elem, type, callback) {
if (Elem.addeventlistener) {
if (type = = ' MouseEnter ') {
Elem.addeventlistener (' MouseOver ', Withinelement (callback), false);
else if (type = = ' MouseLeave ') {
Elem.addeventlistener (' Mouseout ', Withinelement (callback), false);
} else {
Elem.addeventlistener (Type, callback, false);
};
} else {
Elem.attachevent (' on ' + type, function () {Callback.call (Elem, Window.event)});
};
},
Withinelement = function (callback) {
return function (event) {
var parent = Event.relatedtarget;
try {
while (parent && parent!== this) parent = Parent.parentnode;
if (parent!== this) callback.apply (this, arguments);
catch (e) {};
};
};
This[pluginname] = function (Elem, over, out, group, speed) {
ID + +;
if (Arguments.length = = 0) return ID;
if (typeof arguments[1]!== ' function ') return cleartimeout (data[arguments[1)]);
if (typeof elem = = = ' String ') Elem = document.getElementById (elem);
Group = Group | | Elem[pluginname] | | Id
Speed = Speed | | 150;
Elem[pluginname] = group;
Addevent (Elem, ' MouseEnter ', function () {
var elem = this,
fn = function () {Over.call (elem)};
Cleartimeout (Data[group]);
Data[group] = settimeout (FN, speed);
});
Addevent (Elem, ' MouseLeave ', function () {
var elem = this,
fn = function () {Out.call (elem)};
Cleartimeout (Data[group]);
Data[group] = settimeout (FN, speed);
});
};
}) (' Hoverdelay ');

View version 1.1 Demo
Http://demo.jb51.net/js/2011/hover/index.htm
New window opens

Download

1, native version 1.1
2, jquery plug-in version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.