其實在stylus與sass中實現移動端1像素線各個手機設備的適配問題的原理是一樣的,
首先我還是先介紹一下原理和所依賴的方法
原理:其實他們都是通過css3的媒體查詢來實現的
步驟思路:
1、給目標元素進行相對定位
2、給目標元素添加偽元素 ::after/before 並進行絕對定位
3、判斷DPI 1DPI 100% --------------使用媒體查詢
2DPI 200%
3DPI 300%
4、通過css3中的transform scale等比縮放
下面是具體的代碼,使用時只需引入代碼 在style里直接寫 border: 1px 0 0 0, #ccc 即可使用
在stylus中
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中
@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); } }
在component中
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
親自使用過超級好使
如果本文對您有幫助,請抬抬您的小手,點下右下角的推薦, ^-^,當然如果看了這篇博客對您有幫助是我最開心的事,畢竟贈人玫瑰,手有余香, ^-^,如果這篇博客沒有幫助到您,那就只能說一聲抱歉啦