gpt4 book ai didi

jquery - LESS 文件扩展名变量

转载 作者:行者123 更新时间:2023-11-28 12:19:05 24 4
gpt4 key购买 nike

是否可以使用 LESS 设置文件扩展名变量?

以下是我所进行的工作的基本概要。

我将 sprite 用作我的背景图像并使用 SVG 作为我的文件类型,效果非常好。但是,它显然不适用于 IE8-。

我在 Modernizr 中使用了一个很好的检查,如果浏览器不支持它,用 .png 扩展名替换 .svg 扩展名:

if (!Modernizr.svg) {
jQuery('img[src$=".svg"]').each(function()
{
jQuery(this).attr('src', jQuery(this).attr('src').replace('.svg', '.png'));
});
}

问题是我使用的 sprite mixin:

@sprite-Grid: 23px;
.sprite(@x, @y) {
background: url(../img/sprite.svg) no-repeat;
background-position: -(@x*@sprite-Grid) -(@y*@sprite-Grid);
}

而且很棒的小图片替换脚本不适用于背景图片,只适用于标签。

理想情况下,我想要做的是创建一个像这样工作的 LESS 变量:

jQuery

if (!Modernizr.svg) {
jQuery('img[src$=".svg"]').each(function()
{
jQuery(this).attr('src', jQuery(this).attr('src').replace('.svg', '.png'));
});


less.modifyVars({
'@filetype': 'png'
});
} else {
less.modifyVars({
'@filetype': 'svg'
});
}

CSS

figure {
.sprite(0,0,@filetype);
}

@sprite-Grid: 23px;
.sprite(@x, @y, @filetype) {
background: url(../img/sprite.@filetype) no-repeat;
background-position: -(@x*@sprite-Grid) -(@y*@sprite-Grid);
}

简而言之,我想创建一个被传递的变量,它取决于浏览器是否支持 SVG。如果是,则将其更改为“sprite.svg”,如果不是,则将其更改为“sprite.png”

这甚至是一件事吗?

非常感谢任何帮助。

最佳答案

由于 Modernizr 为支持的属性向 html 元素添加了类,在本例中添加了 svg 类,这似乎是处理 switch 的最简单方法LESS 是在你的 mixin 中这样做:

.sprite(@x, @y) {
background: url(../img/sprite.png) no-repeat;
background-position: -(@x*@sprite-Grid) -(@y*@sprite-Grid);
.svg & {
background: url(../img/sprite.svg) no-repeat;
}
}

然后在使用的时候(这里假设@sprite-Grid: 23px;),这个LESS:

.testClass {
.sprite(10,10);
}

生成这个 CSS:

.testClass {
background: url(../img/sprite.png) no-repeat;
background-position: -230px -230px;
}
.svg .testClass {
background: url(../img/sprite.svg) no-repeat;
}

因此它将 .svg 类选择器附加到整个选择器链之上,从而在 svg可用的。这会增加 CSS 文件的整体大小,但确实允许预编译 LESS 文件,并防止您必须动态替换背景图像的路径。

关于jquery - LESS 文件扩展名变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com