1.1.0 • Published 6 months ago

modern-flexible v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

modern-flexible

现代多设备伸缩方案

在线示例

Demo

配置项

参数类型默认值描述
rootValuenumber16根字体大小,最好跟postcss-pxtorem中的rootValue一致
resizeOptionobject{ type: 'debounce', delay: 60 }窗口大小改变时resize防抖或节流配置
distinctDevicearray[{ deviceWidthRange: [0, Infinity], isDevice: true, UIWidth: 375 }]不同设备的适配方式。 deviceWidthRange定义设备宽度范围(超出范围后不会再触发resize)isDevice用于判断当前窗口是否属于此设备UIWidth 定义对应的设计图宽度
landscapebooleanfalse是否横屏

基础使用

playground

main.js入口中引入

这样做有个坏处,flexible会在页面加载完毕后才执行,这样会导致页面闪烁,所以建议在html中直接引入

import { flexible } from 'modern-flexible'


flexible({
  // 以下数字只是举例
  // 具体宽度设定请自行定义
  distinctDevice: [
    { isDevice: (clientWidth: number) => clientWidth < 750, UIWidth: 375, deviceWidthRange: [300, 375] }, // 手机
    { isDevice: (clientWidth: number) => clientWidth >= 750 && clientWidth < 1280, UIWidth: 1280, deviceWidthRange: [960, 1280] }, // 平板
    { isDevice: (clientWidth: number) => clientWidth >= 1280, UIWidth: 1920, deviceWidthRange: [1280, 1920] }, // 电脑
  ],
})

html中引入

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 尽早引入flexible,避免页面闪烁 -->
  <script type="module" src="/src/flexible.ts"></script>
</head>

<body>
  <div id="root"></div>
  <script type="module" src="/src/main.tsx"></script>
</body>

</html>

配合vite插件使用(进阶)

在vite中使用,可以搭配 vite-plugin-public-typescript 使用,这样就可以在html中直接引入了

示例请参考 playground

tailwindcss

可配合 tailwindcss 实现多设备 自适应 + 响应式布局

1.1.0

6 months ago

1.0.1

6 months ago

1.0.0

8 months ago

0.0.8

2 years ago

0.0.3

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago