微信小程序中用WebStorm使用LESS
前提
自己前端不熟悉,很多都需要練習(xí)
網(wǎng)上找了一個(gè)css的demo, 放到微信小程序后,可以運(yùn)行
圖片很大,沒(méi)有弄,加載可能有點(diǎn)慢(不相關(guān)的,就不扯了)
Less環(huán)境
Less需要nodejs的npm
nodejs的環(huán)境這里略了
自己百度
通過(guò)
npm install less -g
安裝好 less
(沒(méi)有用過(guò)的,可以理解為 maven的庫(kù), gradle庫(kù),pods的庫(kù))
WebStorm的Less使用
先關(guān)聯(lián)對(duì)應(yīng)的less
當(dāng)然,對(duì)應(yīng)的wxss文件,在webstorm中的顯示,
可以參考自己其他文章
這里,只要?jiǎng)?chuàng)建less文件,就會(huì)自動(dòng)生成對(duì)應(yīng)的wxss文件了(當(dāng)然,寫(xiě)好保存less文件,會(huì)自動(dòng)刷新wxss文件,很方便吧)
直接wxss和 less的比較
我們先看下頁(yè)面
頁(yè)面很簡(jiǎn)單
就只有一個(gè) sky 套用 3個(gè)cloud 類(lèi)
view class="container"> <view class="sky"> <view class="clouds_one"> </view > <view class="clouds_two"> </view > <view class="clouds_three"> </view > <view class="clouds_three"></view> </view> </view>
再看看css
.sky {
height: 480px;
background: #007fd5;
position: relative;
overflow: hidden;
animation: sky_background 50s ease-out infinite;
}
.sky .clouds_one {
background: url("../../resources/cloud/cloud_one.png");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud 50s linear infinite;
transform: translate3d(0, 0, 0);
}
.sky .clouds_two {
background: url("../../resources/cloud/cloud_two.png");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud 75s linear infinite;
transform: translate3d(0, 0, 0);
}
.sky .clouds_three {
background: url("../../resources/cloud/cloud_three.png");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud 120s linear infinite;
transform: translate3d(0, 0, 0);
}
@keyframes cloud {
0% {
left: 0;
}
100% {
left: -200%;
}
}
我們發(fā)現(xiàn)有很多重復(fù)的地方
功能不難,但是占了70行,并且很難復(fù)用
修改的畫(huà),還要看里面的邏輯
修改也不方便
Less的使用
我們簡(jiǎn)單定義變量 和 方法以后
用less 大體是這樣的
@dodo-out-height : 480px; //@dodo-out-height : 480rpx;
@dodo-bg-sky : #007fd5;
@dodo-img-url-clouds_one : "../../resources/cloud/cloud_one.png";
@dodo-img-url-clouds_two : "../../resources/cloud/cloud_two.png";
@dodo-img-url-clouds_three : "../../resources/cloud/cloud_three.png";
.sky {
height: @dodo-out-height;
background: @dodo-bg-sky;
position: relative;
overflow: hidden;
animation: sky_background 50s ease-out infinite;
}
.sky .clouds_one {
.dodo_clouds(@url:@dodo-img-url-clouds_one, @time: 50s)
}
.sky .clouds_two {
.dodo_clouds(@url:@dodo-img-url-clouds_two, @time: 75s)
}
.sky .clouds_three {
.dodo_clouds(@url:@dodo-img-url-clouds_three, @time: 120s)
}
.dodo_clouds (@url: @dodo-img-url-clouds_one, @height: 100%, @width: 300%, @time: 100s){
background: url(@url);
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud @time linear infinite;
transform: translate3d(0, 0, 0);
}
@keyframes cloud {
0% {
left: 0
}
100% {
left: -200%
}
}
保存后,
我們發(fā)現(xiàn)對(duì)應(yīng)的wxss文件,也改變了,直接生成了可以讀取的文件
和之前直接寫(xiě)的文件沒(méi)有太大區(qū)別
也不會(huì)出現(xiàn)對(duì)應(yīng)的變量和方法
.sky {
height: 480px;
background: #007fd5;
position: relative;
overflow: hidden;
animation: sky_background 50s ease-out infinite;
}
.sky .clouds_one {
background: url("../../resources/cloud/cloud_one.png");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud 50s linear infinite;
transform: translate3d(0, 0, 0);
}
.sky .clouds_two {
background: url("../../resources/cloud/cloud_two.png");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud 75s linear infinite;
transform: translate3d(0, 0, 0);
}
.sky .clouds_three {
background: url("../../resources/cloud/cloud_three.png");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
animation: cloud 120s linear infinite;
transform: translate3d(0, 0, 0);
}
@keyframes cloud {
0% {
left: 0;
}
100% {
left: -200%;
}
}
預(yù)覽下:
也沒(méi)有區(qū)別,只是代碼寫(xiě)起來(lái)更方便(建議機(jī)子配置可以的畫(huà),開(kāi)發(fā)別用微信提供的ide,效率太低)
less很強(qiáng)大,其他的地方,有時(shí)間再深入,
感覺(jué)less好用在于它的復(fù)用性 :)
簡(jiǎn)單demo源碼:http://xiazai.jb51.net/201703/yuanma/weapp-start-master(jb51.net).rar
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
gulp-font-spider實(shí)現(xiàn)中文字體包壓縮實(shí)踐
這篇文章主要為大家介紹了gulp-font-spider實(shí)現(xiàn)中文字體包壓縮實(shí)踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
lodash內(nèi)部方法getData和setData實(shí)例解析
本篇章我們將了解lodash里內(nèi)部關(guān)于Data的操作方法,重點(diǎn)關(guān)注getData、setData兩個(gè)內(nèi)部方法,同時(shí)由實(shí)現(xiàn)上引申其他內(nèi)部封裝的方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
小程序開(kāi)發(fā)踩坑:頁(yè)面窗口定位(相對(duì)于瀏覽器定位)(推薦)
這篇文章主要介紹了小程序開(kāi)發(fā)頁(yè)面窗口定位,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
用javascript制作qq注冊(cè)動(dòng)態(tài)頁(yè)面
這篇文章主要介紹了用javascript制作qq注冊(cè)動(dòng)態(tài)頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-04-04
面試手寫(xiě)實(shí)現(xiàn)Promise.all
這篇文章主要為大家介紹了面試手寫(xiě)實(shí)現(xiàn)Promise.all過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06




