c语言less函数,使用 Less + 各种框架开发小程序快速切图布局常用的自定义 Less 函数【原创】...

本文介绍了在小程序开发中如何通过使用Less语言进行样式编写,提供了一系列预定义的函数,如宽高计算、定位调整等,以减少代码量并提高开发效率。作者分享了自己的.fn.less文件和详细使用示例,旨在帮助开发者简化工作流程。
摘要由CSDN通过智能技术生成

前言

开发小程序已有一段时间了,之前使用过 wepy、uni-app 框架来开发小程序,切图布局时对比过原始 css、scss / sass 和 less 三种方式来写样式,最后发现使用 less 来写样式是最省心的,高效的定义好 less 样式函数,可以让你写起代码来事半功倍,比起其他方式,高效地使用 less ,代码量甚至可以少一倍左右,另外还有个好处,就是可以让你写样式时尽可能少写或者不写 rpx 这个烦人的单位。

小程序快速切图布局常用 Less 函数

下面是自己定义好的 fn.less 函数文件,项目中大家可按需增删改。可以通过 @import './fn.less'; 导入自己的样式中使用,详见下面使用方式。

// 宽高顶左

.whtl (@w, @h, @t, @l) {

width: unit(@w, rpx);

height: unit(@h, rpx);

top: unit(@t, rpx);

left: unit(@l, rpx);

}

// 宽高顶右

.whtr (@w, @h, @t, @r) {

width: unit(@w, rpx);

height: unit(@h, rpx);

top: unit(@t, rpx);

right: unit(@r, rpx);

}

// 宽高底左

.whbl (@w, @h, @b, @l) {

width: unit(@w, rpx);

height: unit(@h, rpx);

bottom: unit(@b, rpx);

left: unit(@l, rpx);

}

// 宽高底右

.whbr (@w, @h, @b, @r) {

width: unit(@w, rpx);

height: unit(@h, rpx);

bottom: unit(@b, rpx);

right: unit(@r, rpx);

}

// 宽高

.wh(@w, @h) {

width: unit(@w, rpx);

height: unit(@h, rpx);

}

// 高

.h (@h) {

height: unit(@h, rpx);

}

// 宽

.w (@w) {

width: unit(@w, rpx);

}

// 顶

.t (@t) {

top: unit(@t, rpx);

}

// 左

.l (@l) {

left: unit(@l, rpx);

}

// 右

.r (@r) {

right: unit(@r, rpx);

}

// 底

.b (@b) {

bottom: unit(@b, rpx);

}

// 行高

.lh (@lh) {

line-height: unit(@lh, rpx);

}

// padding-left

.pl (@pl) {

padding-left: unit(@pl, rpx);

}

// padding-right

.pr (@pr) {

padding-right: unit(@pr, rpx);

}

// padding-top

.pt (@pt) {

padding-top: unit(@pt, rpx);

}

// padding-bottom

.pb (@pb) {

padding-bottom: unit(@pb, rpx);

}

// padding 1 参数

.p1 (@p) {

padding: unit(@p, rpx);

}

// padding 2 参数

.p2 (@tb, @lr) {

padding: unit(@tb, rpx) unit(@lr, rpx);

}

// padding 3 参数

.p3 (@t, @lr, @b) {

padding: unit(@t, rpx) unit(@lr, rpx) unit(@b, rpx);

}

// padding 4 参数

.p4 (@t, @r, @b, @l) {

padding: unit(@t, rpx) unit(@r, rpx) unit(@b, rpx) unit(@l, rpx);

}

// margin-top

.mt (@mt) {

margin-top: unit(@mt, rpx);

}

// margin-bottom

.mb (@mb) {

margin-bottom: unit(@mb, rpx);

}

// margin-left

.ml (@ml) {

margin-left: unit(@ml, rpx);

}

// margin-right

.mr (@mr) {

margin-right: unit(@mr, rpx);

}

// margin 1 参数

.m1 (@m) {

margin: unit(@m, rpx);

}

// margin 2 参数

.m2 (@tb, @lr) {

margin: unit(@tb, rpx) unit(@lr, rpx);

}

// margin 3 参数

.m3 (@t, @lr, @b) {

margin: unit(@t, rpx) unit(@lr, rpx) unit(@b, rpx);

}

// margin 4 参数

.m4 (@t, @r, @b, @l) {

margin: unit(@t, rpx) unit(@r, rpx) unit(@b, rpx) unit(@l, rpx);

}

// font-size

.fs(@fs) {

font-size: unit(@fs, rpx);

}

// border-radius

.br(@br) {

border-radius: unit(@br, rpx);

}

// text-align: center

.tc() {

text-align: center;

}

// font-weight: bold

.fwb() {

font-weight: 700;

}

// letter-spacing

.ls(@val) {

letter-spacing: unit(@val, rpx);

}

// 各种 position

.posr() {

position: relative;

}

.posa() {

position: absolute;

}

.posf() {

position: fixed;

}

// 单行文字超出显示 ...

.els () {

overflow: hidden;

text-overflow: ellipsis;

white-space: nowrap;

}

// background-size

.bs(@w, @h) {

background-size: unit(@w, rpx) unit(@h, rpx);

}

// text-shadow 文字描边 @f 边大小,@c 颜色

.textshadow(@f, @c) {

text-shadow: unit(@f, rpx) unit(@f, rpx) unit(@f, rpx) @c, unit(@f * -1, rpx) unit(@f * -1, rpx) unit(@f, rpx) @c, unit(@f, rpx) 0 unit(@f, rpx) @c, 0 unit(@f, rpx) unit(@f, rpx) @c, 0 unit(@f * -1, rpx) unit(@f, rpx) @c, unit(@f * -1, rpx) 0 unit(@f, rpx) @c;

}

使用方式

@import './fn.less';

.parent {

.wh(400, 200); // width, height

margin: 0 auto;

.posr(); // position: relative

&__block {

.posa(); // position: absolute

.whtl(400, 200, 0, 0); // width, height, top, left

}

&__text {

display:block;

.posa(); // position: absolute

.whbr(120, 60, 20, 40); // width, height, bottom, right

color: #fff;

.tc(); // text-align: center

.fs(28); // font-size

.lh(60); // line-height

.els(); // 文字超出显示 ...

}

}

阅读:

1,365

6999225be7f2128c6d26e866523259ac?s=42&d=wavatar&r=g

作者: 博主

Talk is cheap, show me the code!查看博主的所有文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值