LoginSignup
2
2

More than 5 years have passed since last update.

Sassで乱数を返す関数

Posted at

コード

require 'sass'

module ::Sass::Script::Functions

  # <tt>0</tt>から<tt>x</tt>の間の乱数を返す。
  # 返り値の単位は<tt>x</tt>と同じ。
  # @param [::Sass::Script::Number] x
  # @return [::Sass::Script::Number]
  def rand_between_0_and(x)
    numeric_transformation(x){|value| rand*value}
  end

  # キーワード引数をサポート。
  # この場合は引数1つだから不要ではあるが。
  declare :rand_between_0_and, args: [:x]
end

使い方

// h1-7要素のcolor属性をランダムな色相を持つ暗めの色に設定。
h1, h2, h3, h4, h5, h6, h7
  color: hsl(rand_between_0_and(360deg), 30%, 35%)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2