1.0.2 • Published 10 years ago
dom-create-element v1.0.2
dom-create-element
Create DOM elements.
Usage
npm install dom-create-element
var create = require('dom-create-element');
var el = create({
selector: 'div',
id: 'el-id',
styles: 'el-classes',
children: create({
selector: 'a',
link: 'http://google.com',
target: '_blank',
html: 'Link'
})
});
document.body.appendChild(el);Available options:
selector: the string type tag of the HTML element (required)id: unique ID selector of the elementstyles: CSS classes of the element (this is not inline styles)attr: custom attributeslink: ifselectoris a<a>element, provide ahref(required)target: ifselectoris a<a>element, you can define targetsrc: ifselectoris an<img>element, provide asrclazyload: ifselectoris an<img>element, you can definelazyload(see below)html: the content of the element (basically innerHTML)children: the children of the element. could be another instance ofcreateso you could chain element creation
For images, you can pass lazyload: true.
This will set the image to opacity: 0, then go back to 1 when it's loaded.
var image = create({
selector: 'img',
src: '/path/to/img.jpg',
lazyload: true
});TODOs
- lazyload for images
- cleaner options:
optshould first be the selector with more options as second parameter - tests
License
MIT, see LICENSE.md for details.