移动端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 ...
随机推荐
- eclipse中jsp页面乱码问题
若上述位置均改为utf-8之后,页面展示扔为乱码,检查jsp页面是否有编码说明
- BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论
BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论 Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作 ...
- Cortex-M3 / M4 Hard Fault Handler (转载)
转自大伟的,感谢大伟的帮助调试:http://www.cnblogs.com/shangdawei/archive/2013/04/30/3052491.html http://blog.frankv ...
- 值得网页设计师&前端收藏的实用工具列表
原文地址:http://www.uisdc.com/tool-list-web-developers# 无论你是经验丰富的前端,还是刚刚起步的设计师,这些为真正的网页设计师和开发者所准备的实用工具.在 ...
- 代码中特殊的注释技术——TODO、FIXME和XXX的用处 (转载)
转自:http://blog.csdn.net/reille/article/details/7161942 作者:reille 本博客网址:http://blog.csdn.net/reille/, ...
- HDU3949:XOR(高斯消元)(线性基)
传送门 题意 给出n个数,任意个数任意数异或构成一个集合,询问第k大个数 分析 这题需要用到线性基,下面是一些资料 1.高斯消元&线性基&Matirx_Tree定理 笔记 2.关于线性 ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
- ubuntu vim设置显示行号
打开vim的配置文件 /etc/vim/vimrc sudo vim /etc/vim/vimrc 然后找到 #set number ,把注释取消就行了 如果没有,就自己加一行
- Vim 插件的安装
Vim 自带了文本格式化,通过 gg=G 触发.但大数情况下不满足需求,对于特定语言,比如 JavaScript,需要安装相应的插件来实现. 插件的存在形式 Vim 插件以三种形式存在, 单个的 .v ...
- Ubuntu 必装软件及安装教程
安装搜狗拼音输入法 因为sogou是基于fcitx的,所以先添加fcitx键盘输入法系统[系统默认是iBus].在终端中,输入命令将下载源添加至系统源(添加依赖). sudo add-apt-repo ...