dom
#
DescriptionA set of utilities to interact with the DOM, event listeners and similar low level tasks. For a more structured API (jQuery like) consider DOMtastic.
#
API#
escape- See: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector#Escaping_special_characters
Escape colons to allow use class names as .module:block__element
#
Parametersselector
string
Returns string
\$
Sizzle/jQuery like DOM nodes shortcut for document.querySelector
To avoid an extra function call we inline the above escape
#
Parametersselector
string DOM selectorparent
HTMLElement? It falls back towindow.document
#
ExamplesBasic DOM selection
Returns HTMLElement
#
\$\$Sizzle/jQuery like DOM nodes shortcut for document.querySelectorAll
To avoid an extra function call we inline the above escape
#
Parametersselector
string DOM selectorparent
HTMLElement? It falls back towindow.document
Returns NodeList
#
forEachFor each, iterate through a NodeList of HTMLElements
#
Parametersnodes
NodeList DOM nodes collectioncallback
function (HTMLElement, number): anyscope
Object?
#
\$eachEach shortcut, iterate through a NodeList of HTMLElements retrieved with the given selector (and optionally a parent container passed as thrid arguement)
#
Parametersselector
string DOM selectorcallback
function (HTMLElement, number): anyparent
HTMLElement? It falls back towindow.document
scope
Object?
#
toArrayNode list to array
#
ParametersnodeList
(NodeList | HTMLFormControlsCollection)
Returns (Array<HTMLElement> | Array<HTMLFormElement>)
#
setVendorCSSSet vendor CSS rule
#
Parameterselement
HTMLElement A single HTMLElementprop
string CSS rule proertyvalue
(string | number | boolean) The CSS value to set, it will be automatically vendor prefixed
#
isNodeListIs node list
#
Parametersnodes
any The object to check
Returns boolean
#
onShortcut for addEventListener
#
Parametersel
(Window | Document | HTMLElement)type
stringhandler
EventListeneroptions
(AddEventListenerOptions | boolean) (optional, defaultfalse
)
#
offShortcut for removeEventListener
#
Parametersel
(Window | Document | HTMLElement)type
stringhandler
EventListeneroptions
(EventListenerOptions | boolean) (optional, defaultfalse
)
#
onceOne shot listener, it addEventListener
and removes it first time is called
with removeEventListener
#
Parametersel
(Window | Document | HTMLElement)type
stringhandler
EventListeneroptions
(EventListenerOptions | boolean) (optional, defaultfalse
)
#
createElementShortcut for document.createElement
, allowing to to create an HTML element
with a given className directly (a very common use case)
#
ParametersReturns HTMLElement
#
isHiddenIs element hidden?
#
Parametersel
HTMLElement?
Returns boolean
#
siblings- See: @glidejs/glide/src/utils/dom (source)
Finds siblings nodes of the passed node.
#
Parametersnode
Element
Returns Array
#
exists- See: @glidejs/glide/src/utils/dom (source)
Checks if passed node exist and is a valid element.
#
Parametersnode
Element
Returns Boolean
#
getDataAttrGet data attribute
#
Parameterselement
HTMLElementattribute
string The name of thedata-{attr}
Returns (string | null)
#
setDataAttrSet data attribute
#
Parameterselement
Elementattribute
string The name of thedata-{attr}
value
(string | number | null)? The value to set,null
orundefined
will remove the attribute
#
activeEventsListen: events delegation system
From: https://github.com/cferdinandi/events https://github.com/cferdinandi/events/blob/master/src/js/events/events.js
#
activeEventsActive events
#
ListenEventType: Object
#
Propertiesselector
string
#
getIndexGet the index for the listener
#
ParametersReturns number The index of the listener
#
getRunTargetCheck if the listener callback should run or not
#
Parameterstarget
Element The event.targetselector
(string | window | Document | Element) The selector/element to check the target against
Returns (window | Document | Element | false
) If not false, run listener and pass the targeted element to use in the callback
#
eventHandlerHandle listeners after event fires
#
Parametersevent
Event The event
#
eventType: Element
#
listenListen an event
#
Parameterstypes
string The event type or types (comma separated)selector
string The selector to run the event oncallback
#
unlistenStop listening for an event
#
Parameterstypes
string The event type or types (comma separated)selector
string The selector to remove the event fromcallback
Function The function to remove
#
listenOnceListen an event, and automatically unlisten it after it's first run
#
Parameterstypes
string The event type or types (comma separated)selector
string The selector to run the event oncallback
Function The function to run when the event fires
#
getListenersGet an immutable copy of all active event listeners
Returns Object Active event listeners
#
listenLoadedFires a callback when the DOM content is loaded
#
Parametershandler
EventHandlerNonNull
#
listenResizeListen window resize event using lodash's debounce
#
Parametershandler
EventHandlerNonNulldebounceOptions
Returns Function An automatic unbinding function to run to deregister the listener upon call
#
unbinderUnbind the previously attached scroll handler
#
unbinderUnbind the previously attached scroll handler
#
listenScrollListen window scroll event using lodash's debounce
#
Parametershandler
EventHandlerNonNulldebounceOptions
#
throttleThrottle for resize / scroll
#
ParametersReturns Function
#
emitEventEmit event (use only if the targeted browser supports CustomEvent
s)
#
Parameters#
getHeightGet element height
#
Parameterselement
HTMLElement
Returns number
#
getOffsetGet an element's distance from the top and left of the Document.
#
Parameterselem
HTMLElement The HTML node element
Returns {top: number, left: number} Distance from the top and left in pixels
#
getOffsetTopGet an element's distance from the top of the Document.
#
Parameterselem
HTMLElement The HTML node element
Returns number Distance from the top in pixels
#
getDocumentHeight- See: https://github.com/cferdinandi/smooth-scroll (credits)
Determine the document's height
Returns number
#
getScrollbarWidthGet scrollbar's current width
#
Parameterselement
HTMLElement?
Returns number
#
isTotallyScrolled- See: https://github.com/willmcpo/body-scroll-lock/blob/master/src/bodyScrollLock.js#L116
- See: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
Is element totally scrolled
#
Parametersel
HTMLElement?
Returns boolean
#
isInViewportDetermine if an element is in the viewport
(c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com
#
Parameterselem
Element The element
Returns boolean Returns true if element is in the viewport