Savelyev_Vyacheslav

CopyStyle

Sep 16th, 2024
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function copyStylesForCSS(element) {
  2.     const styles = window.getComputedStyle(element);
  3.     let styleString = '';
  4.  
  5.     // Список стилей, которые нужно включить
  6.     const allowedStyles = [
  7.         'min-height', 'max-width', 'height', 'width', 'padding', 'margin',
  8.         'grid-gap', 'gap', 'display', 'grid-template-areas', 'grid-template-rows',
  9.         'grid-template-columns', 'grid-area', 'flex-direction', 'background-color',
  10.         'color', 'border-bottom', 'background', 'background-repeat', 'background-position',
  11.         'font-family', 'font-style', 'font-weight', 'font-size', 'line-height',
  12.         'letter-spacing', 'text-decoration', 'text-overflow', 'overflow-wrap',
  13.         'position', 'top', 'left', 'right', 'z-index', 'border-radius', 'transition',
  14.         'cursor', 'box-shadow', 'overflow-wrap', 'opacity', 'overflow', 'cursor', 'grid-area'
  15.     ];
  16.  
  17.     for (let i = 0; i < styles.length; i++) {
  18.         const styleName = styles[i];
  19.         const styleValue = styles.getPropertyValue(styleName);
  20.  
  21.         // Проверяем, что стиль не равен его значению по умолчанию и входит в список разрешенных стилей
  22.         if (allowedStyles.includes(styleName) && styleValue && styleValue !== 'auto' && styleValue !== 'none' && styleValue !== 'normal') {
  23.             styleString += `${styleName}: ${styleValue};\n`;
  24.         }
  25.     }
  26.    
  27.     console.log(styleString);
  28. }
  29.  
  30. const item = document.querySelector('.vvp-header-menu__item');
  31. copyStylesForCSS(item);
  32.  
Advertisement
Add Comment
Please, Sign In to add comment