有一个相当流行的 CSS 技巧,它使用透明边框在宽度/高度为 0 的元素上模拟三角形。这里有一个 CSS 代码片段 在 CSS-Tricks 上展示了它。
如果,像我一样,你总是记不住它是如何工作的,请确保我们可以使用 Sass 来帮助我们。
/// Triangle helper mixin
/// @param {Direction} $direction - Triangle direction, either `top`, `right`, `bottom` or `left`
/// @param {Color} $color [currentcolor] - Triangle color
/// @param {Length} $size [1em] - Triangle size
@mixin triangle($direction, $color: currentcolor, $size: 1em) {
@if not index(top right bottom left, $direction) {
@error "Direction must be either `top`, `right`, `bottom` or `left`.";
}
width: 0;
height: 0;
content: '';
z-index: 2;
border-#{opposite-position($direction)}: ($size * 1.5) solid $color;
$perpendicular-borders: $size solid transparent;
@if $direction == top or $direction == bottom {
border-left: $perpendicular-borders;
border-right: $perpendicular-borders;
} @else if $direction == right or $direction == left {
border-bottom: $perpendicular-borders;
border-top: $perpendicular-borders;
}
}
其他说明
* opposite-position
函数 来自 Compass;如果你不使用 Compass,你可能需要 自己实现一个;
* Mixin 不处理三角形的位置;不过,完全可以将其与 定位 Mixin 组合使用;
* content
指令旨在允许它用于伪元素,实际上这才是大多数情况。
用法
.foo::before {
@include triangle(bottom);
position: absolute;
left: 50%;
bottom: 100%;
}
.foo::before {
width: 0;
height: 0;
content: '';
z-index: 2;
border-top: 1.5em solid currentColor;
border-left: 1em solid transparent;
border-right: 1em solid transparent;
position: absolute;
left: 50%;
bottom: 100%;
}
我也会添加
transform: scale(1.01)
https://brettstrikesback.com/de-pixelating-the-css-triangle/
我认为大小(边框宽度)应该动态的。有时你需要两个值,例如:
border-width: 5px 8px;
此外,你在混合中提到了“opposite-position”这个词。这对于 SASS 没有任何意义……它会破坏编译器
你好,
很棒的发现。我调试了一下。现在使用 SCSS 运行良好。我不确定我之前代码哪里出错了,但我无法显示颜色参数。下面的 Mixin 修改用于例如
@include triangle(top,#CCC,8px);
……………………………………………………………..
@mixin triangle($direction, $color: currentcolor, $size: 1em) {
@if not index(top right bottom left, $direction) {
@error “方向必须是
top
、right
、bottom
或left
。”;}
width: 0;
height: 0;
content: ”;
z-index: 2;
$perpendicular-borders: $size solid transparent;
@if $direction == top {
border-left: $perpendicular-borders;
border-right: $perpendicular-borders;
border-bottom: $size solid $color;
} @else if $direction == bottom {
border-left: $perpendicular-borders;
border-right: $perpendicular-borders;
border-top: $size solid $color;
} @else if $direction == right {
border-bottom: $perpendicular-borders;
border-top: $perpendicular-borders;
border-left: $size solid $color;
} @else if $direction == left {
border-bottom: $perpendicular-borders;
border-top: $perpendicular-borders;
border-right: $size solid $color;
}
}
/*
* 用于基本 CSS 三角形的 Mixin
* @include triangle(up, #000, 50px);
* @include triangle(bottomleft, #000, 50px);
*/
@mixin triangle($direction:up, $color:#000, $size:100px) {
@if ($direction == up){
border-color: transparent transparent $color;
border-style: solid;
border-width: 0 $size $size;
height: 0;
width: 0;
}
@if ($direction == down) {
border-color: $color transparent transparent transparent;
border-style: solid;
border-width: $size;
height: 0;
width: 0;
}
@if ($direction == left) {
border-color: transparent $color transparent transparent;
border-style: solid;
border-width: $size $size $size 0;
height: 0;
width: 0;
}
@if ($direction == right) {
border-color: transparent transparent transparent $color;
border-style: solid;
border-width: $size 0 $size $size;
height: 0;
width: 0;
}
@if ($direction == bottomright) {
border-color: transparent transparent $color transparent;
border-style: solid;
border-width: 0 0 $size $size;
height: 0;
width: 0;
}
@if ($direction == bottomleft) {
border-color: transparent transparent transparent $color;
border-style: solid;
border-width: $size 0 0 $size;
height: 0;
width: 0;
}
@if ($direction == topleft) {
border-color: $color transparent transparent transparent;
border-style: solid;
border-width: $size $size 0 0;
height: 0;
width: 0;
}
@if ($direction == topright) {
border-color: transparent $color transparent transparent;
border-style: solid;
border-width: 0 $size $size 0;
height: 0;
width: 0;
}
}
感谢评论和修复。此外,我认为大小参数实际上应该除以 2,对于左右箭头,才能获得 1em 的准确高度。如果您按照文本高度计算,则顶部和底部的 1em 箭头实际上是左右 .5em 箭头。
对于尝试使用该代码的人,您需要包含函数
opposite-direction
,并且您需要从链接的文章中获取该函数,除了它是 **opposite-direction**,而不是 *opposite-position*(它不存在,*position* mixin 是另一回事)。