1.0.11 • Published 9 months ago
@danielgindi/dom-utils v1.0.11
@danielgindi/dom-utils
A collection of dom utilities. So you can work natively with the dom without dom frameworks.
All functions have appropriate JSDocs.
lib/Css.js: A set of css helpersgetCssProps(el: Element, props: string[]): Object<string, string>setCssProps(el: ElementCSSInlineStyle, props: Object<string, string>)getElementWidth(el: Element, paddings: boolean = false, borders: boolean = false, margins: boolean = false): numbergetElementHeight(el: Element, paddings: boolean = false, borders: boolean = false, margins: boolean = false): numbergetPseudoElementWidth(el: Element, pseudo: string, paddings: boolean = false, borders: boolean = false, margins: boolean = false): numbergetPseudoElementHeight(el: Element, pseudo: string, paddings: boolean = false, borders: boolean = false, margins: boolean = false): numbersetElementWidth(el: Element, value: number, paddings: boolean = false, borders: boolean = false, margins: boolean = false)setElementHeight(el: Element, value: number, paddings: boolean = false, borders: boolean = false, margins: boolean = false)getElementOffset(el: Element): {top: number, left: number}anchoredPosition(el: Element, xSpec: 'left'|'center'|'right'|'start'|'end'|string|number, ySpec: 'top'|'center'|'bottom'|string|number, size: {width: number: height: number}, rtl?: boolean): {left: number, top: number, right: number, bottom: number, xSpec: string|number, ySpec: string|number}
lib/Dom.js: A set of dom helperscreateElement(tag: tag, attrs: Object<string, string>, children: Node|Node[]): ElementsetElementAttrs(el: Element, attrs: Object<string, string>)closestUntil(el: Element, selector: string, until?: Node): Node|nullprev(el: Element, selector: string): Element|nullnext(el: Element, selector: string): Element|null
lib/DomCompat.js: A set of compatibility functions, for several functions which are not available on all browsersgetRootNode(el: Node): Node|nullclosest(el: Element, selector: string):Node|nullremove(el: Element)before(before: ChildNode, ...nodes: (Node|string)[])after(after: ChildNode, ...nodes: (Node|string)[])prepend(parent: ParentNode, ...nodes: (Node|string)[])append(parent: ParentNode, ...nodes: (Node|string)[])scopedSelector(el: Element, selector: Element): Element|nullscopedSelectorAll(el: Element, selector: Element): NodeListOf<Element>toggleClass(el: Element, className: string, toggle?: boolean): boolean
lib/DomEventsSink.js: A class for registering/unregistering event listenersadd(el: EventTarget, eventName: string, handler: EventListenerOrEventListenerObject, optionsOrCapture?: boolean|AddEventListenerOptions): DomEventsSink
event names support namespacing, where'event.namespace'.remove(el?: EventTarget, eventName?: string, handler?: EventListenerOrEventListenerObject, optionsOrCapture?: boolean|AddEventListenerOptions): DomEventsSink
specify any argument as a filter for what to remove. specify '.namespace' event name in order to remove everything for a specific namespace.
Touches.js: Touch helpersbindTouchTap(el: Element, options: { distance: number = 9, handler: function(event: TouchEvent) }): { unbind: Function }
This little function will manage touch events to detect a single tap.
ScrollHelper.js: scroll helpers- You can use
getScrollLeft/setScrollLeftto normalize the behavior ofscrollLeftacross the quirks of different browsers. - You can use
getScrollHorz/setScrollHorzto record and restorescrollLeftpositions on RTL elements, or to switch between ltr/rtl modes without loosing scroll position. calculateNativeScrollLeftForLeft(el: Element, left: number, rtl?: boolean|undefined): number
Calculates a nativescrollLeftvalue for the specific element in the current browser.
The input value is a standardscrollLeftvalue (taken withgetScrollLeft).
You will use this function instead ofsetScrollLeftif you want to animate to this value, or other kinds of manipulation.rtlis optional, you can pass it if you already have the 'direction' of the element and want to avoid that extra calculation.getScrollLeft(el: Element, rtl?: boolean|undefined): number
Fetches thescrollLeftvalue from an element, and normalizes across the different scroll methods to a unified method.
You can later usesetScrollLeftto set a value back to the element.rtlis optional, you can pass it if you already have the 'direction' of the element and want to avoid that extra calculation.setScrollLeft(el: Element, value: number, rtl?: boolean|undefined)
Sets thescrollLefton an element.rtlis optional, you can pass it if you already have the 'direction' of the element and want to avoid that extra calculation.
This function usescalculateNativeScrollLeftForLeft(...)to calculate the nativescrollLeft.calculateNativeScrollLeftForHorz(el: Element, left: number, rtl?: boolean|undefined): number
Calculates a nativescrollLeftvalue for an element, from an rtl-awarescrollLeft(i.e fromgetScrollHorz()). You will use this function instead ofsetScrollHorzif you want to animate to this value, or other kinds of manipulation.rtlis optional, you can pass it if you already have the 'direction' of the element and want to avoid that extra calculation.getScrollHorz(el: Element, rtl?: boolean|undefined): number
Gets an rtl-awarescrollLeftvalue from an element. The value will behave like a standardscrollLeft, except that for an rtl element it will have an rtl meaning, instead of being some crazy number.rtlis optional, you can pass it if you already have the 'direction' of the element and want to avoid that extra calculation.setScrollHorz(el: Element, value: number, rtl?: boolean|undefined)
Sets thescrollLefton an element, with an rtl-aware value.rtlis optional, you can pass it if you already have the 'direction' of the element and want to avoid that extra calculation.
This function usescalculateNativeScrollLeftForHorz(...)to calculate the nativescrollLeft.
- You can use
Me
- Hi! I am Daniel Cohen Gindi. Or in short- Daniel.
- danielgindi@gmail.com is my email address.
- That's all you need to know.
Help
If you want to help, you could:
License
All the code here is under MIT license. Which means you could do virtually anything with the code. I will appreciate it very much if you keep an attribution where appropriate.
The MIT License (MIT)
Copyright (c) 2013 Daniel Cohen Gindi (danielgindi@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.