在 border-radius 尚未普及的年代,要做出圓角框只能靠一層層 1px 高的 DIV 去「堆」出弧度。這個 rounder 插件就是把整套做法封裝起來,只要呼叫一行就能替任意元素套上圓角邊框。
$('.test').rounder();
用預設設定就會產生一個淺灰邊框、白底的圓角框:
當然,圓角處會有一點鋸齒,這是純 DIV 模擬無法避免的物理極限。若只是這樣,那這個插件也沒什麼意義——真正的價值在於樣式可以自由覆寫:
$('.test').rounder({ borderColor: 'red', backgroundColor: '#EEE', color: 'blue' });
核心概念是:外層包一個容器 .mapping_rounder,裡頭上下各放三層逐漸內縮的細條模擬圓弧,中間夾真正的內容區。程式碼如下:
(function($){
$.fn.rounder = function(options){
var setting = $.extend({
backgroundColor: '#FFF',
borderColor: '#AAA',
color: '#000'
}, options || {});
this.each(function(){
var source = $(this);
var container = source.parents(".mapping_rounder");
if (container.size() <= 0) {
container = $('<div class="mapping_rounder"></div>');
source.before(container);
// 上下各三層 1px DIV,中間夾內容
container.append(
'<div class="rounder_px3"></div><div class="rounder_px2"></div>'
+ '<div class="rounder_px1"></div><div class="rounder_px0"></div>'
+ '<div class="rounder_content"></div>'
+ '<div class="rounder_px0"></div><div class="rounder_px1"></div>'
+ '<div class="rounder_px2"></div><div class="rounder_px3"></div>'
);
container.find('.rounder_content').append(source);
// 保持原有的形態:高度、寬度、行高與四邊 margin
container.width(source.width());
source.width(source.width() - 12); // 左右各扣掉邊框佔寬
container.height(source.height());
source.height(source.height() - 8); // 上下各扣掉圓弧佔高
source.css('lineHeight', source.height() + 'px');
container.css('marginTop', source.css('marginTop'));
source.css('marginTop', 0);
container.css('marginBottom', source.css('marginBottom'));
source.css('marginBottom', 0);
container.css('marginLeft', source.css('marginLeft'));
source.css('marginLeft', 0);
container.css('marginRight', source.css('marginRight'));
source.css('marginRight', 0);
}
// 依設定替各層上色,堆出圓角
container.find('.rounder_px3').css('backgroundColor', setting.borderColor);
container.find('.rounder_px2').css({borderColor: setting.borderColor, backgroundColor: setting.backgroundColor});
container.find('.rounder_px1').css({borderColor: setting.borderColor, backgroundColor: setting.backgroundColor});
container.find('.rounder_px0').css({borderColor: setting.borderColor, backgroundColor: setting.backgroundColor});
container.find('.rounder_content').css({borderColor: setting.borderColor, backgroundColor: setting.backgroundColor});
// 清掉元素原本的邊框與底色,交給外層呈現
source.css('borderStyle', 'none');
source.css('backgroundColor', setting.backgroundColor);
source.css('color', setting.color);
});
}
})(jQuery);
這些樣式理論上都能改用 jQuery 動態套用,但那樣會多出不少程式碼,所以這裡選擇寫在樣式表裡:
.rounder_content{padding:0 5px;border-left:1px solid;border-right:1px solid;}
.rounder_px0{margin:0;height:2px;border-left:2px solid;border-right:2px solid;overflow:hidden;}
.rounder_px1{margin:0 1px;height:1px;border-left:2px solid;border-right:2px solid;overflow:hidden;}
.rounder_px2{margin:0 2px;height:1px;border-left:3px solid;border-right:3px solid;overflow:hidden;}
.rounder_px3{margin:0 3px;height:1px;background:#AAA;overflow:hidden;}
每層的 margin 逐步內縮,搭配遞增的左右 border 寬度,視覺上就形成一道平滑的弧線。特別要說明的是 overflow:hidden:這一行是為了相容 IE6,因為 IE6 底下的 DIV 有一個預設最小高度(大約 13px),不加上就無法把細條壓到 1px。
把圓角與焦點事件結合起來,就能做出取得焦點時換色、失去焦點時復原的輸入框:
<script type="text/javascript">
$(document).ready(function(){
$('.test').rounder({ borderColor: '#AAA', color: '#000' });
$('.test').focus(function(event){
$(event.target).rounder({ borderColor: 'red', backgroundColor: '#EEE', color: 'blue' });
});
$('.test').blur(function(event){
$(event.target).rounder({ borderColor: '#AAA', color: '#000' });
});
});
</script>
優點:體積小,兩個檔案壓縮後合計僅 2.23KB;使用方式簡單,一行搞定。
不足:圓角弧度與線條粗細無法調整(若有這個需求建議改用 jquery.corner 這類插件);高度與字級的搭配不夠完美,字太大時會被遮住一半。測試環境涵蓋 IE6、Firefox、Opera、Safari 與 Chrome。
以今天的標準看,這套做法早已被 border-radius 取代;但它示範的「多層元素堆疊 + 動態上色」思路,在需要支援老舊環境的專案裡偶爾還是派得上用場。