背景
LESS确实不错,只是每次写完LESS都要手工编译一下有点麻烦(VS插件一直没有安装好),昨天在官方看到了如何用IBundleTransform集成LESS,今天就记录一下。
参考资料:http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification。
代码
LessTransform
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.Web.Optimization; 7 8 namespace LessStudy.Infrastructure 9 { 10 public class LessTransform : IBundleTransform 11 { 12 public void Process(BundleContext context, BundleResponse response) 13 { 14 response.Content = dotless.Core.Less.Parse(response.Content); 15 response.ContentType = "text/css"; 16 } 17 } 18 }
BundleConfig中增加如下代码
1 var lessBundle = new Bundle("~/Content/less").IncludeDirectory("~/Content/less", "*.less"); 2 lessBundle.Transforms.Add(new LessTransform()); 3 lessBundle.Transforms.Add(new CssMinify()); 4 bundles.Add(lessBundle);
在模板中引用
1 @Styles.Render("~/Content/less")
顺便说一下LESS的编码
写注释和统一的格式化
1 /*栅格的左边距*/ 2 @span_margin: 20px; 3 /*栅格的的宽度*/ 4 @span_ 60px; 5 /*栅格数量*/ 6 @span_length: 12; 7 /*像素单位*/ 8 @px_unit: 1px; 9 10 /*固定栅格系统*/ 11 .row 12 { 13 margin-left: -@span_margin;/*抵消第一个栅格的左边距*/ 14 *zoom: 1; 15 } 16 17 [class*="span"] 18 { 19 float: left; 20 min-height:1px; 21 margin-left: @span_margin; 22 } 23 24 .span (@index) when (@index > 0) 25 { 26 .span@{index} 27 { 28 width: (@index * @span_width + (@index - 1) * @span_margin) * @px_unit; 29 } 30 31 .span(@index - 1); 32 } 33 34 .span (0) {} 35 36 .span (@span_length); 37 38 .offset (@index) when (@index > 0) 39 { 40 .offset@{index} 41 { 42 margin-left: (@index * @span_width + (@index + 1) * @span_margin) * @px_unit; 43 } 44 45 .offset(@index - 1); 46 } 47 48 .offset (0) {} 49 50 .offset (@span_length); 51 52 53 54 55 /*自动栅格系统*/ 56 57 /*栅格的宽度和左边距之比*/ 58 @span_width_margin_scale: 3; 59 /*栅格的左边距比例*/ 60 @span_margin_percentage: (1 / (@span_length * (@span_width_margin_scale + 1) - 1)); 61 62 .row-fluid 63 { 64 width: 100%; 65 *zoom: 1; 66 } 67 68 .row-fluid [class*="span"]:first-child 69 { 70 margin-left: 0; 71 } 72 73 .row-fluid [class*="span"] 74 { 75 display: block; 76 float: left; 77 min-height: 30px; 78 margin-left: percentage(@span_margin_percentage); 79 -webkit-box-sizing: border-box; 80 -moz-box-sizing: border-box; 81 box-sizing: border-box; 82 } 83 84 .fluid_span (@index) when (@index > 0) 85 { 86 .row-fluid .span@{index} 87 { 88 width: percentage((@index * (@span_width_margin_scale + 1) - 1) * @span_margin_percentage); 89 } 90 91 .fluid_span(@index - 1); 92 } 93 94 .fluid_span (0) {} 95 96 .fluid_span (@span_length); 97 98 .fluid_offset (@index) when (@index > 0) 99 { 100 .row-fluid .offset@{index} 101 { 102 margin-left: percentage((@index * (@span_width_margin_scale + 1) + 1) * @span_margin_percentage); 103 } 104 105 .fluid_offset(@index - 1); 106 } 107 108 .fluid_offset (0) {} 109 110 .fluid_offset (@span_length);
如何利用CSS的智能提示呢?
添加文件的时候选择css,然后修改后缀名为less就行了。
备注
学习HTML+CSS有一周了,今天做个网站试试。