Vue项目中封装一个sass的计算方法全局使用,设置自适应字体大小和布局

web端项目根据显示器做适配一般都是rem,

1rem=网页根节点字体大小,但是往往设计图给的尺寸都是px,pt,直接用sass封装一个方法,直接输入px值转rem,

先看看使用的情况

<style lang="scss" scoped>
.parameter-configuration {
  padding: rem(10);
  position: relative;
  height: 100%;
  .btns {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
    .lefgt {
      margin-left: rem(20);
    }
  }
}
</style>

废话不多说直接上代码

首先是新建一个scss文件.里面写上我们的计算方法

$remRate: 16;
@function rem($r) {
    @return $r / $remRate * 1rem;
}

 然后是在vue.config.js里面配置一下,这样就可以全局使用这个方法了

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath:'./',
  css:{
    loaderOptions:{
      scss:{
        additionalData:
        '@import "./src/style/rem.scss";'
      }
    }
  },
  devServer: {
    }
})

当然还有个简单的办法 就是给html的font-size设置为100px然后再使用rem的时候 就非常好计算了,不用写方法了

好了到此结束,前端菜鸟,记录遇到的问题和解决办法,各位大佬就当看个乐呵

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
抱歉,作为AI语言模型,我无法提供代码的可视化效果。但是,我可以为您提供一个基于Vue和SCSS的考试用户喜好选择页面的代码示例,希望对您有所帮助。 HTML模板: ``` <template> <div class="page-container"> <h1 class="title">请选择您的考试喜好</h1> <div class="options-container"> <div v-for="(option, index) in options" :key="index" class="option" :class="{ selected: selectedOption === option }" @click="selectOption(option)" > <div class="option-icon"> <i :class="option.icon"></i> </div> <div class="option-label">{{ option.label }}</div> </div> </div> <button class="submit-button" :disabled="selectedOption === null"> 开始考试 </button> </div> </template> ``` SCSS样式: ``` .page-container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; .title { font-size: 2rem; margin-bottom: 2rem; } .options-container { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; margin-bottom: 2rem; .option { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 15rem; height: 15rem; margin: 1rem; border-radius: 50%; background-color: #f1f1f1; cursor: pointer; transition: all 0.2s ease-in-out; &:hover { transform: scale(1.05); } &.selected { background-color: #1a1a1a; color: #fff; .option-icon { color: #fff; } } .option-icon { font-size: 5rem; margin-bottom: 1rem; color: #1a1a1a; transition: all 0.2s ease-in-out; } .option-label { font-size: 1.5rem; font-weight: bold; text-align: center; } } } .submit-button { padding: 1rem 2rem; font-size: 1.5rem; font-weight: bold; border: none; border-radius: 0.5rem; background-color: #1a1a1a; color: #fff; cursor: pointer; transition: all 0.2s ease-in-out; &:hover { background-color: #fff; color: #1a1a1a; } &:disabled { background-color: #c1c1c1; color: #fff; cursor: not-allowed; } } } ``` Vue组件: ``` <script> export default { data() { return { options: [ { label: '编程', icon: 'fab fa-codepen' }, { label: '文学', icon: 'fas fa-book-open' }, { label: '科学', icon: 'fas fa-flask' }, { label: '艺术', icon: 'fas fa-palette' } ], selectedOption: null }; }, methods: { selectOption(option) { this.selectedOption = option; } } }; </script> ``` 这个页面包括一个标题、一组可选项、一个提交按钮。每个可选项都有一个图标和一个标签。用户可以单击选项以选择其喜好。选定的选项将以不同的样式突出显示,并且提交按钮将在没有选定选项的情况下禁用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丝网如风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值