百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

SCSS绘制角标:右下角、右上角、左上角、左下角

myzbx 2025-01-31 14:13 39 浏览

本文讲述基于SCSS编写样式实现角标。参考上文【WEB:将i清楚CSS、Less、Sass、Scss】。实现后的效果如下图:


原理

CSS有一个特性,就是可以指定一个矩形框边框的宽度,以及其颜色。如下图:

然后缩小矩形框本身的尺寸,例如从300px改为100px:


然后继续直到矩形的长度和宽度都是0,就会看到一个完全由边组成的矩形,如下图:


然后我们来改变边的颜色,例如




这样就可以绘制出三角形了,当然也可以搞成沙漏型,和带上圆角。



实现

使用绝对定位,将三角形布局到指定位置,并通过z-index遮挡,实现条带。然后通过文本框绝对定位,并旋转到指定位置。

绝对定位一个大的三角形布局到特定位置。


.script-tag-container {
  margin: 1rem;
  position: relative;
  height: 100px;
  border: 1px solid #efefef;
  background-color: #efefef;
  border-radius: 5px;
}
.script-tag-container .rb-large {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  bottom: 0;
  right: 0;
  border-top: 30px solid transparent;
  border-left: 30px solid transparent;
  border-bottom: 30px solid red;
  border-right: 30px solid red;
  z-index: 0;
}
.script-tag-container .rb-small {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  bottom: 0;
  right: 0;
  border-top: 10px solid transparent;
  border-left: 10px solid transparent;
  border-bottom: 10px solid #efefef;
  border-right: 10px solid #efefef;
  z-index: 1;
}
.script-tag-container .rb-text {
  position: absolute;
  color: white;
  width: 84.8528137424px;
  height: 84.8528137424px;
  font-size: 1rem;
  text-align: center;
  transform-origin: center center;
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: flex-end; /* 垂直靠底部 */
  transform: rotate(-45deg); /*逆时针旋转*/
  bottom: 0;
  right: 0;
}

SCSS

定义相应的变量

/*_vars.scss*/
$script-tag-background-color: #efefef;

$script-tag-raduis: 5px;

$script-tag-angle: 45deg;

$cos-script-tag-angle: cos($script-tag-angle);
$sin-script-tag-angle: sin($script-tag-angle);

定义函数

/*_index.scss*/
@import 'vars';

@mixin script-tag-text($edge-length:2rem,$mode:"rb",$font-color:white,$font-size:0.75rem){
    position: absolute;
    color: $font-color;
    width: $edge-length / $sin-script-tag-angle;
    height: $edge-length / $cos-script-tag-angle;
    font-size: $font-size;
    text-align: center;
        
    transform-origin: center center;

    

    @if $mode == "rb"{
        display: flex;
        justify-content: center; /* 水平居中 */
        align-items: flex-end; /* 垂直靠底部 */
        transform: rotate(calc(-1 * $script-tag-angle)); /*逆时针旋转*/
        bottom: 0;
        right: 0;
    }@else if $mode == "rt"{
        display: flex;
        justify-content: center; /* 水平居中 */
        align-items: flex-start; /* 垂直靠底部 */
        transform: rotate($script-tag-angle);/*正时针旋转*/
        top: 0;
        right:0;
    }@else if $mode == "lt"{
        display: flex;
        justify-content: center; /* 水平居中 */
        align-items: flex-start; /* 垂直靠顶部 */
        transform: rotate(calc(-1 * $script-tag-angle)); /*逆时针旋转*/
        
        top: 0;
        left: 0;
    }@else if $mode == "lb"{
        display: flex;
        justify-content: center; /* 水平居中 */
        align-items: flex-end; /* 垂直靠底部 */
        transform: rotate($script-tag-angle);/*正时针旋转*/
        left: 0;
        bottom:0;
    }

}

@mixin script-tag-triangle($edge-color:red,$edge-length:2rem,$mode:"rb"){
    position: absolute;
    content: '';
    width: 0;
    height: 0;
    @if $mode == "rb"{
        bottom: 0;
        right: 0;
        border-top:$edge-length solid transparent;
        border-left:$edge-length solid transparent;
        border-bottom:$edge-length solid $edge-color;
        border-right:$edge-length solid $edge-color;
    }@else if $mode == "lt"{
        top: 0;
        left: 0;
        border-top:$edge-length solid $edge-color;
        border-bottom:$edge-length solid transparent;
        border-left:$edge-length solid $edge-color;
        border-right:$edge-length solid transparent;
    }@else if $mode == "lb"{
        bottom:  0;
        left: 0;
        border-top:$edge-length solid transparent;
        border-bottom:$edge-length solid $edge-color;
        border-left:$edge-length solid $edge-color;
        border-right:$edge-length solid transparent;
    }@else if $mode == "rt"{
        top: 0;
        right: 0;
        border-top:$edge-length solid $edge-color;
        border-right: $edge-length solid $edge-color;
        border-bottom:$edge-length solid transparent;
        border-left:$edge-length solid transparent;
    }
}

.script-tag-container{
    margin: 1rem;
    position: relative;
    height: 100px;
    border: 1px solid $script-tag-background-color;
    background-color: $script-tag-background-color;
    border-radius: $script-tag-raduis;

    .rb-large{
        @include script-tag-triangle(red, 30px,"rb" );
        z-index: 0;
    }
    .rb-small{
        @include script-tag-triangle($script-tag-background-color, 10px,"rb" );
        z-index: 1;
    }
    .rb-text{
        @include script-tag-text(60px,"rb",white ,1rem)
    }


    
    .rt-large{
        @include script-tag-triangle(orange, 30px,"rt" );
        z-index: 0;
    }
    .rt-small{
        @include script-tag-triangle($script-tag-background-color, 10px,"rt" );
        z-index: 1;
    }
    .rt-text{
        @include script-tag-text(60px,"rt",white ,1rem)
    }

    .lt-large{
        @include script-tag-triangle(blue, 30px,"lt" );
        z-index: 0;
    }
    .lt-small{
        @include script-tag-triangle($script-tag-background-color, 10px,"lt" );
        z-index: 1;
    }
    .lt-text{
        @include script-tag-text(60px,"lt",white ,1rem)
    }

    .lb-large{
        @include script-tag-triangle(green, 30px,"lb" );
        z-index: 0;
    }
    .lb-small{
        @include script-tag-triangle($script-tag-background-color, 10px,"lb" );
        z-index: 1;
    }
    .lb-text{
        @include script-tag-text(60px,"lb",white ,1rem)
    }
}


相关推荐

vue:生命周期钩子函数及顺序_列举出5个vue中常用的生命周期钩子函数

一、vue的钩子相关顺序Vue实例有一个完整的生命周期,在newVue()后,会初始化数据,如下://初始化的入口,各种初始化工作initMixin(Vue);//数据绑定的核心方法,包括常用...

最长递增子序列:从经典算法到 Vue3 运行时核心优化

最长递增子序列(LongestIncreasingSubsequence,LIS)正悄然成为性能分水岭。它不仅是面试的高频考点,更是Vue3快速Diff算法赖以实现O(nlogn)...

十分钟掌握Vue 3性能优化:实战技巧与避坑指南

「为什么我的Vue应用越做越卡?」这是最近团队新人最常问的问题。本文将从真实电商项目出发,手把手教你用Vue3的现代特性实现性能飞跃,文末还准备了可复用的优化检查清单!一、先看疗效:优化前后对比优...

JavaScript学习 -- 文本节点_html 文本节点

什么是文本节点在HTML文档中,文本节点是一种特殊的dom节点,它包含文本内容,没有任何标记或属性。<p>这是一段文本节点</p>在上面的代码中,<p>元素包含了...

JavaScript中this指向各种场景_javascript的this指向

在JavaScript中,this的指向是一个核心概念,其值取决于函数的调用方式,而非定义位置(箭头函数除外)。以下是this指向的常见场景及具体说明:1.全局作用域中的this在全局作用域(非...

v-if和v-for的优先级是什么?_v-if和v-for的区别,什么时候用

#一、作用v-if指令用于条件性地渲染一块内容。这块内容只会在指令的表达式返回true值的时候被渲染v-for指令基于一个数组来渲染一个列表。v-for指令需要使用iteminitems...

Vue插槽(Slot)深度解析:从匿名到作用域的组件复用革命

在Vue组件化开发中,内容分发始终是核心挑战之一。当我们需要让组件既能保持结构复用,又能灵活定制局部内容时,插槽(Slot)机制应运而生。从基础的匿名插槽到复杂的作用域插槽,Vue的插槽系统逐步解决了...

手摸手带你解决AI应用开发中Markdown渲染问题

使用Markdown-It+VueRender实现安全可控的Markdown渲染在前端项目中,Markdown的渲染经常使用markdown-it。它功能丰富、插件多,但默认的渲染方...

Vue3 新趋势:10 个最强 X 操作!_vue.3

Vue3为前端开发带来了诸多革新,它不仅提升了性能,还提供了更简洁、更强大的API。以下是十个最值得学习和使用的Vue3API,它们将助力你的开发工作迈向新高度。浅层响应式API:shall...

25个React最佳实践小技巧_reactor设计模式

以下是25个React开发中实用的最佳实践与小技巧,覆盖组件设计、状态管理、性能优化、代码规范、错误处理等核心场景,每个技巧均附示例和核心原因,帮助你写出更高效、可维护的React代码。一...

javascript函数的call、apply和bind的原理及作用详解

javascript函数的call、apply和bind本质是用来实现继承的,专业点说法就是改变函数体内部this的指向,当一个对象没有某个功能时,就可以用这3个来从有相关功能的对象里借用过来...

简单介绍一下前端各框架中的模板标签

在各大前端框架、小程序中,此类标签的作用主要是用来帮助我们包裹多个元素。在浏览器实际渲染中会将其移除只渲染其包裹的DOM元素,所以说不会增加额外的DOM节点在小程序中使用小程序中的模板标签是<...

面试官问我,后端一次性返回十万条数据,前端应该怎么处理 ?

问题描述面试官:后端一次性返回10万条数据给你,你如何处理?我:歪嘴一笑,马上给后端发送一百万次请求,干蹦他的服务器,让他给爷哭!问题考察点性能优化意识(能否识别出“10万条数据”会导致性能问题?是...

React系列十 - 高阶组件以及组件补充

源自:coderwhy一.高阶组件1.1.认识高阶组件什么是高阶组件呢?相信很多同学都听说过,也用过高阶函数,它们非常相似,所以我们可以先来回顾一下什么是高阶函数。高阶函数的维基百科定义:至少...

从0开始写一个虚拟滚动组件_虚拟滚动原理

如果一个页面有1W+条数据,该怎么渲染比较好。不管是在我们的实际项目开发中还是在面试的过程中都会遇到类似的问题。相信很多同学会想到分页。当然这也是最传统也是最保底的解决方案了。如果有开发过electr...