Less 大屏自适应
由于 less 不能像 scss 一样自定义函数,只能被迫使用 minxin 混入封装大屏自适应相关样式代码
封装的代码
新建一个 less 文件,用来封装自适应代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| @designWidth: 1920px;
@designHeight: 1080px;
@normalFontSize: 16px;
.vw(@px) { width: @px / @designWidth * 100vw; }
.vh(@px) { height: @px / @designHeight * 100vh; }
.marginVw(@left, @right) { margin-left: @left / @designWidth * 100vw; margin-right: @right / @designWidth * 100vw; }
.marginVh(@top, @bottom) { margin-top: @top / @designHeight * 100vh; margin-bottom: @bottom / @designHeight * 100vh; }
.marginVwAll(@value) { .marginVw(@value, @value); }
.marginVhAll(@value) { .marginVh(@value, @value); }
.marginAll(@value) { .marginVwAll(@value, @value); .marginVhAll(@value, @value); }
.paddingVw(@left, @right) { padding-left: @left / @designWidth * 100vw; padding-right: @right / @designWidth * 100vw; }
.paddingVh(@top, @bottom) { padding-top: @top / @designHeight * 100vh; padding-bottom: @bottom / @designHeight * 100vh; }
.paddingVwAll(@value) { .paddingVw(@value, @value); }
.paddingVhAll(@value) { .paddingVh(@value, @value); }
.paddingAll(@value) { .paddingVwAll(@value); .paddingVhAll(@value); }
.left(@px) { left: @px / @designWidth * 100vw; }
.right(@px) { right: @px / @designWidth * 100vw; }
.top(@px) { top: @px / @designHeight * 100vh; }
.bottom(@px) { bottom: @px / @designHeight * 100vh; }
.rem(@px) { font-size: @px / @normalFontSize * 1rem; }
.lineHeightVh(@px) { line-height: @px / @designHeight * 100vh; }
.lineHeightRem(@px) { line-height: @px / @normalFontSize * 1rem; }
|
使用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @import "adaptive.less";
.container { .vw(1920px); .vh(1080px);
.content { .paddingVw(12px, 24px); .paddingVh(12px, 24px); .rem(18px) cursor: pointer; } }
|