style.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { addStyle, deepMerge, addUnit, trim } from '../function/index';
  2. export default {
  3. props: {
  4. // flex排列方式
  5. flexDirection: {
  6. type: String,
  7. default: ''
  8. },
  9. // flex-direction的简写
  10. fd: {
  11. type: String,
  12. default: ''
  13. },
  14. // 展示类型
  15. display: {
  16. type: String,
  17. default: ''
  18. },
  19. // display简写
  20. d: {
  21. type: String,
  22. default: ''
  23. },
  24. // 主轴排列方式
  25. justifyContent: {
  26. type: String,
  27. default: ''
  28. },
  29. // justifyContent的简写
  30. jc: {
  31. type: String,
  32. default: ''
  33. },
  34. // 纵轴排列方式
  35. alignItems: {
  36. type: String,
  37. default: ''
  38. },
  39. // align-items的简写
  40. ai: {
  41. type: String,
  42. default: ''
  43. },
  44. color: {
  45. type: String,
  46. default: ''
  47. },
  48. // color简写
  49. c: {
  50. type: String,
  51. default: ''
  52. },
  53. // 字体大小
  54. fontSize: {
  55. type: [String, Number],
  56. default: 0
  57. },
  58. // font-size简写
  59. fs: {
  60. type: [String, Number],
  61. default: ''
  62. },
  63. margin: {
  64. type: [String, Number],
  65. default: 0
  66. },
  67. // margin简写
  68. m: {
  69. type: [String, Number],
  70. default: 0
  71. },
  72. // margin-top
  73. marginTop: {
  74. type: [String, Number],
  75. default: 0
  76. },
  77. // margin-top简写
  78. mt: {
  79. type: [String, Number],
  80. default: 0
  81. },
  82. // margin-right
  83. marginRight: {
  84. type: [String, Number],
  85. default: 0
  86. },
  87. // margin-right简写
  88. mr: {
  89. type: [String, Number],
  90. default: 0
  91. },
  92. // margin-bottom
  93. marginBottom: {
  94. type: [String, Number],
  95. default: 0
  96. },
  97. // margin-bottom简写
  98. mb: {
  99. type: [String, Number],
  100. default: 0
  101. },
  102. // margin-left
  103. marginLeft: {
  104. type: [String, Number],
  105. default: 0
  106. },
  107. // margin-left简写
  108. ml: {
  109. type: [String, Number],
  110. default: 0
  111. },
  112. // padding-left
  113. paddingLeft: {
  114. type: [String, Number],
  115. default: 0
  116. },
  117. // padding-left简写
  118. pl: {
  119. type: [String, Number],
  120. default: 0
  121. },
  122. // padding-top
  123. paddingTop: {
  124. type: [String, Number],
  125. default: 0
  126. },
  127. // padding-top简写
  128. pt: {
  129. type: [String, Number],
  130. default: 0
  131. },
  132. // padding-right
  133. paddingRight: {
  134. type: [String, Number],
  135. default: 0
  136. },
  137. // padding-right简写
  138. pr: {
  139. type: [String, Number],
  140. default: 0
  141. },
  142. // padding-bottom
  143. paddingBottom: {
  144. type: [String, Number],
  145. default: 0
  146. },
  147. // padding-bottom简写
  148. pb: {
  149. type: [String, Number],
  150. default: 0
  151. },
  152. // border-radius
  153. borderRadius: {
  154. type: [String, Number],
  155. default: 0
  156. },
  157. // border-radius简写
  158. radius: {
  159. type: [String, Number],
  160. default: 0
  161. },
  162. // transform
  163. transform: {
  164. type: String,
  165. default: ''
  166. },
  167. // 定位
  168. position: {
  169. type: String,
  170. default: ''
  171. },
  172. // position简写
  173. pos: {
  174. type: String,
  175. default: ''
  176. },
  177. // 宽度
  178. width: {
  179. type: [String, Number],
  180. default: null
  181. },
  182. // width简写
  183. w: {
  184. type: [String, Number],
  185. default: null
  186. },
  187. // 高度
  188. height: {
  189. type: [String, Number],
  190. default: null
  191. },
  192. // height简写
  193. h: {
  194. type: [String, Number],
  195. default: null
  196. },
  197. top: {
  198. type: [String, Number],
  199. default: 0
  200. },
  201. right: {
  202. type: [String, Number],
  203. default: 0
  204. },
  205. bottom: {
  206. type: [String, Number],
  207. default: 0
  208. },
  209. left: {
  210. type: [String, Number],
  211. default: 0
  212. }
  213. },
  214. computed: {
  215. viewStyle() {
  216. const style = {}
  217. const addStyleTmp = addStyle(this.width || this.w)
  218. && (style.width = addStyle(this.width || this.w))(this.height || this.h)
  219. && (style.height = addStyle(this.height || this.h))(this.margin || this.m)
  220. && (style.margin = addStyle(this.margin || this.m))(this.marginTop || this.mt)
  221. && (style.marginTop = addStyle(this.marginTop || this.mt))(this.marginRight || this.mr)
  222. && (style.marginRight = addStyle(this.marginRight || this.mr))(this.marginBottom || this.mb)
  223. && (style.marginBottom = addStyle(this.marginBottom || this.mb))(this.marginLeft || this.ml)
  224. && (style.marginLeft = addStyle(this.marginLeft || this.ml))(this.padding || this.p)
  225. && (style.padding = addStyle(this.padding || this.p))(this.paddingTop || this.pt)
  226. && (style.paddingTop = addStyle(this.paddingTop || this.pt))(this.paddingRight || this.pr)
  227. && (style.paddingRight = addStyle(this.paddingRight || this.pr))(this.paddingBottom || this.pb)
  228. && (style.paddingBottom = addStyle(this.paddingBottom || this.pb))(this.paddingLeft || this.pl)
  229. && (style.paddingLeft = addStyle(this.paddingLeft || this.pl))(this.color || this.c)
  230. && (style.color = this.color || this.c)(this.fontSize || this.fs)
  231. && (style.fontSize = this.fontSize || this.fs)(this.borderRadius || this.radius)
  232. && (style.borderRadius = this.borderRadius || this.radius)(this.position || this.pos)
  233. && (this.position = this.position || this.pos)(this.flexDirection || this.fd)
  234. && (this.flexDirection = this.flexDirection || this.fd)(this.justifyContent || jc)
  235. && (this.justifyContent = this.justifyContent || jc)(this.alignItems || ai)
  236. && (this.alignItems = this.alignItems || ai)
  237. return deepMerge(style, addStyleTmp(this.customStyle))
  238. }
  239. },
  240. methods: {
  241. // 获取margin或者padding的单位,比如padding: 0 20转为padding: 0 20px
  242. getUnit(unit = '') {
  243. // 取出两端空格,分隔成数组,再对数组的每个元素添加单位,最后再合并成字符串
  244. return trim(unit).split(' ').map((item) => addUnit(item)).join(' ')
  245. }
  246. }
  247. }