指南针/ Sass检查文件是否存在 [英] Compass / Sass check if file exists

查看:83
本文介绍了指南针/ Sass检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用罗盘生成子画面,并且有一个mixin可以计算原始资产的宽度和高度并将其添加到CSS中。

I'm using compass to generate sprites and a have a mixin that calculates the width and height of the original asset and adds that into the css.

在此mixin上有一个选项可以为悬浮状态图像传递true或false。但是,不必每次使用mixin都指定它,而是在文件存在的情况下默认添加悬停CSS。

I also have an option on this mixin to pass in true or false for hover state images. But rather than having to specify this each time I use the mixin I'd like to add the hover css by default if the file exists.

我添加了以下内容到我的config.rb文件(到目前为止是我唯一的扩展名)

I've added the following to my config.rb file (thus far its the only extension I have)

module Sass::Script::Functions
  def file_exists(image_file)
    path = image_file.value
    Sass::Script::Bool.new(File.exists?(path))
  end
end

我的混合看起来像这样

@mixin sprite($name, $hover: true, $active: false, $pad:0){
    @include sprite-dimensions($sprites, $name);
    background-repeat: no-repeat;
    background-image: sprite-url($sprites);
    background-position: sprite-position($sprites, $name, -$pad, -$pad);

    @if $hover == true{
        $name_hover: $name + _hover;
        @if file_exists($name_hover){
            &:hover {
                background-position: sprite-position($sprites, $name_hover, -$pad, -$pad);
            }
        }
    }

}

但是 file_exists 函数不会返回 true ;大概是因为文件路径不正确。什么/如何指定正确的路径?

But the file_exists function doesn't return true; presumably because the path to the file is incorrect. What / How do i specify the correct path?

推荐答案

我设法找到了解决方案,而不是检查文件是否存在,它会检查(从确实存在的所有文件中)创建的子画面贴图。

I managed to find a solution, that, rather than checking if the file exists, it checks the sprite map that is created (from all the files that definitely exist)

我更改了

@if file_exists($name_hover){

@if sprite_has_selector($sprites, $name, hover){

这篇关于指南针/ Sass检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆