首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指南针:将精灵位置导出为百分比

指南针:将精灵位置导出为百分比
EN

Stack Overflow用户
提问于 2014-09-10 23:16:10
回答 1查看 210关注 0票数 2

使用compass的sprite生成器时,生成器会基于像素创建CSS规则。

示例代码:

代码语言:javascript
复制
@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;

输出示例:

代码语言:javascript
复制
/* 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;
}

有没有一种方法可以以百分比的形式生成这些数据,以便对它们进行响应?

我已经编写了自己的代码,但它在某些浏览器中不能很好地工作。以下是我的方法:

代码语言:javascript
复制
@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,其中精灵的位置以百分比而不是像素的形式生成?

EN

回答 1

Stack Overflow用户

发布于 2014-09-11 09:52:56

最后,解决方案是使所有百分比都没有小数位或1个小数位。这在所有浏览器中都能很好地工作。

对于有奇怪数量的图像的精灵(让我澄清一下,这些精灵都是相同的宽度),解决方案是让图像变大。例如,如果图像有17张幻灯片,则可以扩展图像以模拟25张幻灯片,从而创建由浏览器正确呈现的百分比。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25769103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档