min.js
v2.0.6 / 2015-12-02
A super tiny JavaScript library to execute simple DOM querying, hooking name spaced event listeners, trigger events and some simple DOM node helpers.
This is not jQuery or a jQuery replacement - more a convenience library to help you type less when writing vanilla JS. It intentionally doesn't try to replicate jQuery's API in order to keep its size down and encourage you to write vanilla JS on your own.
Browser Compatibility
Uses querySelectorAll, addEventListener, getComputedStyle and Object.keys. If the browser doesn't support these it stops running.
Qunit tests: http://www.thirteentwelve.com/minjs/test/test.html
Tests pass in:
- Chrome 41+ (OSX)
- Chrome 41+ (Windows 7, Android 5, Android 4.4.4)
- Safari 5, 8, 9 (OSX)
- Safari 7.1, 8.3 (iOS)
- Firefox 4+ (OSX)
- Firefox 34+ (Windows 7)
- IE9, IE10, IE11 (Windows 7)
- Android 2.3.7, 4.1.1, 4.2.2, 4.3, 4.4.4, 5, 5.1 native browser
Filesize
- ~6kb uncompressed
- ~2kb minified
- ~1kb minified and gzipped
Query elements
var divs = ; // minjs object
Creates minjs object: an array of nodes with prototype methods.
Optionally you can supply a context in which to look:
var node = document; // #content nodevar divs = ; // minjs object
You can also pass in a node, to turn it into a minjs object:
var node = document; // #content nodevar container = ; // minjs object
To return nodes from the minjs object:
var divs = ;var first_div = divs0; // node
$ Selector
min.js isn't jQuery. You can, of course, bind $ to min$:
window$ = min$;
(after min$ and before the rest of your JavaScript)
And then:
var divs = ; // minjs object
Events
Bind events with on()
Basic:
;
Or with a namespace:
;
Note:
- only accepts singular events and singular namespaces
- must contain an event type (namespace is optional)
Unbind events with off()
; // clears all handlers; // clears just the click handlers; // clears just foo namespaced click handlers; // clears foo namespaced handlers
Custom events
;
Trigger events
min$(document).trigger('foo');
## Looping
### Looping elements
```js
min$('p').each(function(el,index) {
console.log(el.innerHTML); // node's inner HTML
});
To break a loop, return false:
// assume you have 5 p's;// 0, 1
Looping any array
var my_arr = "a""b""c";min$;// 0 "a", 1 "b", 2 "c"
Index
Search for a given element in a collection.
var node = document; // p#foo nodevar i = indexnode; // number
var p_foo = ; // p#foo minjs objvar i = indexp_foo; // number
If a match isn't found the number returned is -1.
If nothing is passed then the returned number will be that of the first child of the collection, likely 0.
Chaining events
;
Add, remove, has CSS class
; // adds class to all links; // removes class from all linksvar is_foo = ; // assumes the first item, returns true/false
Read/write attributes
; // returns contents of attribute for first returned element; // sets attribute on all divs
Read/write CSS styles
; // returns computed value of color; // sets style; // sets multiple styles
Assumes the first item if passed a collection larger than 1.
Careful with reading shorthand properties in Firefox, it doesn't handle them like Webkit Bugzilla - Bug 137688
Extending
You can extend min$ by adding to its prototype:
min$prototype{ min$; // allow for chaining return this;};
Then you could use:
; // HTML, HEAD, META, TITLE, LINK..
Or aliased:
window$ = min$;$; // HTML, HEAD, META, TITLE, LINK..
'this' inside your function, is the minjs object, which is an array like object with the minjs methods on its prototype. To loop a collection, use the internal minjs each method.Returning 'this' at the end allows for chaining.
This example returns the offset of an element. If your collection has more than one element, it only returns the offset for the first.
min$prototype{ var node = thislength > 0 ? this0 : this; if documentbody var rect = node return top: recttop + documentbodyscrollTop left: rectleft + documentbodyscrollLeft ; else return null; };
Then you could use:
; // Object {top: 300, left: 20}
Alternatively you can extend minjs itself. Here is an example of a method to merge objects together:
min${ var merged = {}; for var def in obj1 mergeddef = obj1def; for var def in obj2 mergeddef = obj2def; return merged;};
Then you could use:
min$; // Object {foo: "bar", bar: "foo"}
Or aliased:
window$ = min$;$; // Object {foo: "bar", bar: "foo"}
Alternatives
These libraries aim to replicate the jQuery API in a more complete way:
Inspiration
minjs is inspired by Remy Sharp's minjs. Remy chose to extend the global node prototype where as this minjs has its own prototype. Initially I forked his repo and added a few useful things and then nedbaldessin gently encouraged me to not use the global node prototype.
As used on the Internet
- Krrb blog by mrdoinel
- Dering Hall by myself
- Scientific American by myself and joecritch
- Library of America by helloimseth
If you used it in a project, please let me know!
Thanks
More info
License: MIT