2

我正在使用Susy

我未能利用内容优先的方法,并决定先使用 window-px-widths-first

起初我尝试了内容优先的网格方法,但很快我发现我的网站在不同的设备上表现出意外。它会显示一个移动布局,我想要一个平板电脑布局等。我最终调整了 Susy 设置的 em 值,以匹配某些屏幕宽度(px 值)。代码变得丑陋,我意识到我实际上不再使用内容优先的方法。

这是我使用这种错误方法创建的网站主页的静态快照及其代码的快照。

所以我决定完全放弃内容优先的方法,首先使用 px 值。

在编写代码之前,我为我的网格制定了要求

首先,我按屏幕尺寸对不同设备进行了分组。然后我想出了最适合这些设备组的断点的 px 值:

Break-    Layout   Number of            Common
points     name     columns             usage
(px)               (sample)

    0  ┬
       │
       │     S        1       Smartphones-portrait, old phones
       │
   400 ┼
       │     M        2       Smartphones-landscape
   600 ┼
       │     L        3       Tablets-portrait
   800 ┼
       │     XL       4       Tablets-landscape, netbooks
  1000 ┼
       │    XXL       5       Laptops, desktop computers
  1200 ┼
       ↓

我想以下假设/要求:

  1. Window-px-widths-first 方法(如上所述)。

  2. $container-style 是流动的。当屏幕宽度介于两个断点之间时,容器的宽度会自动调整以匹配较大的断点。布局中的列数不变,对应下断点。

  3. 最后一个断点是容器的最大宽度。该网站不会进一步延伸,而是会有额外的排水沟。

  4. 移动优先。从“S”布局开始,随着屏幕变宽,用其他布局覆盖它。

经过深思熟虑,我的方法演变为以下代码

(此代码是一个综合示例。我从我的实际代码中摘录并进行了一些修改,因此它可能会遗漏一些东西或有不一致的地方。)

<div id="header-wrapper">
  <header id="header">
    ...
  </header>
</div>

<div id="main-wrapper">
  <div id="main">

    <article id="content">...</article>
    <aside id="sidebar">...</aside>

  </div>
</div>

<div id="footer-wrapper">
  <footer id="footer">
    ...
  </footer>
</div>
/////////////
// Variables
/////////////

$development: true // This enables susy-grid-backgrounds and outlines

// Breakpoints
$bp-s-m:    400px
$bp-m-l:    600px
$bp-l-xl:   800px
$bp-xl-xxl: 1000px
$max-width: 1200px

// Columns
$cols-s:   1
$cols-m:   2
$cols-l:   3
$cols-xl:  4
$cols-xxl: 5

// Layouts
// $layout-s is not necessary due to a mobile-first approach
$layout-m:    $bp-s-m     $cols-m
$layout-l:    $bp-m-l     $cols-l
$layout-xl:   $bp-l-xl    $cols-xl
$layout-xxl:  $bp-xl-xxl  $cols-xxl

// Default grid settings
$total-columns:   $cols-s
$column-width:    85%
$gutter-width:    100% - $column-width
$grid-padding:    1em
$container-width: 100%
$container-style: fluid
+border-box-sizing


/////////////
// Mixins
/////////////

// A couple of mixins to open the developer's third eye
=dev-outline
  @if $development
    outline: 1px solid red
=dev-grid-bg
  +dev-outline
  @if $development
    +susy-grid-background

// A custom container declaration
=standard-container
  +container // ← An actual line of Susy code, yay! :D
             //  This whole post is actualy an attempt to make use of it.
  max-width: $max-width
  +dev-grid-bg

  +at-breakpoint($layout-m)
    +set-container-width
    +dev-grid-bg

  +at-breakpoint($layout-l)
    +set-container-width
    +dev-grid-bg

  +at-breakpoint($layout-xl)
    +set-container-width
    +dev-grid-bg

  +at-breakpoint($layout-xxl)
    +set-container-width
    +dev-grid-bg


/////////////
// Backgrounds
/////////////

// The wrapper divs are necessary for screen-wide backgrounds
html
  background: url('../images/main-background.png') // also repeat, color, this kind of stuff

#header-wrapper
  background: url('../images/header-background.png') 

#footer-wrapper
  background: url('../images/footer-background.png')


/////////////
// Containers
/////////////

// Actually declared in separate SASS files
#header, #main, #footer
  +my-container


/////////////
// Columns
/////////////

// An example of declaring columns
$cols-sidebar: 1
#sidebar-first
  +dev-outline
  +at-breakpoint($layout-l)
    +span-columns($cols-sidebar, $cols-l)

  +at-breakpoint($layout-xl)
    +span-columns($cols-sidebar, $cols-xl)

  +at-breakpoint($layout-xxl)
    +span-columns($cols-sidebar, $cols-xxl)
#content
  +dev-outline
  +at-breakpoint($layout-l)
    +span-columns($cols-l - $cols-sidebar omega, $cols-l)

  +at-breakpoint($layout-xl)
    +span-columns($cols-xl - $cols-sidebar omega, $cols-xl)

  +at-breakpoint($layout-xxl)
    +span-columns($cols-xxl - $cols-sidebar omega, $cols-xxl)

这是我使用这种方法创建的网站主页的静态快照及其代码的快照

问题

  1. 遵循 window-px-widths-first 方法是我深思熟虑的决定,并且是针对以下问题的。我很欣赏您关于内容优先的论点,但请不要局限于说我错了,请回答以下特定于 window-px-widths-first 的问题。

  2. 我的方法是先使用 Susy 执行 window-px-widths-first 的一种优雅方式,还是一段丑陋的代码?

  3. 我怎样才能让我的代码更优雅?

    我不喜欢那些必须为每个容器和每个列重复的大量断点声明。我只能考虑使用记录不充分的可能性来为+container. 但只要+set-container-width不是我在每个媒体查询中执行的唯一代码,即使这个想法也不是一种选择。:(

  4. 与 Susy 一起使用 window-px-widths-first 的推荐方法是什么(并满足我上面描述的要求)?

  5. 请揭示您发现的我的代码的任何缺点。我是 SASS 的新手,我相信您可以提出更有效的方法来做同样的事情。

4

1 回答 1

6

还不错,但有些东西你可以清理。

首先是设置。将 Susy 用于单个列是没有意义的,因此您可以完全放弃您的小网格,手动创建它(只是填充),并拥有更清晰的代码。添加多个列后,您当前的设置就没有多大意义了。85% 的 2 列,有 15% 的装订线?这增加了 185% 的宽度。它有效,因为 Susy 实际上对数字之间的比率比数字本身更感兴趣,但它有点难看。我会将其更改为例如85pxand15px8.5emand 1.5em。由于无论如何您都在覆盖容器,这不应该改变任何东西 - 只是更明智一点。

我要做的另一个主要更改是删除所有 set-column-width 调用,因为无论如何您的宽度都是 100% 的流体覆盖。它所做的只是每次设置 100% 的宽度。何苦?有了这个,我想你可以通过断点的简单循环来自动化开发后台调用。

$layouts: $layout-m $layout-l $layout-xl $layout-xxl
@each $layout in $layouts
  +at-breakpoint($layout)
    +dev-grid-bg

创建一个快捷方式来更改不同断点的列跨度对您或我们来说都是困难的,并且会增加相当多的输出膨胀,除非这真的是您在每种尺寸下所做的唯一更改。你目前拥有的东西看起来不错。

于 2012-11-19T20:56:31.737 回答