前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用WebStorm在微信小程序中使用LESS

用WebStorm在微信小程序中使用LESS

作者头像
dodo_lihao
发布2018-09-12 10:28:18
2K0
发布2018-09-12 10:28:18
举报
文章被收录于专栏:懒人开发懒人开发

前提

自己前端不熟悉,很多都需要练习

网上找了一个css的demo, 放到微信小程序后,可以运行

图片很大,没有弄,加载可能有点慢

(不相关的,就不扯了)


Less环境

Less需要nodejs的npm

nodejs的环境这里略了

自己百度

通过

代码语言:javascript
复制
npm install less -g
  • 安装好 less (没有用过的,可以理解为 maven的库, gradle库,pods的库)

WebStorm的Less使用

先关联对应的less

当然,对应的wxss文件,在webstorm中的显示,

可以参考自己其他文章

WebStorm:遇到的问题

这里,只要创建less文件,

就会自动生成对应的wxss文件了

(当然,写好保存less文件,会自动刷新wxss文件,很方便吧)


直接wxss和 less的比较

我们先看下页面

页面很简单

就只有一个 sky 套用 3个cloud 类

代码语言:javascript
复制
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

代码语言:javascript
复制
.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%;
  }
}

我们发现有很多重复的地方

功能不难,但是占了70行,并且很难复用

修改的画,还要看里面的逻辑

修改也不方便


Less的使用

我们简单定义变量 和 方法以后

用less 大体是这样的

代码语言:javascript
复制
@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%
  }
}

保存后,

我们发现对应的wxss文件,也改变了,直接生成了可以读取的文件

和之前直接写的文件没有太大区别

也不会出现对应的变量和方法

代码语言:javascript
复制
.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%;
  }
}

预览下:

也没有区别,只是代码写起来更方便

(建议机子配置可以的画,开发别用微信提供的ide,效率太低)

less很强大,其他的地方,有时间再深入, 感觉less好用在于它的复用性 :)


简单demo源码

刚开始写微信小程序

因为所有的do都来自于网络

自己放在github上,如果愿意参考,可以看

(时间原因,官方demo上面简单添加)

dodo的微信小程序demo

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提
  • Less环境
  • WebStorm的Less使用
  • 直接wxss和 less的比较
  • Less的使用
  • 简单demo源码
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档