ASP.NET MVC:利用ASP.NET MVC4的IBundleTransform集成LESS

背景

LESS確實不錯,只是每次寫完LESS都要手工編譯一下有點麻煩(VS插件一直沒有安裝好),昨天在官方看到了如何用IBundleTransform集成LESS,今天就記錄一下。css

參考資料:http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minificationweb

代碼

LessTransformmvc

 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中增長以下代碼asp.net

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);

在模板中引用less

1     @Styles.Render("~/Content/less")

順便說一下LESS的編碼

寫註釋和統一的格式化學習

  1 /*柵格的左邊距*/
  2 @span_margin: 20px;
  3 /*柵格的的寬度*/
  4 @span_width: 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就好了。ui

備註

學習HTML+CSS有一週了,今天作個網站試試。編碼

相關文章
相關標籤/搜索