首页 > 技术文章 > SASS的安装及使用(前提:安装Ruby)

bettyling 2016-11-21 13:46 原文

本文仅适用于Windows系统。

 

一、安装Ruby

  Sass是用Ruby语言写的,但是两者的语法没有关系,所以学 Sass 不用学 Ruby,只是必须先安装Ruby,然后再安装Sass。

  Linux和Mac已自带Ruby,不用再安装。Windows用户可以从这里下载Ruby的安装程序。

  我下载的是第二个,Ruby 2.3.1 (x64)

  安装过程没什么麻烦的地方,按提示来就可以。

  

 

二、安装SASS

  进入运行cmd进入命令提示符,输入ruby -v查看版本号,如果能正确显示版本号,则说明ruby安装成功。

  
  然后输入gem install sass进行安装。

  可能会出现下图(提示Could not find a valid gem 'sass')的情况,可以参考这篇文章解决。

  

  正常情况下显示的结果是这样的:

  

  

三、使用SASS

  要编译的scss文件(mystyle.scss)如下:

$anime-time: 8s;

$box-size: 350px;
$clip-distance: .05;
$clip-size: $box-size * (1 + $clip-distance * 2);
$path-width: 2px;

$main-color: #5FB9D5;

$codepen-logo-path: url('//blog.codepen.io/wp-content/uploads/2012/06/Button-White-Large.png');

%full-fill {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.bb {
  @extend %full-fill;
  width: $box-size;
  height: $box-size;
  margin: auto;
  background: $codepen-logo-path no-repeat 50% / 70% rgba(#000, .1);
  color: $main-color;
  box-shadow: inset 0 0 0 1px rgba($main-color, .5);

  &::before,
  &::after {
    @extend %full-fill;
    content: '';
    z-index: -1;
    margin: -1 * $clip-distance * 100%;
    box-shadow: inset 0 0 0 $path-width; 
    animation: clipMe $anime-time linear infinite;
  }

  &::before {
    animation-delay: $anime-time * -.5;
  }

  // for debug
  &:hover {
    &::after,
    &::before {
      background-color: rgba(#f00, .3);
    }
  }

}

@keyframes clipMe {
  0%, 100% {clip: rect(0px, $clip-size, $path-width, 0px); }
  25% {clip: rect(0px, $path-width, $clip-size, 0px); }
  50% {clip: rect($clip-size - $path-width, $clip-size, $clip-size, 0px); }
  75% {clip: rect(0px, $clip-size, $clip-size, $clip-size - $path-width); }
}

/////

html,
body {
  height: 100%;
}

body {
  position: relative;
  background-color: #0f222b;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

  进入需要编译的scss所在的目录,执行sass style.scss style.css

   

  之后可在该目录获得编译后的css文件以及css.map文件。

  

  scss文件相当于源文件,css相当于编译后的文件,当检查到页面问题的时候,看到的是css,但是需要修改的是scss文件,这个css.map就是两个文件的对应关系表。

  

 

参考资料:

http://www.sass-zh.com/

http://www.haorooms.com/post/sass_css

推荐阅读