萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 操作 CSS 樣式屬性完整對照表:CSS 命名轉駝峰、分類速查、特殊屬性處理

JS 操作 `element.style` 時,CSS 屬性名需轉為**駝峰命名法**(`background-color` → `backgroundColor`)。本文提供完整分類對照表,涵蓋盒模型、定位、視覺格式化、字體、文字、顏色背景、表格、列表、生成內容等 9 大類 100+ 屬性,並說明特殊屬性(`float`、`cssText`、CSS 變數)處理方式。

命名轉換規則

  • 連字號分隔 → 駝峰:`font-size` → `fontSize`、`z-index` → `zIndex`。
  • 前綴 `-webkit-` → `Webkit` (大寫 W):`-webkit-transform` → `webkitTransform`。
  • 保留字 `float` → `cssFloat` (標準) / `styleFloat` (IE舊版)。
  • 通用:`element.style['font-size']` 字串鍵值也可用,但駝峰為標準。

完整分類對照表 (節選重點)

盒模型

CSSJS
width / heightwidth / height
margin / margin-top / -right / -bottom / -leftmargin / marginTop / Right / Bottom / Left
padding / padding-top / ...padding / paddingTop / ...
border / border-width / -style / -color / -top / ...border / borderWidth / Style / Color / Top / ...
box-sizingboxSizing
overflow / overflow-x / -yoverflow / overflowX / Y

定位與視覺格式化

CSSJS
positionposition
top / right / bottom / lefttop / right / bottom / left
z-indexzIndex
displaydisplay
visibilityvisibility
floatcssFloat / styleFloat
clearclear
clip / clip-pathclip / clipPath
opacityopacity

Flexbox / Grid (現代佈局)

CSSJS
flex / flex-grow / -shrink / -basis / -direction / -wrap / -flow / justify-content / align-items / align-content / align-self / order / gapflex / flexGrow / Shrink / Basis / Direction / Wrap / Flow / justifyContent / alignItems / alignContent / alignSelf / order / gap
grid / grid-template-columns / -rows / -areas / -column / -row / gap / column-gap / row-gap / auto-flow / auto-columns / auto-rowsgrid / gridTemplateColumns / Rows / Areas / Column / Row / gap / columnGap / rowGap / autoFlow / autoColumns / autoRows

字體與文字

CSSJS
font / font-family / -size / -weight / -style / -variant / -stretch / -size-adjustfont / fontFamily / Size / Weight / Style / Variant / Stretch / SizeAdjust
colorcolor
text-align / -decoration / -indent / -transform / -overflow / -shadow / line-height / letter-spacing / word-spacing / white-space / word-break / overflow-wrap / tab-sizetextAlign / Decoration / Indent / Transform / Overflow / Shadow / lineHeight / letterSpacing / wordSpacing / whiteSpace / wordBreak / overflowWrap / tabSize

背景與邊框進階

CSSJS
background / -color / -image / -position / -size / -repeat / -attachment / -origin / -clip / -blend-modebackground / Color / Image / Position / Size / Repeat / Attachment / Origin / Clip / BlendMode
border-radius / -top-left / -top-right / -bottom-right / -bottom-leftborderRadius / TopLeft / TopRight / BottomRight / BottomLeft
box-shadowboxShadow
filter / backdrop-filterfilter / backdropFilter

變形、動畫、轉場

CSSJS
transform / transform-origin / transform-style / perspective / perspective-origin / rotate / scale / translatetransform / transformOrigin / transformStyle / perspective / perspectiveOrigin / rotate / scale / translate (個別屬性現代瀏覽器支援)
transition / -property / -duration / -timing-function / -delaytransition / TransitionProperty / Duration / TimingFunction / Delay
animation / -name / -duration / -timing-function / -delay / -iteration-count / -direction / -fill-mode / -play-stateanimation / AnimationName / Duration / TimingFunction / Delay / IterationCount / Direction / FillMode / PlayState

特殊屬性處理

1. `cssText` 一次性讀寫完整內聯樣式

el.style.cssText = 'color: red; font-size: 14px;'; // 覆寫
el.style.cssText += '; background: blue;'; // 追加
console.log(el.style.cssText); // "color: red; font-size: 14px; background: blue;"

2. CSS 自訂屬性 (CSS Variables)

// 設定
el.style.setProperty('--main-color', '#007bff');
el.style.setProperty('--gap', '16px');

// 獲取
getComputedStyle(el).getPropertyValue('--main-color'); // "#007bff"
el.style.getPropertyValue('--main-color'); // 僅內聯設定值

3. `float` 關鍵字衝突

el.style.cssFloat = 'left';      // 標準
el.style.styleFloat = 'left';    // IE ≤ 8
// 推薦統一
el.style.float = 'left';         // 現代瀏覽器支援 style.float 別名

效能建議

  • 批量修改用 `cssText` 或 `classList` 切換預定義類別。
  • 讀取計算值用 `getComputedStyle(el).prop`,非 `el.style.prop` (後者只讀內聯)。
  • 動畫屬性優先用 `transform`/`opacity` (合成器層),避免觸發佈局/繪製。
  • 使用 `will-change` 提示瀏覽器優化。

收藏此表,查找 JS 樣式屬性名不再猜測,寫出零錯誤的動態樣式代碼。

copyright © 萬盛學電腦網 all rights reserved