JS Module to add custom styles to head tag
/*-------------------------------------------------------------//
HeadStyle.js
---------------------------------------------------------------//
Add Styles to head tag instead in jQuery's goofy inline style.
These styles can be over written by your own linked CSS and
removed as needed. For ES6, import the module like so:
import HeadStyle from 'modules/headStyle.js';
// Create a new constructor
var headStyle = new HeadStyle();
// Add Rules by passing an object as an argument:
headStyle.addRules({
'.classOne':'height: calc(100vh - 7rem);display:none;',
'.classTwo':'height: 80rem;width:100%;',
});
// Remove Created Styles:
headStyle.removeRules();
---------------------------------------------------------------*/
var headStyle = function() {
var head = document.getElementsByTagName('head')[0];
this.addRules = function(rules) {
var css = '',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
for (var property in rules) {
if (rules.hasOwnProperty(property)) {
var css = css + property + '{' + rules[property] + '}';
}
}
style.type = 'text/css';
style.setAttribute('id', 'customHeadStyle')
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
},
this.removeRules = function() {
var customHeadStyle = document.getElementById('customHeadStyle');
customHeadStyle.remove();
}
}
module.exports = headStyle;