发布于 2017-04-26 04:35:08 | 121 次阅读 | 评论: 0 | 来源: 网友投递
jQuery javascript框架
jQuery是一个兼容多浏览器的javascript框架,核心理念是write less,do more(写得更少,做得更多)。jQuery在2006年1月由美国人John Resig在纽约的barcamp发布,吸引了来自世界各地的众多JavaScript高手加入,由Dave Methvin率领团队进行开发。
$('.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);
.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;}
<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>