在 jQuery 中设置背景渐变的 -os-/-ms 前缀失败?

发布于 2025-01-06 05:21:26 字数 1461 浏览 0 评论 0原文

我在这里看到了一些关于 jQuery .css() 不能与 -webkit-gradient 一起使用的其他帖子,但是我还没有找到相关的帖子到-ms-线性梯度-o-线性梯度线性梯度

长话短说,我创建了一个函数,它使用所有最流行的 CSS 属性为元素设置基于 #hex 的背景渐变,以实现跨浏览器兼容性,直接取自 ColorZilla

下面是我正在讨论的特定代码片段:

$(elem).css({
        'background': b,
        'background': '-moz-linear-gradient(top,  '+a+' 0%, '+b+' 100%)', 
        'background': '-webkit-gradient(linear, left top, left bottom, color-stop(0%,'+a+'), color-stop(100%,'+b+'))',
        'background': '-webkit-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
        'background': '-o-linear-gradient(top,  '+a+' 0%,'+b+' 100%)', // Breaks execution
        'background': '-ms-linear-gradient(top,  '+a+' 0%,'+b+' 100%)', // Breaks execution
        'background': 'linear-gradient(top,  '+a+' 0%,'+b+' 100%)', // Breaks execution
        'filter': 'progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+a+'\', endColorstr=\''+b+'\',GradientType=0 )'
        });

我在每个处于活动状态的属性旁边放置了一个 //breaksexecution 注释(或者统称为或单独)中断脚本的执行(没有设置其他属性,例如 background: bb 当然是一个变量)。

我绝对不明白为什么会这样 。

是的 尝试:

  • 使用双引号而不是单引号
  • 替换变量用法(+a++b+

我想也许有一个 。这三个角色中需要转义的角色或其他什么?

任何有助于弄清楚为什么会这样的帮助将不胜感激!

I've seen a couple of other posts on here in regards to jQuery .css() not working with -webkit-gradient, however I've yet to find one pertaining to -ms-linear-gradient, -o-linear-gradient and linear-gradient.

Long story short, I've created a function which sets a #hex based background gradient for an element using the all the most popular CSS properties for cross-browser compatibility, taken directly from ColorZilla.

Here's a look at the particular snippet I'm talking about:

$(elem).css({
        'background': b,
        'background': '-moz-linear-gradient(top,  '+a+' 0%, '+b+' 100%)', 
        'background': '-webkit-gradient(linear, left top, left bottom, color-stop(0%,'+a+'), color-stop(100%,'+b+'))',
        'background': '-webkit-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
        'background': '-o-linear-gradient(top,  '+a+' 0%,'+b+' 100%)', // Breaks execution
        'background': '-ms-linear-gradient(top,  '+a+' 0%,'+b+' 100%)', // Breaks execution
        'background': 'linear-gradient(top,  '+a+' 0%,'+b+' 100%)', // Breaks execution
        'filter': 'progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+a+'\', endColorstr=\''+b+'\',GradientType=0 )'
        });

I've placed a //breaks execution comment next to each of the properties that when active (either collectively, or individually) break the script's execution (no other properties, e.g. background: b (b of course being a variable) are set.

I'm absolutely stumped as to why this is.

So far I've tried:

  • Using double quotes instead of single quotes.
  • Replacing the variable usage (+a+, +b+) with actual hex's.

I'm thinking perhaps there's a character in those three that needs to be escaped or something?

Any help towards figuring out why this is would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城之時有危險 2025-01-13 05:21:26

您一次又一次地设置和重新设置 JavaScript 对象的 background 属性。

最后,如果您尝试记录传递给 .css() 的对象,您会发现它只包含 2 个属性:最后一个 background 值列表和过滤器

所以你的代码相当于:

$(elem).css({
    'background': 'linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    'filter': 'progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+a+'\', endColorstr=\''+b+'\',GradientType=0 )'
});

你可以尝试这样的东西(jsfiddle demo):

var i, l, backgrounds = [
    '-moz-linear-gradient(top,  '+a+' 0%, '+b+' 100%)', 
    '-webkit-gradient(linear, left top, left bottom, color-stop(0%,'+a+'), color-stop(100%,'+b+'))',
    '-webkit-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    '-o-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    '-ms-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    'linear-gradient(top,  '+a+' 0%,'+b+' 100%)'
];

// Try each background declaration, one at a time
// Like in a stylesheet, the browser should(!) ignore those
// it doesn't understand.
for( i = 0, l = backgrounds.length ; i < l ; i++ ) {
    $(elem).css({ background: backgrounds[i] });
}

$(elem).css({
    'filter': "progid:DXImageTransform.Microsoft.gradient( startColorstr='"+a+"', endColorstr='"+b+"',GradientType=0 )"
});

这段代码当然是效率不是很高。对于理解多个声明的浏览器,它将毫无意义地覆盖那些已经起作用的声明。因此,请根据您认为合适的方式进行优化。

You're setting and re-setting the background property of the JavaScript object again and again.

In the end, if you try logging the object you're passing to .css(), you'll see it only contains 2 properties: The last background value in the list, and filter.

So your code is equivalent to:

$(elem).css({
    'background': 'linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    'filter': 'progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+a+'\', endColorstr=\''+b+'\',GradientType=0 )'
});

You can try something like this instead (jsfiddle demo):

var i, l, backgrounds = [
    '-moz-linear-gradient(top,  '+a+' 0%, '+b+' 100%)', 
    '-webkit-gradient(linear, left top, left bottom, color-stop(0%,'+a+'), color-stop(100%,'+b+'))',
    '-webkit-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    '-o-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    '-ms-linear-gradient(top,  '+a+' 0%,'+b+' 100%)',
    'linear-gradient(top,  '+a+' 0%,'+b+' 100%)'
];

// Try each background declaration, one at a time
// Like in a stylesheet, the browser should(!) ignore those
// it doesn't understand.
for( i = 0, l = backgrounds.length ; i < l ; i++ ) {
    $(elem).css({ background: backgrounds[i] });
}

$(elem).css({
    'filter': "progid:DXImageTransform.Microsoft.gradient( startColorstr='"+a+"', endColorstr='"+b+"',GradientType=0 )"
});

This code is of course not very efficient at all. And for browsers that understand more than one of the declarations, it'll pointlessly overwrite those that work already. So optimize as you see fit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文