使用compass的sprite生成器时,生成器会基于像素创建CSS规则。
示例代码:
@import 'compass/utilities/sprites';
$sprite-2-layout: horizontal;
@import "sections/anatomy-sprite-test/sprite-2/*.png";
@include all-sprite-2-sprites;
$sprite-3-layout: horizontal;
@import "sections/anatomy-sprite-test/sprite-3/*.png";
@include all-sprite-3-sprites;
$sprite-4-layout: horizontal;
@import "sections/anatomy-sprite-test/sprite-4/*.png";
@include all-sprite-4-sprites;输出示例:
/* line 84, ../../../../../../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/utilities/sprites/_base.scss */
.sprite-2-00 {
background-position: 0 0;
}
/* line 84, ../../../../../../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/utilities/sprites/_base.scss */
.sprite-2-00 {
background-position: -244px 0;
}有没有一种方法可以以百分比的形式生成这些数据,以便对它们进行响应?
我已经编写了自己的代码,但它在某些浏览器中不能很好地工作。以下是我的方法:
@mixin sprite-percentage-step-generate($steps, $single_image_width) {
$total_image_width: ($single_image_width * $steps) - $single_image_width;
background-position: 0 0;
img {
display: none;
}
@for $i from 0 through ($steps - 1) {
$step_width: $i * $single_image_width;
&.step-#{$i} {
background-position: percentage(($step_width / $total_image_width)) 0;
// We include these to see the relation between the percentage and the step count
img {
display: none;
}
}
}
}也许这不是一个好主意,因为生成器可能会做一些特殊的优化,以使基于像素的方法工作得最好?
同样,问题是:有没有一种方法可以让Compass生成CSS,其中精灵的位置以百分比而不是像素的形式生成?
发布于 2014-09-11 09:52:56
最后,解决方案是使所有百分比都没有小数位或1个小数位。这在所有浏览器中都能很好地工作。
对于有奇怪数量的图像的精灵(让我澄清一下,这些精灵都是相同的宽度),解决方案是让图像变大。例如,如果图像有17张幻灯片,则可以扩展图像以模拟25张幻灯片,从而创建由浏览器正确呈现的百分比。
https://stackoverflow.com/questions/25769103
复制相似问题