- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) >
- 微信小程序中WebStorm使用LESS的示例分析
小編給大家分享一下微信小程序中WebStorm使用LESS的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
網(wǎng)上找了一個(gè)css的demo, 放到微信小程序后,可以運行
圖片很大,沒(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的庫, gradle庫,pods的庫)
WebStorm的Less使用
先關(guān)聯(lián)對應的less
當然,對應的wxss文件,在webstorm中的顯示,
可以參考自己其他文章
WebStorm:遇到的問(wèn)題
這里,只要創(chuàng )建less文件,就會(huì )自動(dòng)生成對應的wxss文件了(當然,寫(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ā)現有很多重復的地方
功能不難,但是占了70行,并且很難復用
修改的畫(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ā)現對應的wxss文件,也改變了,直接生成了可以讀取的文件
和之前直接寫(xiě)的文件沒(méi)有太大區別
也不會(huì )出現對應的變量和方法
.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%; } }
預覽下:
也沒(méi)有區別,只是代碼寫(xiě)起來(lái)更方便(建議機子配置可以的畫(huà),開(kāi)發(fā)別用微信提供的ide,效率太低)
less很強大,其他的地方,有時(shí)間再深入,
感覺(jué)less好用在于它的復用性 :)
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2 建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站