Dom7 - Custom DOM Library
Framework7 doesn’t use any third party library, even for DOM manipulation. It has its own custom DOM7 - DOM library that utilizes most edge and high-performance methods for DOM manipulation. You don’t need to learn something new, its usage is very simple because it has the same syntax as well known jQuery library with support of the most popular and widely used methods and jQuery-like chaining.
To start use it there is a Dom7
global window function, but we recommend to assign it to some local variable with more handy name, like $$
, but not to “$” to prevent confilicts with other libraries like jQuery or Zepto:
//Export DOM7 to local variable to make it easy accessable
var $$ = Dom7;
Usage Example
Just everything you already know:
$$('.something').on('click', function (e) {
$$(this).addClass('hello').attr('title', 'world').insertAfter('.something-else');
});
Available Methods
All these methods works almost in the same way and with the same arguments as in jQuery or Zepto:
$$(window).trigger('resize');
Classes | |
---|---|
.addClass(className) | Add class to elements
|
.removeClass(className) | Remove specified class
|
.hasClass(className) | Determine whether any of the matched elements are assigned the given class:
|
.toggleClass(className) | Remove (if class is present) or add (if not) one or more classes from each element in the set of matched elements:
|
Attributes and properties | |
.prop(propName) | Get property value:
|
.prop(propName, propValue) | Set single property value:
|
.prop(propertiesObject) | Set multiple properties:
|
.attr(attrName) | Get attribute value:
|
.attr(attrName, attrValue) | Set single attribute value:
|
.attr(attributesObject) | Set multiple attributes:
|
.removeAttr(attrName) | Remove specified attribute:
|
.val() | Get the current value of the first element in the set of matched elements
|
.val(newValue) | Set the value of every matched element
|
Data storage | |
.data(key, value) | Store arbitrary data associated with the matched elements
|
.data(key) | Return the value at the named data store for the first element in the collection, as set by data(key, value) or by an HTML5 data- attribute
or
|
.removeData(key) | Remove specified data
|
Data Set | |
.dataset() | Returns element’s data set (set of data- attributes) as plain Object
|
CSS transform, transitions | |
.transform(CSSTransformString) | Adds prefixed CSS transform property:
|
.transition(transitionDuration) | Set CSS transition-duration property to collection:
|
Events | |
.on(eventName, handler, useCapture) | Add event handler function to one or more events to the selected elements.
|
.on(eventName, delegatedTarget, handler, useCapture) | Live/delegated event handler
|
.once(eventName, handler, useCapture) | Add event handler function to one or more events to the selected elements that will be executed only once
|
.once(eventName, delegatedTarget, handler, useCapture) | Live/delegated event handler that will be executed only once
|
.off(eventName, handler, useCapture) | Remove event handler
|
.off(eventName, delegatedTarget, handler, useCapture) | Remove live/delegated event handler
|
.trigger(eventName, eventData) | Execute all handlers added to the matched elements for the specified event |
.transitionEnd(callback, permanent) | Adds prefixed transitionEnd event handler to collection:
|
.animationEnd(callback) | Adds prefixed animationEnd event handler to collection:
|
Styles | |
.width() | Get the current computed width for the first element in the set of matched elements
|
.outerWidth([includeMargin]) | Get the current computed width for the first element in the set of matched elements, including padding and border, and margin (if includeMargin is true)
|
.height() | Get the current computed height for the first element in the set of matched elements
|
.outerHeight([includeMargin]) | Get the current computed height for the first element in the set of matched elements, including padding and border, and margin (if includeMargin is true)
|
.offset() | Get the current coordinates of the first element relative to the document
|
.hide() | Set “display:none” to the matched elements
|
.show() | Set “display:block” to the matched elements
|
.css(property) | Get value of specified CSS property for the first element:
|
.css(property, value) | Set specified CSS property to the matched elements:
|
.css(propertiesObject) | Set multiple CSS properties to the matched elements:
|
Scroll | |
.scrollTop() | Get scrollTop position of element |
.scrollTop(position, duration, callback) | Set scrollTop “position” with animation during “duration” (in ms). Scroll top position will be set immediately if duration is not specified. If you have specified “callback” function, then it will be executed after scrolling completed |
.scrollLeft() | Get scrollLeft position of element |
.scrollLeft(position, duration, callback) | Set scrollLeft “position” with animation during “duration” (in ms). Scroll left postion will be set immediately if duration is not specified. If you have specified “callback” function, then it will be executed after scrolling completed |
.scrollTo(left, top, duration, callback) | Set scroll left and scroll top with animation during “duration” (in ms). Scroll postion will be set immediately if duration is not specified. If you have specified “callback” function, then it will be executed after scrolling completed |
Dom Manipulation | |
.add(elements) | Create a new Dom7 collection with elements added to the set of matched elements:
|
.each(callback) | Iterate over collection, executing a callback function for each matched element |
.html() | Get the HTML contents of the first element in the set of matched elements |
.html(newInnerHTML) | Set the HTML contents of every matched element |
.text() | Get the text contents of the first element in the set of matched elements |
.text(newTextContent) | Set the text contents of every matched element |
.is(CSSSelector) | Check the current matched set of elements against CSS selector |
.is(HTMLElement) | Check the current matched set of elements against HTML element or Dom7 collection |
.index() | Return the position of the first element within the Dom7 collection relative to its sibling elements |
.eq(index) | Reduce the set of matched elements to the one at the specified index |
.append(HTMLString) | Insert content, specified by the parameter, to the end of each element in the set of matched elements |
.append(HTMLElement) | Insert specified HTML element to the end of element in the set of matched elements |
.appendTo(HTMLElement) | Insert content/elements, to the end of element specified in parameter |
.prepend(newHTML) | Insert content, specified by the parameter, to the beginning of each element in the set of matched elements |
.prepend(HTMLElement) | Insert specified HTML element to the beginning of element in the set of matched elements |
.prependTo(HTMLElement) | Insert content/elements, to the beginning of element specified in parameter |
.insertBefore(target) | Insert every element in the set of matched elements before the target. Target could be specified as CSS selector or HTML element or Dom7 collection |
.insertAfter(target) | Insert every element in the set of matched elements after the target. Target could be specified as CSS selector or HTML element or Dom7 collection |
.next([selector]) | Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector |
.nextAll([selector]) | Get all following siblings of each element in the set of matched elements, optionally filtered by a selector |
.prev([selector]) | Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector |
.prevAll([selector]) | Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector |
.siblings([selector]) | Get the siblings of each element in the set of matched elements, optionally filtered by a selector |
.parent([selector]) | Get the first parent of each element in the current set of matched elements, optionally filtered by a selector |
.parents([selector]) | Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector |
.closest([selector]) | For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree |
.find(selector) | Get the descendants of each element in the current set of matched elements, filtered by a selector |
.children(selector) | Get the children of each element in the set of matched elements, optionally filtered by a selector |
.filter(callback) | Filter collection of elements
|
.remove() | Remove/detach matched elements from the Dom |
.empty() | Remove all child nodes of the set of matched elements from the DOM. Alias for .html(‘’) |
Shortcuts | |
.click() | Trigger “click” event on collection |
.click(handler) | Add “click” event handler to collection |
.blur() | Trigger “blur” event on collection |
.blur(handler) | Add “blur” event handler to collection |
.focus() | Trigger “focus” event on collection |
.focus(handler) | Add “focus” event handler to collection |
.focusin() | Trigger “focusin” event on collection |
.focusin(handler) | Add “focusin” event handler to collection |
.focusout() | Trigger “focusout” event on collection |
.focusout(handler) | Add “focusout” event handler to collection |
.keyup() | Trigger “keyup” event on collection |
.keyup(handler) | Add “keyup” event handler to collection |
.keydown() | Trigger “keydown” event on collection |
.keydown(handler) | Add “keydown” event handler to collection |
.keypress() | Trigger “keypress” event on collection |
.keypress(handler) | Add “keypress” event handler to collection |
.submit() | Trigger “submit” event on collection |
.submit(handler) | Add “submit” event handler to collection |
.change() | Trigger “change” event on collection |
.change(handler) | Add “change” event handler to collection |
.mousedown() | Trigger “mousedown” event on collection |
.mousedown(handler) | Add “mousedown” event handler to collection |
.mousemove() | Trigger “mousemove” event on collection |
.mousemove(handler) | Add “mousemove” event handler to collection |
.mouseup() | Trigger “mouseup” event on collection |
.mouseup(handler) | Add “mouseup” event handler to collection |
.mouseenter() | Trigger “mouseenter” event on collection |
.mouseenter(handler) | Add “mouseenter” event handler to collection |
.mouseleave() | Trigger “mouseleave” event on collection |
.mouseleave(handler) | Add “mouseleave” event handler to collection |
.mouseout() | Trigger “mouseout” event on collection |
.mouseout(handler) | Add “mouseout” event handler to collection |
.mouseover() | Trigger “mouseover” event on collection |
.mouseover(handler) | Add “mouseover” event handler to collection |
.touchstart() | Trigger “touchstart” event on collection |
.touchstart(handler) | Add “touchstart” event handler to collection |
.touchend() | Trigger “touchend” event on collection |
.touchend(handler) | Add “touchend” event handler to collection |
.touchmove() | Trigger “touchmove” event on collection |
.touchmove(handler) | Add “touchmove” event handler to collection |
.resize(handler) | Add “resize” event handler to collection |
.scroll(handler) | Add “scroll” event handler to collection |
Animation
.animate(properties, parameters)- Perform a custom animation of a set of CSS properties
- properties - object - CSS properties to animate
- parameters - object - animation parameters
returns Dom7 collection
$$('#animate-me').animate(
/* CSS properties to animate, e.g.: */
{
'margin-left': 100,
'width': 200,
'height': 300,
'opacity': 0.5
},
/* Animation parameters */
{
// Animation duraion in ms, optional (default to 300)
duration: 300,
// Animation easing, optional (default to 'swing'), can be also 'linear'
easing: 'swing',
/* Callbacks */
// Animation begins, optional
begin: function (elements) {
console.log('animation began');
},
// Animation completed, optional
complete: function (elements) {
console.log('animation completed');
},
// Animation in progress, optional
progress: function (elements, complete, remaining, start) {
/* Where
complete - The call's completion percentage (as a decimal value)
remaining - How much time remains until the call completes (in ms)
start - The absolute time at which the call began (in ms)
*/
console.log('animation in progress');
}
}
);
It also supports chaining que:
$$('#animate-me')
.animate(
{
'margin-left': 100,
'width': 200,
'height': 300,
'opacity': 0.5
}
)
.animate(
{
'width': 50,
'height': 50
}
);