Created
September 19, 2018 08:50
-
-
Save IlyaZha/cae8c1e401a5b56dbab18cd7033feae1 to your computer and use it in GitHub Desktop.
Копирование стиля с одного элемента на другой Copying style from one element to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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