视网膜上 1px 实现

发布于 2022-01-05 21:01:22 字数 6154 浏览 964 评论 0

使用到的技术

  1. background-image
  2. scale
// 1px on Retina
// -----------------------------------------------------------------------------
.retina(@top: transparent, @right: transparent, @bottom: transparent, @left: transparent, @w: 1px) {
    @media only screen and (-webkit-min-device-pixel-ratio: 2),
    only screen and (min--moz-device-pixel-ratio: 2),
    only screen and (-o-min-device-pixel-ratio: 2/1),
    only screen and (min-device-pixel-ratio: 2),
    only screen and (min-resolution: 192dpi),
    only screen and (min-resolution: 2dppx) {
        border: none;
        background-image:
            -webkit-linear-gradient(270deg, @top, @top 50%, transparent 50%),
            -webkit-linear-gradient(180deg, @right, @right 50%, transparent 50%),
            -webkit-linear-gradient(90deg, @bottom, @bottom 50%, transparent 50%),
            -webkit-linear-gradient(0, @left, @left 50%, transparent 50%);
        background-image:
            -moz-linear-gradient(270deg, @top, @top 50%, transparent 50%),
            -moz-linear-gradient(180deg, @right, @right 50%, transparent 50%),
            -moz-linear-gradient(90deg, @bottom, @bottom 50%, transparent 50%),
            -moz-linear-gradient(0, @left, @left 50%, transparent 50%);
        background-image:
            -o-linear-gradient(270deg, @top, @top 50%, transparent 50%),
            -o-linear-gradient(180deg, @right, @right 50%, transparent 50%),
            -o-linear-gradient(90deg, @bottom, @bottom 50%, transparent 50%),
            -o-linear-gradient(0, @left, @left 50%, transparent 50%);
        background-image:
            linear-gradient(180deg, @top, @top 50%, transparent 50%),
            linear-gradient(270deg, @right, @right 50%, transparent 50%),
            linear-gradient(0deg, @bottom, @bottom 50%, transparent 50%),
            linear-gradient(90deg, @left, @left 50%, transparent 50%);
        background-size: 100% @w, @w 100%, 100% @w, @w 100%;
        background-repeat: no-repeat;
        background-position: top, right top, bottom, left top;
    }
}

.top-1px(@color: transparent, @w: 1px) {
    @media only screen and (-webkit-min-device-pixel-ratio: 2),
    only screen and (min--moz-device-pixel-ratio: 2),
    only screen and (-o-min-device-pixel-ratio: 2/1),
    only screen and (min-device-pixel-ratio: 2),
    only screen and (min-resolution: 192dpi),
    only screen and (min-resolution: 2dppx) {
        border: none;
        background-image: -webkit-linear-gradient(270deg, @color, @color 50%, transparent 50%);
        background-image: -moz-linear-gradient(270deg, @color, @color 50%, transparent 50%);
        background-image: -o-linear-gradient(270deg, @color, @color 50%, transparent 50%);
        background-image: linear-gradient(180deg, @color, @color 50%, transparent 50%);
        background-size: 100% @w;
        background-repeat: no-repeat;
        background-position: top;
    }
}

.right-1px(@color: transparent, @w: 1px) {
    @media only screen and (-webkit-min-device-pixel-ratio: 2),
    only screen and (min--moz-device-pixel-ratio: 2),
    only screen and (-o-min-device-pixel-ratio: 2/1),
    only screen and (min-device-pixel-ratio: 2),
    only screen and (min-resolution: 192dpi),
    only screen and (min-resolution: 2dppx) {
        border: none;
        background-image: -webkit-linear-gradient(180deg, @color, @color 50%, transparent 50%);
        background-image: -moz-linear-gradient(180deg, @color, @color 50%, transparent 50%);
        background-image: -o-linear-gradient(180deg, @color, @color 50%, transparent 50%);
        background-image: linear-gradient(90deg, @color, @color 50%, transparent 50%);
        background-size: @w 100%;
        background-repeat: no-repeat;
        background-position: right;
    }
}

.bottom-1px(@color: transparent, @w: 1px) {
    @media only screen and (-webkit-min-device-pixel-ratio: 2),
    only screen and (min--moz-device-pixel-ratio: 2),
    only screen and (-o-min-device-pixel-ratio: 2/1),
    only screen and (min-device-pixel-ratio: 2),
    only screen and (min-resolution: 192dpi),
    only screen and (min-resolution: 2dppx) {
        border: none;
        background-image: -webkit-linear-gradient(90deg, @color, @color 50%, transparent 50%);
        background-image: -moz-linear-gradient(90deg, @color, @color 50%, transparent 50%);
        background-image: -o-linear-gradient(90deg, @color, @color 50%, transparent 50%);
        background-image: linear-gradient(0, @color, @color 50%, transparent 50%);
        background-size: 100% @w;
        background-repeat: no-repeat;
        background-position: bottom;
    }
}

.left-1px(@color: transparent, @w: 1px) {
    @media only screen and (-webkit-min-device-pixel-ratio: 2),
    only screen and (min--moz-device-pixel-ratio: 2),
    only screen and (-o-min-device-pixel-ratio: 2/1),
    only screen and (min-device-pixel-ratio: 2),
    only screen and (min-resolution: 192dpi),
    only screen and (min-resolution: 2dppx) {
        border: none;
        background-image: -webkit-linear-gradient(0deg, @color, @color 50%, transparent 50%);
        background-image: -moz-linear-gradient(0deg, @color, @color 50%, transparent 50%);
        background-image: -o-linear-gradient(0deg, @color, @color 50%, transparent 50%);
        background-image: linear-gradient(270deg, @color, @color 50%, transparent 50%);
        background-size: @w 100%;
        background-repeat: no-repeat;
        background-position: left;
    }
}

.border-1px(@ratio: 2, @color, @radius: 0) {
    position: relative;
    border: none !important;
    &::after {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        right: (1 - @ratio) * 100%;
        bottom: (1 - @ratio) * 100%;
        border: 1px solid @color;
        border-radius: @radius;
        .transform(scale(1 / @ratio));
        .transform-origin(0 0);
        pointer-events: none;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

JSmiles

生命进入颠沛而奔忙的本质状态,并将以不断告别和相遇的陈旧方式继续下去。

0 文章
0 评论
84961 人气
更多

推荐作者

爱人如己

文章 0 评论 0

萧瑟寒风

文章 0 评论 0

云雾

文章 0 评论 0

倒带

文章 0 评论 0

浮世清欢

文章 0 评论 0

撩起发的微风

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文