• CSS 0.5px 细线边框的原理和实现方式


      细线边框的具体实现方法有:伪元素缩放或渐变,box-shadow模拟,svg画线,border-image裁剪等。要实现小于1px的线条,有个先决条件:屏幕的分辨率要足够高,设备像素比要大于1,即css中的1个像素对应物理屏幕中1个以上的像素点。

      对于普通电脑,屏幕物理像素和css像素一一对应,显示的最小单位就是1px。而现在的手机,屏幕物理宽度一般都大于页面显示宽度。例如苹果6s的屏幕分辨率为1334x750像素,但是以375px的宽度显示页面,设备像素比就是750/375=2;此时在css中定义0.5px的宽度,实际上对应物理屏幕的1个像素点,这就是border小于1px的的实现基础。

     1 <!-- @media鉴别设备像素比 -->
     2 <style>
     3 @media only screen and (-webkit-min-device-pixel-ratio: 2){
     4   .fineLine{
     5     -webkit-transform: scaleY(.5);
     6   }
     7 }
     8 </style>
     9  
    10 <!-- js获取设备像素比 -->
    11 <script type="text/javascript">
    12   var dpr = window.devicePixelRatio;
    13   // 如下方法精确计算出了物理设备与css的像素比dppx。但实际使用中dpr更广泛,也足以满足一般需求
    14   var dppx = window.screen.width/(window.innerWidth || document.documentElement.clientWidth);
    15 </script>

    一、缩放biefore/after伪元素

      伪元素进行绝对定位,background着色要优于border着色,适合画单线条:

     1 <div class="fineLine"></div>
     2 <!-- fineLine的:before为上边框,:after为下边框 -->
     3 <style type="text/css">
     4 .fineLine {
     5     position: relative;
     6 }
     7 .fineLine:before,.fineLine:after{
     8   position: absolute;
     9   content: " ";
    10   height: 1px;
    11   width: 100%;
    12   left: 0;
    13   transform-origin: 0 0;
    14   -webkit-transform-origin: 0 0;
    15 }
    16 .fineLine:before {
    17     top: 0;
    18     background: #000;
    19 }
    20 .fineLine:after {
    21     bottom: 0;
    22     border-bottom: 1px solid #000;
    23 }
    24 @media only screen and (-webkit-min-device-pixel-ratio: 1.5) {
    25     .fineLine:after,.fineLine:before {
    26         -webkit-transform: scaleY(.667);
    27     }
    28 }
    29 @media only screen and (-webkit-min-device-pixel-ratio: 2) {
    30     .fineLine:after,.fineLine:before {
    31         -webkit-transform: scaleY(.5);
    32     }
    33 }
    34 </style>

    二、box-shadow模拟

      box-shaodw适合模拟box四周都需要细线border的情况,而且支持border-radius。此例中演示的是dppx鉴别:

     1 <div class="fineLine"></div>
     2  
     3 <style type="text/css">
     4 .fineLine {
     5     box-shadow: 0 0 0 1px;
     6 }
     7 @media (min-resolution: 2dppx) {
     8     .fineLine {
     9         box-shadow: 0 0 0 0.5px;
    10     }
    11 }
    12 @media (min-resolution: 3dppx) {
    13     .fineLine {
    14         box-shadow: 0 0 0 0.33333333px;
    15     }
    16 }
    17 </style>
  • 相关阅读:
    正则表达式
    面向对象(访问控制、继承、重写)
    你凌晨1点半会想什么
    10、android学习资源整理
    9、android开发之java.lang.verifyError(转载)
    8、android代码优化技术记录
    6、android 普通日志输出到SD卡
    刷机总结(阿里云os-->android4.2.2)注明:本文不是教程
    忘记树莓派密码怎么办?
    7、android的button如何平铺一张图片?
  • 原文地址:https://www.cnblogs.com/yangshifu/p/9257519.html
Copyright © 2020-2023  润新知