2013-08-16 3 views
1

의 사용자 기능을 사용 : 그래서 기능에만들고 내 HTML이이 lesscss

<script type="text/javascript"> 
    less = { 
     env: "development", // or "production" 
     async: false,  // load imports async 
     fileAsync: false, // load imports async when in a page under 
          // a file protocol 
     poll: 1000,   // when in watch mode, time in ms between polls 
     functions: { 
      returnCenter: function (value, context) { 
       return 'center'; 
      } 
     },  // user functions, keyed by name 
     dumpLineNumbers: "comments", // or "mediaQuery" or "all" 
     relativeUrls: false,// whether to adjust url's to be relative 
          // if false, url's are already relative to the 
          // entry less file 
     rootpath: ":/a.com/"// a path to add on to the start of every url 
          //resource 
    }; 
</script> 
<script src="less.js" type="text/javascript"></script> 

을 어떻게 만들고 내 작은 파일의 사용 기능을 할 수 있습니까?

감사와 행운을 빕니다!

경찰서. 이 returnCenter는 작동하지 않습니다. 매개 변수가 있거나 없음.

답변

0

어떤 이유로 든 함수 이름을 소문자로 써야합니다. 또한 함수가 적절한 유형을 리턴해야합니다.

시도 :

<script> 
less = { 
    env: "development", 
    functions: { 
     returncenter: function(context, value) { 
      return new(less.tree.Anonymous)('center'); 
     } 
    } 
}; 
</script> 

이제 다음과 같은 적은 코드를 사용할 수 있습니다

selector { 
property: returncenter();  
} 

앞의 적은 코드는 다음 CSS 코드로 컴파일 : 또한

selector { 
property: center;  
} 

NB 참조 : User defined functions with LessCSS?

+2

Less는 CSS의 대/소문자를 구분하지 않는 구문을 상속하므로 검색 할 함수 식별자는 먼저 소문자 문자열로 변환되므로 함수 정의는 항상 소문자 ID를 사용해야합니다 (그러나 Less 코드 자체에서는 returnCenter도 유효해야합니다). 이러한 의미에서 낙타 스타일 대신 CSS 스타일 이름 (즉, "returnCenter"대신 "return-center")을 사용하는 것이 일반적으로 덜 혼란 스럽습니다. [** 데모 **] (http://codepen.io/seven-phases-max/pen/qCjLg?editors=100). –

관련 문제