Piszę sobie bibliotekę, która ma być lżejszą, modularną wersją jQ (robię to bardziej dla własnej satysfakcji, nie sądzę, żeby ktoś chciał korzystać). Aktualnie tworzę funkcję hide(). Wydaje mi się, że nie ma co pisać wszystkiego w JS skoro jest CSS 3, napisałem więc taki kod:
function addCss(styles) {
const css = document.createElement('style');
css.type = 'text/css';
css.textContent = styles;
document.head.appendChild(css);
}
function cssSearch(name) {
for(let i = 0; i < document.styleSheets.length; i++)
if(document.styleSheets[i].cssRules) {
for(let y = 0; y < document.styleSheets[i].cssRules.length; y++) {
if(document.styleSheets[i].cssRules[y].name === name) {
return true;
}
}
}
}
function hide(time = 0) {
if(!cssSearch('q-h')) {
addCss('@keyframes q-h{0%{opacity:1}100%{opacity:0;transform:translateY(-100%);visibility:hidden}}');
}
this.each(el => el.style.animation = `${time}ms q-h forwards`);
return this;
}
Teraz pytanie: czy to optymalne rozwiązanie?