DIV CSS 佈局教程網

JS 動態修改 CSS 樣式屬性:命名對照與實務要點
編輯:JavaScript基礎知識     

CSS 與 JavaScript 搭配,能做出許多靜態樣式表做不到的效果。要用 JS 動態改變某個標籤的樣式,就得先搞清楚一件事:CSS 屬性名與 JS 屬性名的寫法並不相同。

最基本的例子

例如要關閉一個漂浮廣告:

document.getElementById('ad').style.display = 'none';

它對應的 CSS 規則是:

.ad { display: none; }

兩者語意相同,但寫法規則不同:CSS 用小寫加連字號(background-color),JS 則要轉成小駝峰(backgroundColor)。原因是連字號在 JavaScript 中被當成減法運算子,style.background-color 是語法錯誤。

命名對照表

下表左欄為 CSS 語法(不區分大小寫),右欄為 JavaScript 語法(區分大小寫):

border → border  border-bottom → borderBottom  border-bottom-color → borderBottomColor  border-bottom-style → borderBottomStyle  border-bottom-width → borderBottomWidth

border-color → borderColor  border-left → borderLeft  border-left-color → borderLeftColor  border-left-style → borderLeftStyle  border-left-width → borderLeftWidth

border-right → borderRight  border-right-color → borderRightColor  border-right-style → borderRightStyle  border-right-width → borderRightWidth

border-style → borderStyle  border-top → borderTop  border-top-color → borderTopColor  border-top-style → borderTopStyle  border-top-width → borderTopWidth  border-width → borderWidth

clear → clear  float → floatStyle  display → display

margin → margin  margin-bottom → marginBottom  margin-left → marginLeft  margin-right → marginRight  margin-top → marginTop

padding → padding  padding-bottom → paddingBottom  padding-left → paddingLeft  padding-right → paddingRight  padding-top → paddingTop

background → background  background-attachment → backgroundAttachment  background-color → backgroundColor  background-image → backgroundImage  background-position → backgroundPosition  background-repeat → backgroundRepeat

color → color

list-style → listStyle  list-style-image → listStyleImage  list-style-position → listStylePosition  list-style-type → listStyleType

white-space → whiteSpace

font → font  font-family → fontFamily  font-size → fontSize  font-style → fontStyle  font-variant → fontVariant  font-weight → fontWeight

letter-spacing → letterSpacing  line-break → lineBreak  line-height → lineHeight

text-align → textAlign  text-decoration → textDecoration  text-indent → textIndent  text-justify → textJustify  text-transform → textTransform

vertical-align → verticalAlign

唯一真正的例外:float

整張對照表裡只有一個需要特別記的:float 在 JS 中要寫成 styleFloat 或 cssFloat,而不是 style.float。原因是 float 在 Java 等語言中是保留字,當年制訂 DOM 規範時為了避免衝突才改名。實務上 cssFloat 是標準寫法,styleFloat 是 IE 的相容寫法:

el.style.cssFloat = 'left';   // 標準
el.style.styleFloat = 'left'; // 舊 IE

更實用的幾個技巧

一次設定多個屬性,用 Object.assign 或展開語法比逐行赋值清楚:

Object.assign(el.style, {
  display: 'none',
  opacity: '0',
  transition: 'opacity .3s'
});

用 classList 切換樣式,而不是直接改 style。這通常是更好的做法,因為樣式定義留在 CSS 檔裡,JS 只負責切換狀態:

el.classList.add('is-hidden');
el.classList.toggle('active');
el.classList.remove('is-hidden');

自訂屬性要帶前綴。CSS 自訂屬性(俗稱 CSS 變數)在 JS 中必須用 setProperty 存取,且名稱要包含 --:

document.documentElement.style.setProperty('--main-color', '#83ACCF');
var c = getComputedStyle(el).getPropertyValue('--main-color');

讀取運算後的樣式要用 getComputedStyle,直接讀 el.style.xxx 只能拿到行內樣式,讀不到樣式表裡的值:

var width = getComputedStyle(el).width;

廠商前綴屬性在 JS 中也要駝峰化,例如 -webkit-transform 對應 WebkitTransform,-moz-transform 對應 MozTransform。現代專案多已不需要手動處理,PostCSS 的 autoprefixer 會自動補上。

用 setProperty 走字串路線

如果屬性名是動態組出來的,無法用點語法,可以改用 setProperty,它接受 CSS 原始寫法:

var prop = 'border-' + side + '-color';
el.style.setProperty(prop, 'red');

把這份對照表放進書籤,加上「記得 float 是例外」、「讀值要用 getComputedStyle」這兩點,動態改樣式基本就不會卡住了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved