Jquery边界插件
我有这个jquery代码,但我不能设置默认行为。在哪里(我是指代码中的位置)可以编写默认指令?在返回后。每个或之前或何处?谢谢你的回答Jquery边界插件,jquery,Jquery,我有这个jquery代码,但我不能设置默认行为。在哪里(我是指代码中的位置)可以编写默认指令?在返回后。每个或之前或何处?谢谢你的回答 // corners v2 $.fn.corners2 = function (options) { // defaults settings for corners2 var defaults = { // corners default style corners: 'default', //
// corners v2
$.fn.corners2 = function (options) {
// defaults settings for corners2
var defaults = {
// corners default style
corners: 'default',
// corners default radius
border_radius: '1',
// borders default style
border_style: 'solid',
// borders default color without # symbol
border_color: '#f7145a'
};
// options
var options = $.extend({}, defaults, options);
return this.each(function () {
var element = $(this);
// corners
switch (options.corners) {
// all
case 'all':
element.css({
'-webkit-border-radius': '' + options.radius + 'px',
'-moz-border-radius': '' + options.radius + 'px',
'border-radius': '' + options.radius + 'px'
});
break;
// top left
case 'top-left':
element.css({
'-webkit-border-top-left-radius': '' + options.radius + 'px',
'-moz-border-radius-topleft': '' + options.radius + 'px',
'border-top-left-radius': '' + options.radius + 'px'
});
break;
// top right
case 'top-right':
element.css({
'-webkit-border-top-right-radius': '' + options.radius + 'px',
'-moz-border-radius-topright': '' + options.radius + 'px',
'border-top-right-radius': '' + options.radius + 'px'
});
break;
}
});
};
}(jQuery));
我不知道你在问什么。另外,您可以修复该代码的格式吗?它的间距太大了。您在默认值中调用参数“border_radius”,但在代码中引用它作为半径,您应该使用其中一个。这里我做了两个更改,首先,我将默认半径参数的名称从
border\u radius
更改为radius
,因为这是您的代码实际使用的,其次,我将您传入的变量的名称从options
更改为opt
,以避免与您在内部使用的变量名称冲突,即options
,如何设置默认属性?例如:$(this.css({'border':'5px solid black'});我可以将此代码放置在何处以便像默认代码一样使用。//当我使用$('div').corners2()时;因此jquery设置$(this.css({'border':'5px solid black'});将radius:'1'
更改为radius:'5'
,将边框颜色:'#f7145a'
更改为边框颜色:'black'
我的意思是如何制作程序来设置这些值如果我没有在$(此)中输入任何内容。拐角2();当设置错误时,切换使用默认值:something,但当我设置任何内容时会发生什么?更新答案以应用所有角的边框宽度、颜色和样式
// corners v2
$.fn.corners2 = function (opts) {
// defaults settings for corners2
var defaults = {
// corners default style
corners: 'default',
// corners default radius
border_radius: '1',
// borders default style
border_style: 'solid',
// borders default color
border_color: '#f7145a'
// border size
border_width: 1
};
// options
var options = $.extend({}, defaults, opts);
return this.each(function () {
var element = $(this);
$(this).css({
borderWidth:options.border_width,
borderStyle:options.border_style,
borderColor:options.border_color});
// corners
switch (options.corners) {
// all
case 'all':
element.css({
'-webkit-border-radius': '' + options.border_radius + 'px',
'-moz-border-radius': '' + options.border_radius + 'px',
'border-radius': '' + options.border_radius + 'px'
});
break;
// top left
case 'top-left':
element.css({
'-webkit-border-top-left-radius': '' + options.border_radius + 'px',
'-moz-border-radius-topleft': '' + options.border_radius + 'px',
'border-top-left-radius': '' + options.border_radius + 'px'
});
break;
// top right
case 'top-right':
element.css({
'-webkit-border-top-right-radius': '' + options.border_radius + 'px',
'-moz-border-radius-topright': '' + options.border_radius + 'px',
'border-top-right-radius': '' + options.border_radius + 'px'
});
break;
// Add cases for bottom-left, bottom-right, top, left, right, bottom, top-bottom, left-right, top-left-right, etc.
}
});
};