Sass實現顏色卡的製作

效果圖:


實現代碼:

colorCard.html:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>sass實現顏色卡</title>
		<link href="colorCard.css" rel="stylesheet" />
	</head>
	<body>
		<script type="text/javascript">
			var list = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'black'];
			var html = '';
			for(var i = 0; i < list.length; i++){
				var ul = document.createElement('ul');
				ul.className = 'swatched '+list[i];
				for(var j = 0; j < 20; j++){
					var li = document.createElement('li');
					ul.appendChild(li);
				}
				document.body.appendChild(ul);
			}
		</script>
	</body>
</html>

colorCard.scss(最後要編譯成colorCard.css文件,這裏就不具體寫了)

@charset "UTF-8";
$red: #BD143c;
$orange: saturate(lighten(adjust_hue($red, 39), 5), 7);
$yellow: saturate(lighten(adjust_hue($red, 64), 6), 13);
$green: saturate(lighten(adjust_hue($red, 102), 2), 11);
$blue: saturate(lighten(adjust_hue($red, 201), 2), 1);
$purple: saturate(lighten(adjust_hue($red, 296), 2), 1);
$black: #777;
$bgc: #fff;
@mixin swatchesDarken($color) {
    @for $i from 1 through 10 {
        $x: $i + 10;
        li:nth-child(#{$x}) {
            $n: $i * 5;
            $bgc: darken($color, $n);
            background-color: $bgc;
            &:hover:before {
                content: '#{$bgc}';
                color: lighten($bgc, 40);
                font-family: verdana;
                font-size: 8px;
                padding: 2px;
            }
        }
    }
}
@mixin swatchesLighten($color) {
    @for $i from 1 through 10 {
        $calColor: $color;
        @debug $calColor;
        $x: 11-$i;
        li:nth-child(#{$x}) {
            $n: $i * 5;
            $bgc: lighten($calColor, $n);
            background-color: $bgc;
            &:hover:before {
                content: '#{$bgc}';
                color: darken($bgc, 40);
                font-family: verdana;
                font-size: 8px;
                padding: 2px;
            }
        }
    }
}
$map: (red:$red, orange:$orange, yellow:$yellow, green:$green, blue:$blue, purple:$purple, black:$black);
.swatched {
    display: block;
    &:after {
        content: "";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
    li {
        width: 4.7619%;
        height: 60px;
        float: left;
        list-style: none outside none;
    }
}
@each $la,
$co in $map {
    ul.#{$la} {
        @debug #{$co};
        @include swatchesLighten($co);
        @include swatchesDarken($co);
    }
}



發佈了44 篇原創文章 · 獲贊 68 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章