uni-app 使用 scss 实现推荐标签区域显示效果

效果图

<view class="tag-box">
  <view class="tag-tip">
    <view>店家</view>
    <view>推荐</view>
  </view>
</view>

方法一

只需修改 $tagFontSize(字体大小) 即可

/* 推荐标签区域 */
.tag-box {
  $tagFontSize: 26; // 字体大小(改变字体大小即可更改总体大小)
  $differ: -3; // 差值与字体大小相关(20[0] 22[-4] 24[-4] 26[-3] 28[-3] 30[-3])
  $tagFontColor: #fff; // 字体颜色
  $tagBgColor: #de1737; // 总体背景
  $tagPaddingUpAndDown: 10; // 上下内边距
  $tagPaddingLeftAndRight: 15; // 左右内边距
  $tagBorderRadius: 8rpx; // 圆角大小
  $tagBeforeTop: floor($tagFontSize / 0.75) * 2 + $differ + $tagPaddingUpAndDown * 2 + rpx;
  $tagBeforeBorderTop: $tagFontSize + rpx;
  $tagBeforeBorderSide: $tagFontSize + $tagPaddingLeftAndRight + rpx;
  .tag-tip {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: $tagFontColor;
    font-size: $tagFontSize + rpx;
    padding: $tagPaddingUpAndDown + rpx $tagPaddingLeftAndRight + rpx;
    background: $tagBgColor;
    border-radius: $tagBorderRadius;
    font-weight: 700;
  }
  .tag-tip::before {
    content: "";
    position: absolute;
    top: $tagBeforeTop;
    left: 0;
    width: 0;
    height: 0;
    border-top: solid $tagBeforeBorderTop $tagBgColor;
    border-right: solid $tagBeforeBorderSide transparent;
    border-left: solid $tagBeforeBorderSide transparent;
    border-radius: $tagBorderRadius;
  }
}

方法二

需要修改 $tagSize(总体大小) 与 $tagFontSize(字体大小) 的值

/* 推荐标签区域 */
.tag-box {
  $tagSize: 90; // 总体大小(只调整大小只需改 $tagSize 和 $tagFontSize 即可)
  $tagFontSize: 26rpx; // 字体大小(只调整大小只需改 $tagSize 和 $tagFontSize 即可)
  $tagFontColor: #fff; // 字体颜色
  $tagBgColor: #de1737; // 总体背景
  $tagBorderRadius: 8rpx; // 圆角大小
  $tagWidth: $tagSize - 5;
  $tagHeight: $tagSize;
  $tagPadding: $tagSize / 10 + rpx;
  $tagBeforeTop: $tagSize - 4 + rpx;
  $tagBeforeBorderTop: $tagSize / 4 + 5 + rpx;
  $tagBeforeBorderSide: $tagWidth / 2 + rpx;
  .tag-tip {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    width: $tagWidth + rpx;
    height: $tagHeight + rpx;
    color: $tagFontColor;
    font-size: $tagFontSize;
    padding: $tagPadding 0;
    background: $tagBgColor;
    border-radius: $tagBorderRadius;
    font-weight: 700;
  }
  .tag-tip::before {
    content: "";
    position: absolute;
    top: $tagBeforeTop;
    left: 0;
    width: 0;
    height: 0;
    border-top: solid $tagBeforeBorderTop $tagBgColor;
    border-right: solid $tagBeforeBorderSide transparent;
    border-left: solid $tagBeforeBorderSide transparent;
    border-radius: $tagBorderRadius;
  }
}
### 在uni-app项目中安装和配置SCSS #### 插件市场安装SASS插件 对于希望在uni-app项目中使用SCSS语法的开发者来说,在HBuilderX环境中,如果遇到提示未安装SCSS的情况,则需前往插件市场下载并安装相应的插件[^2]。登录个人HBuilderX账号之后,通过搜索功能定位到scss条目,点击进入详情页后选择“下载插件并导入”。这一操作会触发HX自动完成插件部署流程。 #### 使用包管理器安装依赖库 除了借助IDE内置的功能外,还需利用npm这样的包管理工具来引入必要的构建支持模块。执行如下命令可实现node-sass以及sass-loader这两个关键软件包的本地安装: ```bash npm install node-sass@4.14.1 sass-loader@7 --save-dev ``` 此步骤确保了后续编译过程中能够正确解析`.scss`文件内的语句结构[^4]。 #### 修改Vue单文件组件以兼容SCSS 为了使自定义样式表生效,应当调整各.vue文件内部的<style>区块声明方式。具体而言就是添加`lang="scss"`属性指示符告知预处理器采用何种方言编写CSS规则集。例如: ```html <template> <!-- 组件模板 --> </template> <script> // JavaScript逻辑代码... </script> <style lang="scss"> /* SCSS样式的书写 */ $primary-color: #4CAF50; body { background-color: $primary-color; } </style> ``` 上述实例展示了如何运用变量简化颜色值设定的过程[^3]。 #### Webpack配置优化建议 虽然大多数情况下默认设置已经足够满足需求,但对于某些特殊场景可能仍有必要进一步定制化Webpack配置项以便更高效地处理资源加载等问题。不过通常无需手动干预这部分细节除非遇到了特定的技术难题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值