vue中设置html的fontsize,vue中rem布局使用js动态改变fontsize

rem布局原理:使用js动态改变html的字体大小+rem的特性,来保证最初的设计图中每个元素的尺寸比例不变,以适用于不同尺寸的设备能够正常显示。

(function (doc, win) {

var docEl = doc.documentElement,

resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',

recalc = function () {

var clientWidth = docEl.clientWidth;

if (!clientWidth) return;

docEl.style.fontSize = 50 * (clientWidth / 375) + 'px';

console.log(docEl.style.fontSize)

};

if (!doc.addEventListener) return;

win.addEventListener(resizeEvt, recalc, false);

doc.addEventListener('DOMContentLoaded', recalc, false);

})(document, window);

引用在main.js中

67981c2f4b1c262aafe1fae84491109e.png

在html是&#x

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue ,可以使用动态绑定来实现不同页面的 `rem` 值不同。具体实现步骤如下: 1. 在 `index.html` 设置根元素的 `font-size` 值: ```html <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My App</title> <style> html { font-size: 62.5%; /* 设置为 10px,方便计算 */ } /* 其他样式 */ </style> </head> <body> <div id="app"></div> <script src="/dist/js/app.js"></script> </body> </html> ``` 2. 在 `main.js` 动态设置根元素的 `font-size` 值: ```javascript import Vue from 'vue' import App from './App.vue' Vue.config.productionTip = false; new Vue({ render: h => h(App) }).$mount('#app'); // 设置根元素的 font-size 值 const setRootFontSize = () => { const html = document.getElementsByTagName('html')[0]; const width = html.getBoundingClientRect().width; const fontSize = width / 1920 * 100; // 1920 是设计稿的宽度,可以根据实际情况修改 html.style.fontSize = `${fontSize}px`; }; setRootFontSize(); // 页面初始化时设置一次 window.addEventListener('resize', setRootFontSize); // 监听窗口大小变化,重新设置 font-size 值 ``` 3. 在需要修改 `rem` 值的页面,可以使用 `scoped` 样式来覆盖根元素的 `font-size` 值,实现动态修改 `rem` 值。例如: ```vue <template> <div class="page"> <h1>这是一个示例页面</h1> <p class="text">这是一段文字</p> </div> </template> <style scoped> .page { /* 设置根元素的 font-size 值为 50px */ font-size: 50px; } .text { /* 使用动态计算的 rem 值,实现字体大小的适配 */ font-size: 1.4rem; } </style> ``` 通过上述方法,就可以实现在 Vue 动态修改不同页面的 `rem` 值,从而实现字体大小的适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值