分享好友 前端技术首页 频道列表

移动端1px线适配问题-------适配各种编译CSS工具 stylus sass styled-componet实现方法

css教程  2023-03-08 20:500

其实在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

 

亲自使用过超级好使

 

如果本文对您有帮助,请抬抬您的小手,点下右下角的推荐, ^-^,当然如果看了这篇博客对您有帮助是我最开心的事,毕竟赠人玫瑰,手有余香, ^-^,如果这篇博客没有帮助到您,那就只能说一声抱歉啦

 

 

 

 

 

  

查看更多关于【css教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
css实现弹出框 css弹出菜单
弹出框也是前端里面经常使用的一个应用场景了,最开始想到的是用js实现这个效果,看到codepen上面有用css实现的。其实就是用到了css里面的一个:target选择器+visibility属性。URL 带有后面跟有锚名称 #,指向文档内某个具体的元素。这个被链接的元素就是目标

0评论2023-03-08519

jfinal框架页面找不到相关css,js文件404
在JFinalConfig中添加配置: @Overridepublic void configHandler(Handlers handlers) {handlers.add(new ContextPathHandler());}页面中添加:base href="${CONTEXT_PATH}/"/之前页面找不到资源,把tomcat工程路径去掉了(path=""),这样暂时解决了问题推荐

0评论2023-03-08576

移动端CSS底部固定和fixed定位在ios上的bug
fixed定位在ios上的bugcss三栏布局假设我们页面的 HTML 结构是这样: div class="wrapper"div class="content"!-- 页面主体内容区域 --/divdiv class="footer"!-- 需要做到 Sticky Footer 效果的页脚 --/div/div方法1.:absolute通过绝对定位处理应该是常见的

0评论2023-03-08624

css实现图片翻转 css使图片旋转
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"html xmlns="http://www.w3.org/1999/xhtml"headmeta http-equiv="Content-Type" content="text/h

0评论2023-03-08577

CSS3中的px,em,rem,vh,vw辨析 css3 vw
1、px:像素,精确显示2、em:继承父类字体的大小,相当于“倍”,如:浏览器默认字体大小为16px=1em,始终按照div继承来的字体大小显示,进场用于移动端          em换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html3、rem:与em类似,

0评论2023-03-08902

gulp自动化构建工具--自动编译less为css--学习笔记
 1.安装      命令:npm install gulp-less 或者 cnpm install gulp-less2.编写文件 //获取gulpvar gulp = require('gulp')//获取gulp-less模块var less = require("gulp-less")//编译less//在命令行输入gulp less启动此任务gulp.task('less',function(){

0评论2023-03-08411

关于动画Animate.css的使用方法
    首先贴个官网:https://daneden.github.io/animate.css/  1、引入animate css文件 1 head2 link rel="stylesheet" href="animate.min.css"3 /head   2、给指定的元素加上指定的动画样式名div class="animated bounceOutLeft"/div    这里包

0评论2023-03-08673

selenium Firefox禁用js,css.falsh,缓存,设置请求头和代理ip
1 profile = FirefoxProfile() 2 #请求头 3 #profile.set_preference("general.useragent.override", util.http_agent_insert(xinhuaUtil.agent_path)) 4 # 激活手动代理配置(对应着在 profile(配置文件)中设置首选项) 5 profile.set_preference("network

0评论2023-03-08777

CSS--容易混淆的.class1.class2与.class1 .class2的区别
容易混淆的.class1.class2与.class1 .class2的区别:第一个中间没空格的,匹配的是同时拥有class1和class2的元素。第二个中间有空格的,匹配的是父节点是class1类,子节点是class2类的元素。

0评论2023-03-08554

页面css样式找不到问题 页面css样式找不到问题怎么解决
出现了一个页面没有样式的问题:问题:  1.路径不对, 可以打开页面f12看样式是否找到  检查路径是否正确。  2.样式没引全或者没引对。查看引入的样式是否正确或缺少样式。  3.路径明明写对了却404找不到。可以试一试把${pageContext.request.context

0评论2023-03-08414

学习前端页面css定位 网页前端设计css
一、相对定位:absolute  相对定位是一个非常容易掌握的概念。如果对一个元素进行相对定位,它将出现在它所在的位置上。然后,可以通过设置垂直或水平位置,让这个元素“相对于”它的起点进行移动。position:relative;同时可以设置上下左右位置偏移;rela

0评论2023-03-08782

css-移动端模拟hover效果 css移动端适配
:hover 在移动端中,点击后一直处于hover状态,不会切换。为此解决:通过:active模拟!-- * @Author: lingxie * @Date: 2020-04-23 13:35:57 * @Descripttion: -- templatediv class="about"h1This is an about page/h1div class="btn-hole"按钮/div/div/templ

0评论2023-03-08409

css固定定位,不随滚动条滚动 css固定在底部
!DOCTYPE htmlhtml lang="en"headmeta charset="UTF-8"meta http-equiv="X-UA-Compatible" content="IE=edge"meta name="viewport" content="width=device-width, initial-scale=1.0"titleDocument/titlestyleb

0评论2023-03-08745

(转)JAVA中解决Filter过滤掉css,js,图片文件等问题
在加入过滤器时,会出现网页可以加载,但是网页中的js,css等文件无法加载的问题。因为在网页加载时,先将整个网页加载,然后加载网页中的js,css等文件,而在进行js,css文件加载时,会被过滤器过滤掉。因此在过滤器中加入一个对css,js等文件的判断,就可以

0评论2023-03-08794

CSS3 Flexbox轻松实现元素的水平居中和垂直居中
CSS3 Flexbox轻松实现元素的水平居中和垂直居中网上有很多关于Flex的教程,对于Flex的叫法也不一,有的叫Flexbox,有的叫Flex,其实这两种叫法都没有错,只是Flexbox旧一点,而Flex是刚出来不久的东西而已,为了方便说明,赶上新技术,下面我就把这种布局叫Fl

0评论2023-03-08774

css3多行文本溢出显示省略号… css3行超出省略号
.title {width: rem(210);overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;}

0评论2023-03-08638

更多推荐