SASS:从列表中随机选择背景图像

Mul*_*gno 16 css sass css-selectors css3

我需要输出这个:

#footer-widgets .container .row {
    background-image: url("RANDOMLY PICKED");
    background-position: right bottom;
    background-repeat: no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

...并且应该有一个列表,其中包含4或5个指向实际背景图像的链接(http://domain.com/blablabla/image.png).我怎样才能用SASS做到这一点?

Kat*_*ieK 25

最新版本的Sass (v3.3.0)增加了一个新random功能.如果将它与图像列表(以及一点点变量插值)混合使用,则每次编译Sass时,您将拥有随机选择的背景图像的CSS.例:

$imgKey: random(5);

$list: apple, banana, cherry, durian, eggplant;
$nth: nth($list, $imgKey);

body {
  background-image: "/images/#{$nth}.jpg";
}
Run Code Online (Sandbox Code Playgroud)

实例:http://sassmeister.com/gist/8966210

如上所述,随机值仅在编译Sass时更改,这不一定是每次访问页面时.