Susy 2 教程 — 实战篇

栏目: CSS · 发布时间: 6年前

内容简介:Susy 2 教程 — 实战篇
Susy 2 教程 — 实战篇

Susy 2 教程 — 入门篇

Susy 2 教程 — Shorhand 篇

在前面介绍了Susy2的配置(config)和简写(shorthand)之后,给大家介绍一下Tookit中几个常用的宏, 然后动手做一个 Bootstrap 栅格系统

Tookit

Susy 2Tookit 是围绕我们的简写语法( shorthand )构建的。通过 shorthand 调整初始配置值来控制每一个细节,让你不会被束缚在一套栅格系统上。

常用Tookit

Span [mixin]

span 是一个高频度的混合宏,可以灵活设定元素的栅格位置。

  • 语法格式: span($span) { @content }
  • $span: shorthand <span>
  • @content: Sass content block
//input scss

$susy: (
  container:1000px,
  column-width:100px,
  math:static,
  columns: 10,  // The number of columns in your grid
  gutters: .25, // The size of a gutter in relation to a single column
  gutter-position:split
);

.col-left{
    @include span(1);
}

.col-right{
  @include span(2 last);
}

.col-half {
  @include span(50%);
}

// ======= output css =========

.col-left {
  width: 100px;
  float: left;
  margin-left: 12.5px;
  margin-right: 12.5px;
}

.col-right {
  width: 225px;
  float: right;
  margin-left: 12.5px;
  margin-right: 12.5px;
}

.col-half {
  width: 50%;
  float: left;
  margin-left: 12.5px;
  margin-right: 12.5px;
}

susy-media [mixin]

susy-media 提供了基本的媒体查询,并内置在 susy-breakpoint 混合宏中。

  • 语法格式: susy-media($query, $no-query)
  • $query: <min-width> [<max-width>] | <string> | <pair> | <map>
  • $no-query: <boolean> | <string>
// input scss
@include susy-media(30em) { 
  background-color: #FFF;
}
// output css
@media (min-width: 30em) {
  background-color: #FFF;
}

// input scss
@include susy-media(30em 60em) { /*...*/ }
// output css
@media (min-width: 30em) and (max-width: 60em) { /*...*/ }


// input scss
@include susy-media(min-height 30em) { /*...*/ }
// output css
@media (min-height: 30em) { /*...*/ }

susy-breakpoint [mixin]

susy-breakpoint 为混合宏 susy-media 或者第三方 breakpoint 插件提供不同媒体断点查询。

  • 语法格式: susy-breakpoint($query, $layout, $no-query)
  • $query: media query shorthand
  • $layout: shorthand <layout>
  • $no-query: <boolean> | <string>
// input scss
$susy: (
  container:1000px,
  column-width:100px,
  math:static,
  columns: 10,  // The number of columns in your grid
  gutters: .25, // The size of a gutter in relation to a single column
  gutter-position:split
);

@include susy-breakpoint(30em, 8) {
  // nested code uses an 8-column grid,
  // starting at a 30em min-width breakpoint...
  .example { @include span(3); }
}

// output css

@media (min-width: 30em) {
  .example {
    width: 350px;
    float: left;
    margin-left: 12.5px;
    margin-right: 12.5px;
  }
}

更多的Tookit请查看官方文档 http://susydocs.oddbird.net/en/latest/toolkit/

创建一个 Bootstrap 栅格系统

超小屏幕 手机 (<768px)<small=""> 小屏幕 平板 (≥768px) 中等屏幕 桌面显示器 (≥992px) 大屏幕 大桌面显示器 (≥1200px)
栅格系统行为 总是水平排列 开始是堆叠在一起的,当大于这些阈值时将变为水平排列C
.container 最大宽度 None (自动) 750px 970px 1170px
类前缀 .col-xs- .col-sm- .col-md- .col-lg-
列(column)数 12
最大列(column)宽 自动 ~62px ~81px ~97px
槽(gutter)宽 30px (每列左右均有 15px)
可嵌套
偏移(Offsets)
列排序

全局配置

$susy:(
  debug:(image:show), // 开启debug
  columns:12,
  gutters: 0, 
  gutter-position: inside,
);

小于 768px 的适配

// 展示容器元素
.container-xs{
  height: 200px;
  @include container;
  // 大于0 , 小于768px
  @include susy-breakpoint(0 768px){
    @include container;
  }
}

[class^=col-xs]{
  // PS:由于susy2的gutters只能设置百分比,不能给绝对值,因而选择另一种方式
  // 当class前面有col-xs的文字时,则自动加上padding左右的设定。
  padding-left: 15px;
  padding-right: 15px;

  background-color:rgba(#CFFFA3,.8);
  border-right:3px #F94B22 solid;
  text-indent:2em;
  height:40px;
}

// col-xs 版
@for $i from 1 through 12 {
    .col-xs-#{$i}{
      @include span($i)
    }
}

大于等于768px 的适配

// col-sm 版 : >= 768px
// 这里列宽为什么是32.5?而不是62呢?
// 道理很简单,因为 gutter-position:inside,会给元素设置 box-sizing:border-box
// layout shorthand : 
// 12grids
// 32.5px columnWidth
// 30px gutterWidth
$SmallDevice:(12 (32.5px 30px) inside); 
$breakSmallDevice: 768px;  //设定断点

[class^=col-sm]{

  background-color:rgba(#F959EB,.8);
  border-right:3px #F94B22 solid;
  text-indent:2em;
  height:40px;
}

// 展示容器元素
.container{
  height: 200px;
  @include container;
  @include susy-breakpoint($breakSmallDevice,$SmallDevice){
    @include container;
  }
}


@include susy-breakpoint($breakSmallDevice){
  @for $i from 1 through 12 {
    @include with-layout($SmallDevice){  
      .col-sm-#{$i}{
        @include span($i);
      }
    }  
  }
}

// 以此类推,≥992px,≥1200px都同样可以实现。

效果图

Susy 2 教程 — 实战篇

通过这个例子,我们可以发现,制作一个栅格系统,用Susy2,只需要用到 span , container , susy-breakpoint , width-layout 就可以轻松制作出来。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Node.js开发指南

Node.js开发指南

郭家寶(BYVoid) / 人民邮电出版社 / 2012-7 / 45.00元

Node.js是一种方兴未艾的新技术,诞生于2009年。经过两年的快速变化,Node.js生态圈已经逐渐走向稳定。Node.js采用了以往类似语言和框架中非常罕见的技术,总结为关键词就是:非阻塞式控制流、异步I/O、单线程消息循环。不少开发者在入门时总要经历一个痛苦的思维转变过程,给学习带来巨大的障碍。 而本书的目的就是帮助读者扫清这些障碍,学会使用Node.js进行Web后端开发,同时掌握事件驱......一起来看看 《Node.js开发指南》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换