摘要:问题描述为了适配各种屏幕,我们写代码时一般使用设备独立像素来对页面进行布局。对此有如下解决方案这种方式可以满足各种场景。
问题描述:
为了适配各种屏幕,我们写代码时一般使用设备独立像素来对页面进行布局。
而在设备像素比大于 1的屏幕上,我们写的 1px实际上是被多个物理像素渲染,这就会出现 1px在有些屏幕上看起来很粗的现象。
对此有如下解决方案:
@mixin border-1px($color, $direction) {
position: relative;
&::before {
content: "";
position: absolute;
z-index: 1;
@if $direction==all {
left: 0;
top: 0;
border: 1px solid $color;
box-sizing: border-box;
transform-origin: left top;
}
@else {
background-color: $color;
@if $direction==top {
left: 0;
top: 0;
width: 100%;
height: 1px;
}
@if $direction==right {
right: 0;
top: 0;
width: 1px;
height: 100%;
}
@if $direction==bottom {
left: 0;
bottom: 0;
width: 100%;
height: 1px;
}
@if $direction==left {
left: 0;
top: 0;
width: 1px;
height: 100%;
}
}
}
}
@each $direction in all,
top,
right,
bottom,
left {
@for $i from 1 to 10 {
$scale: 1 / $i;
@media only screen and (-webkit-min-device-pixel-ratio:$i) {
.border-1px-#{$direction}::before {
@if $direction==all {
width: $i * 100%;
height: $i * 100%;
transform: scale($scale);
}
@if $direction==top {
transform: scaleY($scale);
}
@if $direction==bottom {
transform: scaleY($scale);
}
@if $direction==right {
transform: scaleX($scale);
}
@if $direction==left {
transform: scaleX($scale);
}
}
}
}
}
这种方式可以满足各种场景。
使用方式:
第一步:@include border-1px(blue, all);引入由mixin定义的代码块(创建伪类 模拟border)
第二步:
*注:
为何没有直接对border-width直接操作 是因为有部分机型不支持0.5px这样的值 会被当成0来处理 故用缩放来实现。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/114940.html
问题提出 这是一个比较老的问题了,我第一次注意到的时候,是UI设计师来找我麻烦,emmm那时候我才初入前端职场,啥也不懂啊啊啊啊啊,情形是这样的:设计师拿着手机过来:这些边框都粗了啊,我的设计稿上是1px的我:????我写的是1px呀,不信你看。(说着拿出了css代码设计师:不对啊我眼睛看来这个边框比我设计稿上粗很多,看起来好奇怪(emmm果然像素眼我:那我把它改成0.5px你看看(边说边改了代码...
问题提出 这是一个比较老的问题了,我第一次注意到的时候,是UI设计师来找我麻烦,emmm那时候我才初入前端职场,啥也不懂啊啊啊啊啊,情形是这样的:设计师拿着手机过来:这些边框都粗了啊,我的设计稿上是1px的我:????我写的是1px呀,不信你看。(说着拿出了css代码设计师:不对啊我眼睛看来这个边框比我设计稿上粗很多,看起来好奇怪(emmm果然像素眼我:那我把它改成0.5px你看看(边说边改了代码...
阅读 1183·2023-04-25 22:13
阅读 2527·2019-08-30 15:56
阅读 2385·2019-08-30 11:21
阅读 885·2019-08-30 11:13
阅读 2240·2019-08-26 14:06
阅读 2198·2019-08-26 12:11
阅读 2467·2019-08-23 16:55
阅读 713·2019-08-23 15:30