前端多主题多字体,如何实现?

2021-08-19 00:43:24 +08:00
 MuscleOf2016

问题: 现在大字超大字,不同主题,都做了不同的设计稿,不同字体之间并不是按比列缩放的.而且设计稿这个问题,并不能很好解决,是公司问题.

解决办法: 普通字,大字,超大字,一个页面三套样式,再配置主题色的 class 切换 来实现,暂时看效果还行,就是代码量比较多,有无其他好的解决办法.

注: 设计稿这个没办法, 三套设计稿关系不是很大,缩放没统一比列.

1159 次点击
所在节点    前端开发
3 条回复
tanranran
2021-08-19 01:22:36 +08:00
可以用态换换肤

<html lang="en">
<head>
<title>js 动态换肤</title>
<!-- 利用 axios 实现异步加载样式-->
<script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js"></script>
</head>
<body>
<h3 class="title">js 动态换肤</h3>
<script>
// 1. 主题颜色配置
var colors = {
red: {
themeColor: '#FF0000'
},
blue: {
themeColor: '#0000FF'
}
}

// 2. 异步获取样式
var styles = ''
axios.get('theme.css').then((resp=> {
const colorMap = {
'#FF0000': 'themeColor'
}
styles = resp.data
Object.keys(colorMap).forEach(key => {
const value = colorMap[key]
styles = styles.replace(new RegExp(key, 'ig'), value)
console.log(styles)
})
writeNewStyle (styles, colors.red)
}))

// 3.换色
// console.log 中输入 writeNewStyle (styles, colors.blue)可以换蓝色主题
// console.log 中输入 writeNewStyle (styles, colors.blue)可以换红色主题
function writeNewStyle (originalStyle, colors) {
let oldEl = document.getElementById('temp-style')
let cssText = originalStyle

Object.keys(colors).forEach(key => {
cssText = cssText.replace(new RegExp(key, 'ig'), colors[key])
})
const style = document.createElement('style')
style.innerText = cssText
style.id = 'temp-style'

oldEl ? document.head.replaceChild(style, oldEl) : document.head.appendChild(style)
}
</script>
</body>
</html>


.title {
color: #FF0000
}
xiaopc
2021-08-19 09:14:31 +08:00
如果只需要考虑 Chrome 49+ 的话,可以用 CSS variables
要兼容更好的话,用 sass/scss 写,生成的时候再编译成 CSS
MuscleOf2016
2021-08-19 09:44:24 +08:00
@xiaopc 并不能用,会不兼容

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/796653

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX