Giter Club home page Giter Club logo

autofit.js's Introduction

autofit

autofit.js

简体中文 | English

autofit.js

迄今为止最易用的自适应工具

理论上可以支持从你的设计稿以下的分辨率,只需一行代码。

autofit.js是一个可以让你的PC项目自适应屏幕的工具,其原理非常简单,即在scale等比缩放的基础上,向右或向下增加了宽度或高度,以达到充满全屏的效果,使用autofit.js不会挤压、拉伸元素,它只是单纯的设置了容器的宽高。

引入

import autofit from 'autofit.js'

快速开始

autofit.init()

使用

export default {  
  mounted() {
	autofit.init({
        dh: 1080,
        dw: 1920,
        el:"body",
        resize: true
    })
  },
}

以上使用的是默认参数,可根据实际情况调整,可选参数有

   * - el:渲染的dom,默认是 "body",必须使用id选择器 
   * - dw:设计稿的宽度,默认是 1920 
   * - dh:设计稿的高度,默认是 1080
   * - resize:是否监听resize事件,默认是 true
   * - ignore:忽略缩放的元素(该元素将反向缩放),参数见readme.md
   * - transition:过渡时间,默认是 0
   * - delay:默认是 0
   * - limit:默认是 0.1,当缩放阈值不大于此值时不缩放,比如设置为0.1时,0.9-1.1的范围会被重置为1

忽略某些元素

autofit.init({
  ignore: [
     { 
      el: ".gaodeMap",
     },
  ]
})

传入 ignore 以使元素不被缩放

更个性化的设置:

autofit.init({
  ignore: [
    {
      el: ".gaodeMap", //必填
      height: "300px",//可选,需注明单位
      width: "80%",//可选,需注明单位
      scale:1.2, //可选:回放程度,基于主元素缩放后的大小
      fontSize:26 //可选,如果自定义缩放后文字大小不合适,可以在这里设置文字大小
    },
    {
        //...
    }
  ]
})

如果反向缩放后的元素大小使结构发生变化,你还可以手动传入宽高、回放程度。

ignore还支持传入字符串数组:

autofit.init({
  ignore: ['.gaodeMap','.leaflet','#someElementClassOrId']
})

elRectification

一些canvas渲染的图表也会有事件偏移,而图表不同于地图,当使用ignore时,过大的图表可能会显示不全,因此可以使用elRectification (性能上不如ignore)

如果ignore无法满足需求,可以使用 autofit.elRectification(".classNameOrId")

import { elRectification } from 'autofit.js'
<div class="G2plot"></div>
<div class="G2plot"></div>
<div class="G2plot"></div>
onMounted(() => {
  elRectification(".G2plot")
})

使用 elRectification 时,需要被矫正的元素已经挂载

关闭 autofit.js造成的影响

当autofit未初始化时,会出现无法找到元素的错误,在使用autofit.off()前,请确保已初始化。

autofit.off()

autofit.js's People

Contributors

congwei614 avatar czq297297 avatar electroluxcode avatar larryzhu-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

autofit.js's Issues

针对于 ignore 属性是否存在优化项

目前:
image
期望:
image

目前支持 同时缩放xy轴,我期望 ignore 可以支持 仅缩放x轴仅缩放y轴。为了兼容老api,增加 ignoreX 和 ignoreY 来实现,你觉得怎么样?(我不确定我的使用是否正确,这是就目前情况来看我的想法)

出现留白

我配置后出现留白的情况呢
image
下面是我配置的代码:
image
下面是我的全局文件挂载的dom节点,也就是在 #root 上,
image
请大佬帮忙解惑呢

autofit.js 无法支持任何以屏幕坐标为基准计算属性的第三方插件

如elementUI和各类UI库的弹出层,是基于屏幕坐标计算弹出层位置的,经过transform后,这些计算也会偏移,但是这些偏移是有迹可循的,一般是
鼠标移动距离左上角占真实屏幕的百分比 == 整个页面等比缩放后内部元素所在位置到左上角占缩放后元素的百分比

对此应该由第三方工具调整元素位置的计算策略,以真实的屏幕宽高计算元素位置,或者将弹出层以其父元素为基准偏移,并挂载在body内部。

添加自定义容器

let clientHeight = document.documentElement.clientHeight;
let clientWidth = document.documentElement.clientWidth;

有些大屏页面可能并不是撑满浏览器的,比如有菜单栏,头部导航栏。
能不能增加自定义容器customContainer,如下

let container =document.querySelector(options.customContainer)
let clientHeight = container.clientHeight;
let clientWidth = container .clientWidth;

【bug】currelRectification参数未赋值

currelRectification参数未赋值导致空选择器
在off方法中调用的document.querySelectorAll(currelRectification)中并未用到传入的el参数也,会报错Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document': The provided selector is empty. at <anonymous>:1:27

用了button按钮点击后会出现灰色遮罩

我是vue2项目,我在app组件上用了所有button按钮点击后会出现灰色遮罩
mounted() {
autofit.init(
{
dh: 1080,
dw: 1920,
el: "#routerview-box",
resize: true,
ignore: [
{
el: ".gaodeMap",
},
],
},
false
);
},
beforeDestroy() {
autofit.off();
},

我用了下面的形式就没有了,不知道上面的形式是什么原因
mounted() {
autofit.init({
designHeight: 1080,
designWidth: 1920,
renderDom: "#app",
resize: true,
});
},

beforeDestroy() {
	autofit.off();
},

Cannot read properties of undefined (reading 'isAutofitRunnig')

想通过elRectification来忽略地图容器,但是报错:Cannot read properties of undefined (reading 'isAutofitRunnig')

onMounted(() => {
  elRectification('#map');
});

使用ignore忽略地图容器,会造成地图显示不全,容器宽度设置的都是100%,请问使用elRectification是否可以解决这问题,不忽略的话地图会显得很模糊

element-plus 弹窗异常

上次反映的 日历插件问题值不加拖拽功能就还好,这次这个 element-plus 就难受了。 弹窗后会造成页面右侧留白,element-ui 版本弹窗没有这个问题。
演示地址

添加地图拖拽不流畅

添加全局缩放之后,再加高德地图进去,拖拽感觉不够丝滑了,而且缩放也不是以鼠标所在位置开始缩放,是什么问题呀?

我该使用 px 还是 % 开发?能使用 vh/rem 等相对单位吗?

根据readme中描述,使用autofit.js,能够自动的拉伸或者缩小,对于大屏开发来讲,的确解决了好多问题,这里有两个基础的地方不太明白:
根据设计稿进行页面开发,最外层容器的宽与高是用设计稿的像素还是百分比?还是两者兼可?
容器内部各个元素的宽高,以及padding,margin的值用像素还是百分比?css可定义的值太多了,px, rem ,vh/vw,em,这些在引用autofit.js的项目中,是可以混用,还是单纯便用px来统一开发?

antd的下拉框位置会错误

绑定Body后,Select的下拉弹窗位置错误了。如果不绑定body,绑定到body下面的元素,由于下拉框都是渲染到Body下的,下拉框又不会被缩放。

扫码加入交流群(vue、vChart等官方团队成员坐镇)

如二维码失效加微信:larryzhu-

autofit.js 问题请私聊群主

在这里可以学到什么:

  1. 图表可视化和各种新奇技术
  2. webgis 疑难杂症
  3. 网页适配奇技淫巧
  4. 前端框架深入交流

内含 B站万粉博主 卡诺、v-scale-screen、v-draggable-plus作者远方、vChart团队成员玄魂,alova.js胡镇,大佬云集,卧虎藏龙

cf755b5c2b57f56cedba7eb267d57b1

玩法预览

image

image

image

image

image

fullCalendar 自适应失效

autofit.js 造成 fullCalendar 自适应异常,外部事件拖拽位置偏移
image

注释 autofit.init() 就正常

elRectification 方法,本地试了也没有用
演示地址

FullCalendar 插件自适应

1700795699977
使用 autofitjs 后FullCalendar 本身得高度宽度自适应会出现偏差,外部拖拽事件会发生偏移。使用 ignore和elRectification 方式也不大好使。
"@fullcalendar/vue3": "^6.1.9"
"autofit.js": "^3.0.6"
演示地址

宽度和高度不能沾满

1704272634010
这个是我没有全屏的样子

1704272736991

这个是我全屏的样子

没有全屏的时候宽度没有沾满
全屏的时候高度没有沾满

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.