DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML和Xhtml
自訂 checkbox 樣式,相容 IE8/9/10 與 Firefox 的做法
編輯:HTML和Xhtml     

說來慚愧,這麼常用的 checkbox 要換外觀,我居然得靠搜尋,更麻煩的是繞了很久還找不到可行方案。後來在 csscheckbox.com 上看到一堆樣式,一測相容性,IE 直接不行,時間就這樣耗掉了。

抄近路屢屢碰壁,最後回到 jQuery UI。不必多說,相容性沒問題,樣式又能自己隨意畫。以下記錄一款 checkbox 樣式,給大家參考,也方便我以後取用。

 

它有三種狀態:default、hover 與 active,實測能完整相容 IE8/9/10 與 Firefox 等瀏覽器。做法是用一張精靈圖(sprite)切出三個狀態的位移,再分別掛到 jQuery UI 的 state class 上。

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#check" ).button();
$( "#format" ).buttonset();
});;
</script>
<style>
.ui-button-text-only .ui-button-text {
padding: 8px;
}
.ui-state-default,
.ui-widget-content .ui-state-default,
.ui-widget-header .ui-state-default {
background: url(images/safari-checkbox.png) 0 0 no-repeat; border:none;
}
.ui-state-hover,
.ui-widget-content .ui-state-hover,
.ui-widget-header .ui-state-hover,
.ui-state-focus,
.ui-widget-content .ui-state-focus,
.ui-widget-header .ui-state-focus {
background: url(images/safari-checkbox.png) -16px 0 no-repeat; border:none;
}
.ui-state-active,
.ui-widget-content .ui-state-active,
.ui-widget-header .ui-state-active {
background: url(images/safari-checkbox.png) 0 -16px no-repeat; border:none;
}
</style>
</head>
<body>
<input type="checkbox" id="check" /><label for="check"></label>
</body>
</html>

關鍵在 background-position:default 用 0 0、hover 用 -16px 0、active 用 0 -16px,也就是同一張圖橫向與縱向各偏移一個圖示寬高。只要替換掉 safari-checkbox.png,就能換成自己的視覺設計。

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