今天给大家分享我个人使用的Less版的CSS(3)工具类库,欢迎补充~!
PS:本文假定你已经有了一定的less基础,如果你对less还不熟悉,可以到less中文官网学习:http://www.lesscss.net/
用法很简单,使用 @import 将该类库引入到需要使用的less文件中即可,就好像页面中引入了jQuery一样,接下来就可以使用它的方法和函数了,示例:
@charset "utf-8";
@import "lib/utils.less";
/*!* index.less*/.main { .Clearfix(); }
.notice li { .Text-ellipsis(); }
.comment { .Text-break(); }
utils.less, 欢迎补充:
@charset "utf-8";
/*!
* utils.less v3.0
* based on veryless: http://veryless.org/
*/ // 清除(闭合)浮动:http://www.iyunlu.com/view/css-xhtml/55.html
.Clearfix () { *zoom: 1;&:before, &:after {display: table;content: "";/** Fixes Opera/contenteditable bug:* http://nicolasgallagher.com/micro-clearfix-hack/#comment-36952*/line-height: 0;}&:after {clear: both;}
}// inline-block
.Inline-block () { /* 如果有必要可在其父元素上应用样式vertical-align:top; */display:inline-block; *display:inline; *zoom:1;
} // 文字隐藏
.Text-hidden () {/* * 魔鬼的text-indent-9999px:* http://prezi.com/t8yyirefxn2u/text-indent-9999px/ */text-indent: 100%;white-space: nowrap; /* 强制不换行 */overflow: hidden;
}
.Text-ellipsis () {/* * 文字溢出隐藏显示省略号,需要用在与文字最近的一个block|inline-block的祖先元素上(ie6中该元素的width需要显性的声明且不能为auto,100%是可行的) */overflow: hidden;text-overflow: ellipsis;white-space: nowrap;
}// 文字(强制)折行
.Text-break () {word-wrap:break-word;word-break:break-all;
}// 自定义字体
.Font-face(@path, @font-name){@font-face{font-family: @font-name;src: url('@{path}/@{font-name}.eot');src: url('@{path}/@{font-name}.eot?#iefix') format('embedded-opentype'),url('@{path}/@{font-name}.woff') format('woff'),url('@{path}/@{font-name}.ttf') format('truetype'),url('@{path}/@{font-name}.svg#svgFontName') format('svg');}
}// (整体)透明
.Opacity (@a: 0.3) {@aa: (@a*100); opacity: @a;filter: ~"Alpha(Opacity=@{aa})"; /* ie5-8 */*zoom: 1; /* 应用滤镜的元素必须拥有layout */
}
// 背景透明
.Background-rgba (@r: 0, @g: 0, @b: 0, @a: 0.3) {@c: rgba(@r, @g, @b, @a);@cc: argb(@c);background-color:@c;filter:~"progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr='@{cc}',EndColorStr='@{cc}')";*zoom: 1;
}
// 圆角
.Border-radius (@a: 3px) {-webkit-border-radius: @a;-moz-border-radius: @a;border-radius: @a;
}// 阴影
.Box-shadow ( @a: 0 1px 3px rgba(0,0,0,.3); ) {-webkit-box-shadow: @a;-moz-box-shadow: @a;box-shadow: @a;
}// 渐变
.Linear-gradient (@a: #ffa600; @b: #fe9200) {filter: ~'progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr="@{a}", endColorstr="@{b}")';background-image: -webkit-linear-gradient(top,@a, @b);background-image: -moz-linear-gradient(top,@a, @b);background-image: -ms-linear-gradient(top,@a 0,@b 100%);background-image: -o-linear-gradient(top,@a, @b);background-image: linear-gradient(top,@a, @b);
}
// 禁用渐变
.Linear-gradientDisable () {background-image: none;filter: progid:DXImageTransform.Microsoft.gradient(enabled=false); /* 禁用渐变 */
}.Box-sizing ( @a: content-box ) {-webkit-box-sizing: @a;-moz-box-sizing: @a;box-sizing: @a;
}// 利用边框制作小三角
.Triangle (@type: bottom, @x: 10px, @c: #999, @y: @x) {display: inline-block;width:0; height:0; overflow:hidden;
vertical-align: middle;// 这里有意利用IE6对dashed渲染缺陷解决IE6不支持transparent的问题 @transparent: @y dashed transparent; // 该特殊边框的大小必须小于其他边框大小的四倍,否则IE6会有问题 @special: @x solid @c;.border();.border () when (@type = bottom) {border-bottom: @special;border-left: @transparent;border-right: @transparent;}.border () when (@type = top) {border-top: @special;border-left: @transparent;border-right: @transparent;}.border () when (@type = left) {border-left: @special;border-top: @transparent;border-bottom: @transparent;}.border () when (@type = right) {border-right: @special;border-top: @transparent;border-bottom: @transparent;}}// 鼠标经过旋转
.Rotate (@d: 180deg, @s: .3s) {-webkit-transition: @s ease-in;-moz-transition: @s ease-in;-o-transition: @s ease-in;transition: @s ease-in;&:hover, &-hover{-webkit-transform: rotate(@d);-moz-transform: rotate(@d);-ms-transform: rotate(0); -o-transform: rotate(@d);filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);-ms-filter: ~"progid:DXImageTransform.Microsoft.BasicImage(rotation=1)";transform: rotate(@d);}
}// 去色(不兼容低版本Safari、Opera和某些firefox), 一般用于哀悼日)
.Grayscale() { -webkit-filter: grayscale(100%); -webkit-filter: grayscale(1); /* Chrome 19+ & Safari 6+ */-moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); filter: grayscale(100%);/* firefox 3.5+,慎用!存在性能问题 */ filter: url('data:image/svg+xml;utf8,#grayscale'); filter: gray; /* IE6~9 */
}
.Max-height(@h, @pd: 0) {@Hnum: unit(@h);@PDnum: unit(@pd);max-height: @h;_height:~"expression( (this.clientHeight-@{PDnum})>@{Hnum}? @{Hnum} : (this.clientHeight-@{PDnum}) )";_overflow: hidden;_zoom: 1;/*
* clientHeight = height + padding;* offsetHeight = height + padding+border;*/
}
.Max-width(@h, @pd: 0) {@Hnum: unit(@h);@PDnum: unit(@pd);max-width: @h;_width:~"expression( (this.clientWidth-@{PDnum})>@{Hnum}? @{Hnum} : (this.clientWidth-@{PDnum}) )";_overflow: hidden;_zoom: 1;}
.Min-height(&#64;h, &#64;pd: 0) {&#64;Hnum: unit(&#64;h);&#64;PDnum: unit(&#64;pd);min-height: &#64;h;_height:~"expression( (this.clientHeight-&#64;{PDnum})<&#64;{Hnum}? &#64;{Hnum} : (this.clientHeight-&#64;{PDnum}) )";_overflow: hidden;_zoom: 1;}// 一个老外的博客中看到的&#xff1a;
.Min-height2(&#64;h) {min-height: &#64;h;height: auto !important; /*all browsers except ie6 will respect the !important flag*/height: &#64;h; /*Should have the same value as the min height above*/
}.Min-width(&#64;h, &#64;pd: 0) {&#64;Hnum: unit(&#64;h);&#64;PDnum: unit(&#64;pd);min-width: &#64;h;_width:~"expression( (this.clientWidth-&#64;{PDnum})<&#64;{Hnum}? &#64;{Hnum} : (this.clientWidth-&#64;{PDnum}) )";_overflow: hidden;_zoom: 1;}.Clip (&#64;t: 0px, &#64;r: 0px, &#64;b: 0px, &#64;l: 0px) {position:absolute; clip:rect; /* 矩形剪裁只能作用于 position:absolute|fixed 的元素上 */clip: rect(&#64;t &#64;r &#64;b &#64;l); /* IE6, IE7 */clip: rect(&#64;t, &#64;r, &#64;b, &#64;l);
}// 解决Retina屏幕下的图像显示
.Retina-image(&#64;file-1x, &#64;file-2x, &#64;width-1x, &#64;height-1x) {background-image: url("&#64;{file-1x}");&#64;mediaonly screen and (-webkit-min-device-pixel-ratio: 2),only screen and ( min--moz-device-pixel-ratio: 2),only screen and ( -o-min-device-pixel-ratio: 2/1),only screen and ( min-device-pixel-ratio: 2),only screen and ( min-resolution: 192dpi),only screen and ( min-resolution: 2dppx) {background-image: url("&#64;{file-2x}");background-size: &#64;width-1x &#64;height-1x;}
}
.Image-set (&#64;a1, &#64;a2) { /* * 解决Retina屏幕下的图像显示&#xff0c;淘宝首页的css sprite解决方案* image-set是CSS4的属性&#xff0c;"safari6"和“chrome21”支持* 如果浏览器支持image-set&#xff0c;普通显屏&#xff0c;此时浏览器会选择image-set中的&#64;1x背景图像; Retina屏幕&#xff0c;此时浏览器会选择image-set中的&#64;2x背景图像;* 扩展阅读&#xff1a;http://www.w3cplus.com/css/safari-6-and-chrome-21-add-image-set-to-support-retina-images.html*/background: url(&#64;a1);background-image: -webkit-image-set(url(&#64;a1) 1x, url(&#64;a2) 2x);background-image: -moz-image-set(url(&#64;a1) 1x,url(&#64;a2) 2x);background-image: -ms-image-set(url(&#64;a1) 1x,url(&#64;a2) 2x);background-image: -o-image-set(url(&#64;a1) 1x,url(&#64;a2) 2x);background-repeat: no-repeat;
}.User-select(&#64;select: auto) {-webkit-user-select: &#64;select;-moz-user-select: &#64;select;-ms-user-select: &#64;select;-o-user-select: &#64;select;user-select: &#64;select;
}
// “完全居中”: http://codepen.io/shshaw/full/gEiDt
.Absolute-Center() {
/*
* 1.必须声明元素[max-]height和[max-]width
* 2.Chrome, Firefox, Safari, Mobile Safari, IE8&#43;; 不支持Windows Phone无效
*/
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
overflow: auto;
resize: both; // 不支持移动版浏览器和IE10-,[可选]
}
// 图片水平垂直居中: http://blog.moocss.com/code-snippets/html-css-code-snippets/1390.html
.Image-center(&#64;w, &#64;h: &#64;w){&#64;width: unit(&#64;w);&#64;height: unit(&#64;h);&#64;_fontsize: (&#64;height * 0.873);width: ~&#39;&#64;{width}px&#39;;height: ~&#39;&#64;{height}px&#39;;overflow: hidden;display: table-cell;vertical-align: middle;text-align: center;*display: block;//IE hack*font: ~"&#64;{_fontsize}px/1em Arial";img{display: inline;max-height: 100%;max-width: 100%;vertical-align: middle;}
}// CSS3 "栅格化"(flexbox)&#xff1a;http://www.zhihu.com/question/22991944
.Display-box() {display: -webkit-box; /* Chrome 4&#43;, Safari 3.1, iOS Safari 3.2&#43; */display: -moz-box; /* Firefox 17- */display: -webkit-flex; /* Chrome 21&#43;, Safari 6.1&#43;, iOS Safari 7&#43;, Opera 15/16 */display: -moz-flex; /* Firefox 18&#43; */display: -ms-flexbox; /* IE 10 */display: flex; /* Chrome 29&#43;, Firefox 22&#43;, IE 11&#43;, Opera 12.1/17/18, Android 4.4&#43; */
}
.Box-flex(&#64;a) {-webkit-box-flex: &#64;a;-moz-box-flex: &#64;a;box-flex: &#64;a;
}