zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    2013腾讯编程马拉松初赛第〇场(3月20日)湫湫系列故事——植树节 HDOJ 4503
    组合模式(Composite)
    Git Push 避免用户名和密码方法
    [Yii2] 快速套模板,加载JS,CSS(HTML标签<base>)
    phpstorm 2016.2.x 最新版激活方法
    PHP实现四种基本排序算法
    linux下查看负载均衡的两种方法
    Redis各类型应用场景
    HTTP协议2:请求、响应、缓存 2017-03-16 11:20 197人阅读 评论(0) 收藏
    HTTP协议1:工作原理 2017-03-16 11:18 39人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/yangshifu/p/9257519.html
Copyright © 2011-2022 走看看