移动端1px线适配问题-------适配各种编译CSS工具 stylus sass styled-componet实现方法
其实在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 = )
// 为边框位置提供定位参考
position: relative; if $border-width == null
$border-width: ; border-radius: $radius; &::after
// 用以解决边框layer遮盖内容
pointer-events: none;
position: absolute;
z-index: ;
top: ;
left: ;
// 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: .49dppx)
width: %;
height: %;
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: .5dppx) and (max-resolution: .49dppx)
width: %;
height: %;
transform: scale(.)
if $radius != null {
border-radius: $radius * ;
} @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: .5dppx)
width: %;
height: %;
transform: scale(.)
if $radius != null {
border-radius: $radius * ;
} transform-origin: ;
在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: ;
}
border-radius: $radius;
&::after {
// 用以解决边框layer遮盖内容
pointer-events: none;
position: absolute;
z-index: ;
top: ;
left: ;
// fix当元素宽度出现小数时,边框可能显示不全的问题
// overflow: hidden;
content: "\0020";
border-color: $border-color;
border-style: $border-style;
border-width: $border-width;
// 适配dpr进行缩放
@include responsive(retina1x) {
width: %;
height: %;
@if $radius != null {
border-radius: $radius;
}
}
@include responsive(retina2x) {
width: %;
height: %;
@include transform(scale(.));
@if $radius != null {
border-radius: $radius * ;
}
}
@include responsive(retina3x) {
width: %;
height: %;
@include transform(scale(.));
@if $radius != null {
border-radius: $radius * ;
}
}
@include transform-origin( );
}
}
在component中
import styled from 'styled-components' const border = ({
component = null,
width = '1px',
style = 'solid',
color = '#ccc',
radius = ,
}) => {
return styled(component) `
position: relative;
border-width: ${ width};
border-radius: ${ radius + 'px'};
&::after {
pointer-events: none;
position: absolute;
z-index: ;
top: ;
left: ;
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: .49dppx) {
width: %;
height: %;
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: .5dppx) and (max-resolution: .49dppx) {
width: %;
height: %;
transform: scale(.);
border-radius: ${ radius * + '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: .5dppx) {
width: %;
height: %;
transform: scale(.);
border-radius: ${ radius * + 'px'}
}; transform-origin: ;
};
`
} export default border
亲自使用过超级好使
如果本文对您有帮助,请抬抬您的小手,点下右下角的推荐, ^-^,当然如果看了这篇博客对您有帮助是我最开心的事,毕竟赠人玫瑰,手有余香, ^-^,如果这篇博客没有帮助到您,那就只能说一声抱歉啦
移动端1px线适配问题-------适配各种编译CSS工具 stylus sass styled-componet实现方法的更多相关文章
- CSS3 移动端 1PX 线变成0.5PX
.line1 {position:relative} .line1:after {content:'';position:absolute;bottom:0;left:0;width:100%;hei ...
- 目前解决移动端1px边框最好的方法
在移动端开发时,经常会遇到在视网膜屏幕中元素边框变粗的问题.本文将带你探讨边框变粗问题的产生原因及介绍目前市面上最好的解决方法. 1px 边框问题的由来 苹果 iPhone4 首次提出了 Retina ...
- 移动端1px细线解决方案总结
现在的PM和UI总以看app的眼光看html5, html页面要做的专业美观,而且必须很精细. 去年的时候UI就告诉我h5上的边框线太粗,把整站都给拉low了. 当时工期紧就没太在意1px粗细, 好在 ...
- 移动端1px边框伪类宽高计算
移动端1px边框在手机上看显得比较粗,于是我们用伪类结合css3缩放的方法去设置线条,但是如果设置div的一条边,水平线就设置宽度100%,垂直线就设置高度100%,那么如果是div的四条边呢?宽高1 ...
- 移动端1px边框
问题:移动端1px边框,看起来总是2倍的边框大小,为了解决这个问题试用过很多方法,用图片,用js判断dpr等,都不太满意, 最后找到一个还算好用的方法:伪类 + transform 原理是把原先元素的 ...
- 老项目的#iPhone6与iPhone6Plus适配#LaunchImage适配
本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4020384.html,转载请注明出处. Evernote印象笔记链接:https://www.everno ...
- 老项目的#iPhone6与iPhone6Plus适配#Icon适配
本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020373.html ,转载请注明出处. 这是Evernote印象笔记的链接:https://www ...
- 移动端1px问题处理方法
在做移动端开发时,设计师提供的视觉稿一般是750px,当你定义 border-width:1px 时,在iphone6手机上却发现:边框变粗了.. 这是因为,1px是相对于750px的(物理像素),而 ...
- 移动端1px细线解决方案--利用transform缩放方式
移动端1px会显示为2px; 解决方式很多,这里介绍比较常用的一种方式--css的transform属性缩放 1. 上边框 相当于 border-top <div class="bor ...
随机推荐
- nginx性能优化技巧
前几天买了本高俊峰的<高性能Linux服务器构建实战I>,网上都说运维必备手册,昨天看了目录加小50页感觉还是比较扩充视野的,很多东西在学校是不可能学到的,就是感觉有的地方讲的仍然不是很清 ...
- Ural2102:Michael and Cryptography(数论&素数)
The hacker Michael develops breakthrough password manager, which is called KEK (Keeper of Encrypted ...
- hdu 5289(单调队列)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- bzoj 4596
4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 257 Solved: 152[Submit][Sta ...
- 洛谷 P2774 方格取数问题【最小割】
因为都是正整数,所以当然取得越多越好.先把所有点权加起来,黑白染色后,s向所有黑点连流量为点权的边,所有白点向t连流量为点权的边,然后黑点向相邻的四个白点连流量为inf的边,表示不可割,这样一来,对于 ...
- 51nod 1222 最小公倍数计数【莫比乌斯反演】
参考:https://www.cnblogs.com/SilverNebula/p/7045199.html 所是反演其实反演作用不大,又是一道做起来感觉诡异的题 转成前缀和相减的形式 \[ \sum ...
- 报错Cannot determine embedded database driver class for database type NONE解决方法
由于我不需要数据库,启动springboot报错: Cannot determine embedded database driver class for database type NONE If ...
- centos7静态网络配置
centos7静态网络配置 cd /etc/sysconfig/network-scripts 找到当前网卡名字 vim ifcfg-ens33 TYPE="Ethernet" # ...
- [POI2007]大都市meg
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- LightOj 1197 Help Hanzo (区间素数筛选)
题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围 ...