sass,stylus,styled-component,對一像素線的封裝


原理: 

    給要加1像素的元素一個相對定位,然后給這個元素添加上
一個偽元素,給這個偽元素一個絕對定位,讓他的寬高根據dpr的
值放大相應的倍數為1放大100%,為2放大200%,為3放大300%
然后在使用css3屬性等比縮放0.5,或0.3333倍,最后拿到的結果
就是1像素線

stylus

使用方法

border 0 0 0 0,color,solid,2px

參數1:邊框寬度

參數2:顏色(可選)默認#ccc

參數3:樣式(可選)默認solid

參數4:圓角(可選)默認0px

border($border-width = 1px, $border-color = #ccc, $border-style = solid, $radius = 0)
  // 為邊框位置提供定位參考
  position: relative;

  if $border-width == null
    $border-width: 0;

  border-radius: $radius;

  &::after
    // 用以解決邊框layer遮蓋內容
    pointer-events: none;
    position: absolute;
    z-index: 999;
    top: 0;
    left: 0;
    // fix當元素寬度出現小數時,邊框可能顯示不全的問題
    // overflow: hidden;
    content: "\0020";
    border-color: $border-color;
    border-style: $border-style;
    border-width: $border-width;
    
    // 適配dpr進行縮放
    @media (max--moz-device-pixel-ratio: 1.49), (-webkit-max-device-pixel-ratio: 1.49), (max-device-pixel-ratio: 1.49), (max-resolution: 143dpi), (max-resolution: 1.49dppx)
      width: 100%;
      height: 100%;
      if $radius != null {
        border-radius: $radius;
      }
    
    @media (min--moz-device-pixel-ratio: 1.5) and (max--moz-device-pixel-ratio: 2.49), (-webkit-min-device-pixel-ratio: 1.5) and (-webkit-max-device-pixel-ratio: 2.49),(min-device-pixel-ratio: 1.5) and (max-device-pixel-ratio: 2.49),(min-resolution: 144dpi) and (max-resolution: 239dpi),(min-resolution: 1.5dppx) and (max-resolution: 2.49dppx)
      width: 200%;
      height: 200%;
      transform: scale(.5)
      if $radius != null {
        border-radius: $radius * 2;
      }
    
    @media (min--moz-device-pixel-ratio: 2.5), (-webkit-min-device-pixel-ratio: 2.5),(min-device-pixel-ratio: 2.5), (min-resolution: 240dpi),(min-resolution: 2.5dppx)
      width: 300%;
      height: 300%;
      transform: scale(.33333)
      if $radius != null {
        border-radius: $radius * 3;
      }

    transform-origin: 0 0;

 

sass

使用方法

@include border(1px 0 0 0,color,solid,2px)

參數1:邊框寬度

參數2:顏色(可選)

參數3:樣式(可選)默認solid

參數4:圓角(可選)默認0px

@mixin border($border-width: 1px, $border-color: map-get($base, border-color), $border-style: solid, $radius: null) {
    // 為邊框位置提供定位參考
    position: relative;
    @if $border-width == null {
        $border-width: 0;
    }
    border-radius: $radius;
    &::after {
        // 用以解決邊框layer遮蓋內容
        pointer-events: none;
        position: absolute;
        z-index: 999;
        top: 0;
        left: 0;
        // fix當元素寬度出現小數時,邊框可能顯示不全的問題
        // overflow: hidden;
        content: "\0020";
        border-color: $border-color;
        border-style: $border-style;
        border-width: $border-width;
        // 適配dpr進行縮放
        @include responsive(retina1x) {
            width: 100%;
            height: 100%;
            @if $radius != null {
                border-radius: $radius;
            }
        }
        @include responsive(retina2x) {
            width: 200%;
            height: 200%;
            @include transform(scale(.5));
            @if $radius != null {
                border-radius: $radius * 2;
            }
        }
        @include responsive(retina3x) {
            width: 300%;
            height: 300%;
            @include transform(scale(.33333));
            @if $radius != null {
                border-radius: $radius * 3;
            }
        }
        @include transform-origin(0 0);
    }
}

  style-component

使用方法

const 組件名 = border({

component:styled.div``,

color:'red',(可選)默認#ccc

width:'1px 0 0 0',(可選)默認1px

style:‘solid’(可選)默認solid

radius:'2px' (可選) 默認0px

})

import styled from 'styled-components'

const border = ({
    component=null, 
    width='1px',
    style='solid',
    color='#ccc',
    radius=0
}) => {
  return styled(component) `
    position: relative;
    border-width: ${ width };
    border-radius: ${ radius + 'px' };
    &::after {
      pointer-events: none;
      position: absolute;
      z-index: 999;
      top: 0;
      left: 0;
      content: "";
      border-color: ${ color };
      border-style: ${ style };
      border-width: ${ width };

      @media
        (max--moz-device-pixel-ratio: 1.49), 
        (-webkit-max-device-pixel-ratio: 1.49), 
        (max-device-pixel-ratio: 1.49), 
        (max-resolution: 143dpi), 
        (max-resolution: 1.49dppx) {
          width: 100%;
          height: 100%;
          border-radius: ${ radius + 'px' };
        };
      
      @media 
        (min--moz-device-pixel-ratio: 1.5) and (max--moz-device-pixel-ratio: 2.49), 
        (-webkit-min-device-pixel-ratio: 1.5) and (-webkit-max-device-pixel-ratio: 2.49),
        (min-device-pixel-ratio: 1.5) and (max-device-pixel-ratio: 2.49),
        (min-resolution: 144dpi) and (max-resolution: 239dpi),
        (min-resolution: 1.5dppx) and (max-resolution: 2.49dppx) {
          width: 200%;
          height: 200%;
          transform: scale(.5);
          border-radius: ${ radius * 2 + 'px'};
        };
      
      @media 
        (min--moz-device-pixel-ratio: 2.5), 
        (-webkit-min-device-pixel-ratio: 2.5),
        (min-device-pixel-ratio: 2.5), 
        (min-resolution: 240dpi),
        (min-resolution: 2.5dppx) {
          width: 300%;
          height: 300%;
          transform: scale(.33333);
          border-radius: ${ radius * 3 + 'px' }
        };

        transform-origin: 0 0;
    };
  `
}

export default border

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM