There are a lot of times I find myself writing:
background-image:url(somesweetpicture.png);
background-repeat:no-repeat;
background-position:0 0;
width: 241px (width of sweet picture)
height: 49px (height of sweet pic)
So, good candidate for a SASS mixin:
@include image-background(somesweetpicture.png, w, h)
Is it possible to retrieve the image dimensions within the mixin 开发者_如何学Gowith ruby code?
It's certainly possible. Compass does it. You'd just have to write a Sass extension that opens the file and gets its dimensions. Check out how they do it:
The page on their docs:
http://compass-style.org/reference/compass/helpers/image-dimensions/#image-height
The relevant code at Github:
https://github.com/chriseppstein/compass/blob/stable/lib/compass/sass_extensions/functions/image_size.rb
If you're determined to take a crack at it yourself, you can extend Sass simply by adding methods to the Sass::Script::Functions
module. The trick no doubt is reading the appropriate metadata from each filetype (png,jpg,gif,etc) to get at the headers. You can see how Compass does it at the above link.
Link to the relevant Sass docs:
http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#adding_custom_functions
精彩评论