styles.js 846 B

1234567891011121314151617181920212223242526272829303132333435
  1. // 需要单位转换的属性
  2. const size = ['width', 'height', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', "borderRadius",
  3. "fontSize", "margin",
  4. "padding", "gap"
  5. ];
  6. // 样式的key
  7. const keys = [...size, "fontWeight", "color", "backgroundColor"]
  8. /**
  9. * isCss: 是否需要判断当前属性是否css
  10. */
  11. export const getStyle = (config) => {
  12. if (!config) return '';
  13. // console.log('config = ', config)
  14. const styleObj = {}
  15. try {
  16. for (let key in config) {
  17. if (keys.includes(key)) {
  18. if (size.includes(key)) {
  19. styleObj[key] = uni.upx2px(config[key]) + 'px'
  20. } else {
  21. styleObj[key] = config[key];
  22. }
  23. }
  24. }
  25. } catch (e) {}
  26. return styleObj;
  27. }
  28. export const getSingleStyle = (num) => {
  29. return uni.upx2px(num) + 'px'
  30. }
  31. // 样式单位rpx
  32. export const unit = (target) => {
  33. return `${target * 2}rpx`
  34. }