Skip to content

Instantly share code, notes, and snippets.

@IlyaZha
Created September 19, 2018 08:50
Show Gist options
  • Save IlyaZha/cae8c1e401a5b56dbab18cd7033feae1 to your computer and use it in GitHub Desktop.
Save IlyaZha/cae8c1e401a5b56dbab18cd7033feae1 to your computer and use it in GitHub Desktop.
Копирование стиля с одного элемента на другой Copying style from one element to another
/*
mainElement - the element from which we copy styles
secondElement - element to which we apply styles
Code works similar in Chrome and Firefox. I haven't tested it in Safary
*/
//If button is hidden, styles are copying badly. So we show main element
var display = mainElement.style.display;
mainElement.style.cssText = 'display:block!important';
var style = Object(window.getComputedStyle(mainElement, ""));
var cssText = '';
$.each(style, function (propCount, key) {
var value = style[key];
//Ignoring some styles from copying:
if (key.indexOf('width') > -1 || key.indexOf('padding') > -1 || (key === 'display' && value === 'none')) {
return 'continue';
}
cssText += key + ':' + value + ';';
});
secondElement.style.cssText = cssText;
secondElement.style.padding = 0;
secondElement.style['border-width'] = 0;
secondElement.style['min-width'] = style.width;
secondElement.style['max-width'] = style.width;
secondElement.className += " opacity-on-hover";
mainElement.style.display = display; //Applying initial display to mainElement
//You can't copy hover, so add it if you need
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment