Skip to main content

dom-utils

DG.DomEvent

Utility functions to work with the DOM events.

Functions

FunctionReturnsDescription
on( <HTMLElement> el, <String> types, <Function> fn, <Object> context? ) thisAdds a listener function (fn) to a particular DOM event type of the element el. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. 'click dblclick').
on( <HTMLElement> el, <Object> eventMap, <Object> context? ) thisAdds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove}
off( <HTMLElement> el, <String> types, <Function> fn, <Object> context? ) thisRemoves a previously added listener function. If no function is specified, it will remove all the listeners of that particular DOM event from the element. Note that if you passed a custom context to on, you must pass the same context to off in order to remove the listener.
off( <HTMLElement> el, <Object> eventMap, <Object> context? ) thisRemoves a set of type/listener pairs.
stopPropagation( <DOMEvent> ev ) thisStop the given event from propagation to parent elements. Used inside the listener functions:
DG.DomEvent.on(div, 'click', function (ev) { DG.DomEvent.stopPropagation(ev); });
disableScrollPropagation( <HTMLElement> el ) thisAdds stopPropagation to the element's 'mousewheel' events (plus browser variants).
disableClickPropagation( <HTMLElement> el ) thisAdds stopPropagation to the element's 'click', 'doubleclick', 'mousedown' and 'touchstart' events (plus browser variants).
preventDefault( <DOMEvent> ev ) thisPrevents the default action of the DOM Event ev from happening (such as following a link in the href of the a element, or doing a POST request with page reload when a <form> is submitted). Use it inside listener functions.
stop( <DOMEvent> ev ) thisDoes stopPropagation and preventDefault at the same time.
getMousePosition( <DOMEvent> ev, <HTMLElement> container? ) PointGets normalized mouse position from a DOM event relative to the container or to the whole page if not specified.
getWheelDelta( <DOMEvent> ev ) NumberGets normalized wheel delta from a mousewheel DOM event, in vertical pixels scrolled (negative if scrolling down). Events from pointing devices without precise scrolling are mapped to a best guess of between 50-60 pixels.
addListener( ) thisAlias to DG.DomEvent.on
removeListener( ) thisAlias to DG.DomEvent.off

DG.DomUtil

Utility functions to work with the DOM tree.

Functions

FunctionReturnsDescription
get( <String|HTMLElement> id ) HTMLElementReturns an element given its DOM id, or returns the element itself if it was passed directly.
getStyle( <HTMLElement> el, <String> styleAttrib ) StringReturns the value for a certain style attribute on an element, including computed values or values set through CSS.
create( <String> tagName, <String> className?, <HTMLElement> container? ) HTMLElementCreates an HTML element with tagName, sets its class to className, and optionally appends it to container element.
remove( <HTMLElement> el ) Removes el from its parent element
empty( <HTMLElement> el ) Removes all of el's children elements from el
toFront( <HTMLElement> el ) Makes el the last children of its parent, so it renders in front of the other children.
toBack( <HTMLElement> el ) Makes el the first children of its parent, so it renders back from the other children.
hasClass( <HTMLElement> el, <String> name ) BooleanReturns true if the element's class attribute contains name.
addClass( <HTMLElement> el, <String> name ) Adds name to the element's class attribute.
removeClass( <HTMLElement> el, <String> name ) Removes name from the element's class attribute.
setClass( <HTMLElement> el, <String> name ) Sets the element's class.
getClass( <HTMLElement> el ) StringReturns the element's class.
setOpacity( <HTMLElement> el, <Number> opacity ) Set the opacity of an element. opacity must be a number from 0 to 1.
testProp( <String[]> props ) String|falseGoes through the array of style names and returns the first name that is a valid style name for an element. If no such name is found, it returns false. Useful for vendor-prefixed styles like transform.
setTransform( <HTMLElement> el, <Point> offset, <Number> scale? ) Resets the 3D CSS transform of el so it is translated by offset pixels and optionally scaled by scale. Does not have an effect if the browser doesn't support 3D CSS transforms.
setPosition( <HTMLElement> el, <Point> position ) Sets the position of el to coordinates specified by position, using CSS translate or top/left positioning depending on the browser (used by Leaflet internally to position its layers).
getPosition( <HTMLElement> el ) PointReturns the coordinates of an element previously positioned with setPosition.
disableTextSelection()Prevents the user from generating selectstart DOM events, usually generated when the user drags the mouse through a page with text. Used internally by Leaflet to override the behaviour of any click-and-drag interaction on the map. Affects drag interactions on the whole document.
enableTextSelection()Cancels the effects of a previous DG.DomUtil.disableTextSelection.
disableImageDrag()As DG.DomUtil.disableTextSelection, but for dragstart DOM events, usually generated when the user drags an image.
enableImageDrag()Cancels the effects of a previous DG.DomUtil.disableImageDrag.
preventOutline( <HTMLElement> el ) Makes the outline of the element el invisible. Used internally by Leaflet to prevent focusable elements from displaying an outline when the user performs a drag interaction on them.
restoreOutline()Cancels the effects of a previous DG.DomUtil.preventOutline.

Properties

PropertyTypeDescription
TRANSFORMStringVendor-prefixed fransform style name (e.g. 'webkitTransform' for WebKit).
TRANSITIONStringVendor-prefixed transition style name.

DG.PosAnimation

Used internally for panning animations, utilizing CSS3 Transitions for modern browsers and a timer fallback for IE6-9.

var fx = new DG.PosAnimation();
fx.run(el, [300, 500], 0.5);

Events

EventDataDescription
startEventFired when the animation starts.
stepEventFired continuously during the animation.
endEventFired when the animation ends.

Methods

MethodReturnsDescription
run( <HTMLElement> el, <Point> newPos, <Number> duration?, <Number> easeLinearity?) Run an animation of a given element to a new position, optionally setting duration in seconds (0.25 by default) and easing linearity factor (3rd argument of the cubic bezier curve, 0.5 by default).
stop()Stops the animation (if currently running).

Methods inherited from Evented

DG.Draggable

A class for making DOM elements draggable (including touch support). Used internally for map and marker dragging. Only works for elements that were positioned with DG.DomUtil.setPosition

var draggable = new DG.Draggable(elementToDrag);
draggable.enable();

Properties

PropertyTypeDefaultDescription
clickToleranceNumber3The max number of pixels a user can shift the mouse pointer during a click for it to be considered a valid click (as opposed to a mouse drag).

Events

EventDataDescription
downEventFired when a drag is about to start.
dragstartEventFired when a drag starts
predragEventFired continuously during dragging before each corresponding update of the element's position. Fired continuously during dragging.
dragendEventFired when the drag ends.

Methods

MethodReturnsDescription
enable()Enables the dragging ability.
disable()Disables the dragging ability.

Methods inherited from Evented