logo

/Code Snippet|v2.2.0

Module

Static Methods

getMouseButton(mouseEvent)getMouseButton.jsline 33

Normalize mouse event's button attributes.

PARAMETERS
NameTypeDescription

mouseEvent

MouseEvent

The mouse event object want to know.

RETURNS:
{

number

} - The value of meaning which button is clicked?

getMousePosition(position, relativeElement)getMousePosition.jsline 21

Get mouse position from mouse event

PARAMETERS
NameTypeDescription

position

MouseEventobjectArray.<number>

mouse position object

relativeElement

HTMLElement

HTML element that calculate relative
position

RETURNS:
{

Array.<number>

} - mouse position

getTarget(e)getTarget.jsline 14

Get a target element from an event object.

PARAMETERS
NameTypeDescription

e

Event

event object

RETURNS:
{

HTMLElement

} - target element

off(element, types, handler)off.jsline 40

Unbind DOM events
If a handler function is not passed, remove all events of that type.

PARAMETERS
NameTypeDescription

element

HTMLElement

element to unbind events

types

stringobject

Space splitted events names or eventName:handler object

handler

[ function ]

handler function

EXAMPLES
// Following the example of domEvent#on

// Unbind one event from an element.
off(div, 'click', toggle);

// Unbind multiple events with a same handler from multiple elements at once.
// Use event names splitted by a space.
off(element, 'mouseenter mouseleave', changeColor);

// Unbind multiple events with different handlers from an element at once.
// Use an object which of key is an event name and value is a handler function.
off(div, {
  keydown: highlight,
  keyup: dehighlight
});

// Unbind events without handlers.
off(div, 'drag');

on(element, types, handler, context)on.jsline 45

Bind DOM events.

PARAMETERS
NameTypeDescription

element

HTMLElement

element to bind events

types

stringobject

Space splitted events names or eventName:handler object

handler

functionobject

handler function or context for handler method

context

[ object ]

context - context for handler method.

EXAMPLES
var div = document.querySelector('div');

// Bind one event to an element.
on(div, 'click', toggle);

// Bind multiple events with a same handler to multiple elements at once.
// Use event names splitted by a space.
on(div, 'mouseenter mouseleave', changeColor);

// Bind multiple events with different handlers to an element at once.
// Use an object which of key is an event name and value is a handler function.
on(div, {
  keydown: highlight,
  keyup: dehighlight
});

// Set a context for handler method.
var name = 'global';
var repository = {name: 'CodeSnippet'};
on(div, 'drag', function() {
 console.log(this.name);
}, repository);
// Result when you drag a div: "CodeSnippet"

once(element, types, handler, context)once.jsline 22

Bind DOM event. this event will unbind after invokes.

PARAMETERS
NameTypeDescription

element

HTMLElement

HTMLElement to bind events.

types

stringobject

Space splitted events names or
eventName:handler object.

handler

functionobject

handler function or context for handler method.

context

[ object ]

context object for handler method.

preventDefault(e)preventDefault.jsline 13

Prevent default action

PARAMETERS
NameTypeDescription

e

Event

event object

stopPropagation(e)stopPropagation.jsline 13

Stop event propagation.

PARAMETERS
NameTypeDescription

e

Event

event object

Resizable