DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 自寫的一個jQuery圓角插件
自寫的一個jQuery圓角插件
編輯:JQuery特效代碼     
原理是利用1px的div,具體實現看代碼。
使用方法:
代碼如下:
$('.test').rounder();

這樣會根據默認的設置產生一個圓角框,效果如圖:

圓角處會有點鋸齒:(
如果僅此而已,那肯定是不夠的。我們會想加上自己的一個樣式該怎麼辦?使用方法:
代碼如下:
$('.test').rounder({borderColor:'red',backgroundColor:'#EEE',color:'blue'});

效果如圖:

接下來我就來講講實現過程了,先附上jQuery代碼如下:
代碼如下:
(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);
//添加1pxDIV
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);
//保持原有的形態,如:高度、寬度等
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);
}
//給1pxDIV添加樣式以產生圓角邊框的效果
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);

CSS文件代碼:
代碼如下:
.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;}

本來這個CSS文件的樣式都可以用jQuery加上去,但那樣會多很多代碼,且讓我在此偷下懶- -|||。樣式裡面加上overflow:hidden;的目的是為了兼容IE6,因為在IE6裡面DIV會有個默認的最小高度,好像是13px。
功能非常簡單,但可以應用到我們常見的應用中,如下:
代碼如下:
<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>

即文本框加上圓角,獲取焦點時呈現一種樣式,失去焦點時再呈現另一種樣式。

當然,我們可以通過和jQuery本身強大的功能結合來滿足不同的需求。

優點:

體積小,兩個文件經過壓縮後只有2.23kb
簡單易用
不足:

邊框弧度和線條的粗細不能調節(如果需要請參考jquery.corner插件)
高度和字符大小配合的不是很好,有時字符會被遮住一半
測試通過IE6、FF、Opera、Safari、Chrome

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