移动端 1px 问题

  • 移动端(手机等)已成为人们最常用的设备,为了让用户用得爽,看得服务是不够的,所以有时候你还得丝毫不差的把设计稿给搬到网页上。但是目前市面上的手机屏幕大致分为一倍屏、二倍屏和三倍屏。这就导致了明明是 1px 的线条到手机上显示就是 2px 、3px 了。这个时候,设计书就会跟你讨论这样一个问题:为什么这条线看起来比我原型稿的粗一些,这也太粗了吧?诸如此类的话,让你连反驳的机会都没有... 好了,废话不多说,直接上答案了

方案一:直接来个 1px 的图片做背景图

  • 优点:简单 方便 快捷 明了
  • 缺点:换个颜色什么的就只能 g g 了

方案二:使用伪类 :after 、定位 position 、 缩放 transform:scale 来实现

  • 优点:颜色 高度随便定

  • 缺点:代码说多也不多,说少也不少

  • 使用 border-bottom 实现

    	.border-buttom{
    	    position: relative;
    	}
    
    	.border-buttom:after {
    	    border-bottom: 1px solid #bfbfbf;
    	    content: ' ';
    	    display: block;
    	    width: 100%;
    	    position: absolute;
    	    left: 0;
    	    bottom: 0;
    	    -webkit-transform-origin: left bottom;
    	}
    
    
    	/* Retina 适配 */
    	@media only screen and (-webkit-min-device-pixel-ratio: 2.0),
    	only screen and (min--moz-device-pixel-ratio: 2.0),
    	only screen and (-o-min-device-pixel-ratio: 200/100),
    	only screen and (min-device-pixel-ratio: 2.0) {
    	    .border-buttom:after {
    	        -webkit-transform: scaleY(0.5);
    	        transform: scaleY(0.5);
    	    }
    	}
    
    
    	/* 三倍屏 适配 */
    	@media only screen and (-webkit-min-device-pixel-ratio: 2.5),
    	only screen and (min--moz-device-pixel-ratio: 2.5),
    	only screen and (-o-min-device-pixel-ratio: 250/100),
    	only screen and (min-device-pixel-ratio: 2.5) {
    	    .border-buttom:after{
    	        -webkit-transform: scaleY(0.33333334);
    	        transform: scaleY(0.33333334);
    	    }
    	}
    
  • 使用 background 实现

    	.border-buttom{
    	    position: relative;
    	}
    
    	.border-buttom:after {
    		position: absolute;
    		right: 0;
    		bottom: 0;
    		left: 0;
    		height: 1px;
    		content: '';
    		background-color: #bfbfbf;
    	}
    
    
    	/* Retina 适配 */
    	@media only screen and (-webkit-min-device-pixel-ratio: 2.0),
    	only screen and (min--moz-device-pixel-ratio: 2.0),
    	only screen and (-o-min-device-pixel-ratio: 200/100),
    	only screen and (min-device-pixel-ratio: 2.0) {
    	    .border-buttom:after {
    	        -webkit-transform: scaleY(0.5);
    	        transform: scaleY(0.5);
    	    }
    	}
    
    
    	/* 三倍屏 适配 */
    	@media only screen and (-webkit-min-device-pixel-ratio: 2.5),
    	only screen and (min--moz-device-pixel-ratio: 2.5),
    	only screen and (-o-min-device-pixel-ratio: 250/100),
    	only screen and (min-device-pixel-ratio: 2.5) {
    	    .border-buttom:after{
    	        -webkit-transform: scaleY(0.33333334);
    	        transform: scaleY(0.33333334);
    	    }
    	}
    

方案三:通过 js 获取设备像素比,动态添加 <meta name="viewport"> 标签

  • 优点:页面渲染过程中会自动根据屏幕显示
  • 缺点:设置了缩放导致其他元素跟着缩放
<script type="text/javascript">
   (function() {
       var scale = 1.0;
       if (window.devicePixelRatio === 2) {
           scale *= 0.5;
       }
       if (window.devicePixelRatio === 3) {
           scale *= 0.333333;
       }
       var text = '<meta name="viewport" content="initial-scale=' + scale + ', maximum-scale=' + scale +', minimum-scale=' + scale + ', width=device-width, user-scalable=no" />';
       document.write(text);       
    })();
</script>

转载于:https://my.oschina.net/mayxu/blog/1593495

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值