css变量兼容处理,使用CSS4变量和保持后向兼容

CSS

语言:

CSSSCSS

确定

$css4: true;

$compatibility: true;

//it is nessesary to define the variables in sass map instead of :root, for compatibility reasons.

$variables: (

--color: white,

--background: skyblue,

--font: sans-serif

);

//Here we transfer the variables from the map to the :root element

@if($css4) {

:root {

@each $variable, $value in $variables {

#{$variable}: $value;

}

}

}

//this is the "magic" function

@function var($variable) {

@if($css4) {

@return unquote('var(' + $variable + ')');

} @else {

@return map-get($variables, $variable);

}

}

//the mixin temporally sets the $css4 variable to false, compiles the css3 fallback, then makes the variable true again and compiles the css4 code. It should contain properties that use css4 variables, otherwise there will be unnessesary duplication of properties.

@mixin css4 {

@if ($css4) {

$css4-backup: $css4;

@if($compatibility) {

$css4: false !global;

@content;

}

$css4: true !global;

@content;

$css4: $css4-backup;

}

@else {

@content;

}

}

//the body does not use fallback, its styles will be visible only in compatible browsers

body {

max-width: 500px;

margin: 0 auto;

padding: 30px;

color: var(--color);

background: var(--background);

font: {

family: var(--font);

};

}

// the p and button tags uses the css4 mixin so a fall-back is provided and they will work on all browsers.

p {

text-align: justify;

@include css4 {

color: var(--color);

}

}

button {

@include css4 {

background: var(--color);

color: var(--background);

border: 2px solid currentcolor;

box-shadow: 0 0 0 5px var(--color);

};

&:hover {

@include css4 {

background: var(--background);

color: var(--color);

box-shadow: 0 0 0 5px var(--background);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值