设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 服务器 > 系统 > 正文

利用CSS的Sass预处理器(框架)来制作居中效果(3)

发布时间:2020-03-17 09:56 所属栏目:52 来源:站长网
导读:在这里让我们暂停一下,深入分析后续逻辑的层次: widthheightsolution null null translate defined defined margin defined null margin-left + translateY null defined margin-right + translateX 秀代码: CSS

在这里让我们暂停一下,深入分析后续逻辑的层次:
width height solution
null   null   translate  
defined   defined   margin  
defined   null   margin-left + translateY  
null   defined   margin-right + translateX  

秀代码:

CSS Code复制内容到剪贴板

@mixin center($width: null, $height: null) {   

    position: absolute;   

    top: 50%;   

    left: 50%;   

  

    @if not $width and not $height {   

        // Go with `translate`   

    } @else if $width and $height {   

        // Go width `margin`   

    } @else if not $height {   

        // Go with `margin-left` and `translateY`   

    } @else {   

        // Go with `margin-top` and `translateX`   

    }   

}  

通过上面的代码,我们已经搭好了 mixin 的骨架,只需要再添加上具体的逻辑代码即可:

CSS Code复制内容到剪贴板

@mixin center($width: null, $height: null) {   

    position: absolute;   

    top: 50%;   

    left: 50%;   

  

    @if not $width and not $height {   

        transform: translate(-50%, -50%);   

    } @else if $width and $height {   

        width: $width;   

        height: $height;   

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读