css3 scaleout,在vue项目采用了css3的缩放transform: scale(X)属性适配

在components目录下创建ScaleBox.vue文件

class="ScaleBox"

ref="ScaleBox"

:style="{

width: width + 'px',

height: height + 'px',

}"

>

export default {

name: "ScaleBox",

props: {

},

data() {

return {

scale: "",

width:1920,

height:937

};

},

mounted() {

this.setScale();

window.addEventListener("resize", this.debounce(this.setScale, 100));

},

methods: {

getScale() {

let { width, height } = this;

let wh = window.innerHeight / height;

let ww = window.innerWidth / width;

console.log(ww < wh ? ww : wh);

return ww < wh ? ww : wh;

},

setScale() {

if(window.innerHeight ==1080){

this.height = 1080

}else{

this.height = 937

}

this.scale = this.getScale();

if (this.$refs.ScaleBox) {

this.$refs.ScaleBox.style.setProperty("--scale", this.scale);

}

},

debounce(fn, delay) {

let delays = delay || 500;

let timer;

return function () {

let th = this;

let args = arguments;

if (timer) {

clearTimeout(timer);

}

timer = setTimeout(function () {

timer = null;

fn.apply(th, args);

}, delays);

};

},

},

};

#ScaleBox {

--scale: 1;

}

.ScaleBox {

position: absolute;

transform: scale(var(--scale)) translate(-50%, -50%);

display: flex;

flex-direction: column;

transform-origin: 0 0;

left: 50%;

top: 50%;

transition: 0.3s;

z-index: 999;

}

直接引用

import ScaleBox from '@/components/ScaleBox.vue'

内容

### 使用 CSS 实现元素缩放效果 `transform: scale()` 示例 `transform: scale()` 是一种强大的工具,能够通过指定的比例调整 HTML 元素的大小。以下是其实现方式及其具体应用: #### 基础语法 `scale()` 函数接受一个或两个参数,分别表示 X 和 Y 方向上的缩放比例。如果只提供一个参数,则该值同时应用于 X 和 Y 方向。 ```css /* 单一参数 */ .element { transform: scale(1.5); /* 同时在X轴和Y轴方向放大至原尺寸的1.5倍 */ } /* 双参数 */ .element { transform: scale(2, 0.5); /* X轴放大到原来的两倍,Y轴缩小到原来的一半 */ } ``` 上述代码展示了如何利用单参数或多参数形式实现等比例或非等比例缩放[^2]。 --- #### 动态交互示例:鼠标悬停触发缩放 当用户将鼠标移动到某个元素上方时,可以通过 `hover` 伪类结合 `transform: scale()` 来创建动态交互效果。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Transform Scale Example</title> <style> .box { width: 100px; height: 100px; background-color: blue; transition: transform 0.3s ease-in-out; /* 添加平滑过渡效果 */ } .box:hover { transform: scale(1.2); /* 鼠标悬浮时放大1.2倍 */ } </style> </head> <body> <div class="box"></div> </body> </html> ``` 此示例中,`.box` 类定义了一个蓝色正方形,在用户将其鼠标悬停在其上时,会逐渐放大至原始尺寸的 1.2 倍,并带有流畅的动画过渡效果[^1]。 --- #### 自定义缩放中心点 默认情况下,`scale()` 的变换是以元素的几何中心作为基点执行的。然而,开发者可通过设置 `transform-origin` 属性来自定义缩放的起始位置。 ```css .element { transform: scale(2); transform-origin: top left; /* 设置缩放从左上角开始 */ } ``` 在此配置下,元素将以其左上角为起点进行放大操作,而非传统的中心点。 --- #### 数据可视化场景下的自适应缩放 对于需要适配多种屏幕分辨率的数据可视化项目而言,合理运用 `transform: scale()` 结合 JavaScript 或 Vue.js 混入逻辑可显著提升用户体验。例如,以下是一个基于 Vue.js 构建的大屏展示组件案例: ```vue <template> <div id="app"> <div class="auto-scal-container" ref="AutoScalContainerRef"> <div ref="DomRef" class="auto-scal-container-inner"> <router-view /> </div> </div> </div> </template> <script> import autoResizeScreenMixin from &#39;@/utils/autoResizeScreenMixin&#39;; export default { mixins: [autoResizeScreenMixin], }; </script> <style scoped> .auto-scal-container { position: relative; overflow: hidden; width: 1920px; /* 设计稿宽度 */ height: 1080px; /* 设计稿高度 */ transform-origin: center; } .auto-scal-container-inner { transform: scale(var(--scal-factor)); /* 动态计算缩放因子 */ } </style> ``` 这段代码片段实现了根据实际设备宽高自动调整容器内的内容显示比例的功能,从而确保无论在哪种屏幕上都能呈现一致的效果[^5]。 --- ### 注意事项 1. **布局影响** 尽管视觉上发生了变化,但实际上元素占用的空间并未发生任何改变。这意味着其他相邻的内容不会因当前对象被拉伸而重新排列。 2. **性能优化** 如果频繁调用大规模复杂变形动作可能引起浏览器重绘开销增加,建议适当控制频率并考虑硬件加速技术(如启用 GPU 渲染)。 3. **兼容性考量** 虽然现代主流浏览器普遍支持这些特性,但在针对老旧版本 IE 用户群体设计网页时仍需额外注意替代方案的选择。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值