Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查CSS线性渐变支持_Javascript_Html_Css_Cross Browser - Fatal编程技术网

Javascript 检查CSS线性渐变支持

Javascript 检查CSS线性渐变支持,javascript,html,css,cross-browser,Javascript,Html,Css,Cross Browser,可通过以下代码检查对CSS属性(此处为边界半径)的支持: if(document.createElement('test').style.borderRadius===''){ //some code } 但是在线性渐变的情况下我该怎么办呢? 声明如下: background:linear-gradient(top,bottom,#123,#456); 另外,我不想使用现代化。我想学习如何做到这一点。我为您查找了Modernizer的源代码。在尝试设置渐变后,它会在背景图像上进行字符串搜索。

可通过以下代码检查对CSS属性(此处为边界半径)的支持:

if(document.createElement('test').style.borderRadius===''){
//some code
}
但是在线性渐变的情况下我该怎么办呢? 声明如下:

background:linear-gradient(top,bottom,#123,#456);

另外,我不想使用现代化。我想学习如何做到这一点。

我为您查找了Modernizer的源代码。在尝试设置渐变后,它会在背景图像上进行字符串搜索。这是:


您可能应该直接使用Modernizer,而不是自己复制或重写它。有时人们会从第三方那里借来东西,如果许可证允许的话,那也没关系,只要试着将它与代码分开,包括许可证和版权信息。

我为您查找了Modernizer的源代码。在尝试设置渐变后,它会在背景图像上进行字符串搜索。这是:


您可能应该直接使用Modernizer,而不是自己复制或重写它。有时人们会从第三方那里借来东西,如果许可证允许的话,那也没关系,只要试着将它与代码分开,包括许可证和版权信息。

你肯定可以推出自己的《穷人的现代化》。但是,如果您确实这样做,我建议将所有检查封装在浏览器验证对象中,以便于使用。我是这样做的:

//检查浏览器功能
变量浏览器={
“3d”:testCss(“透视”、“透视”),
“addEventListener:!!window.addEventListener,
“动画”:testCss('animationName','animationName'),
“canvas”:!!window.CanvasRenderingContext2D,
“渐变”:testCss(“背景图像”,“背景图像:线性渐变(黑色,白色)”,径向渐变(黑色,白色)”,
'svg':!!(document.createElements&&document.createElements('http://www.w3.org/2000/svg“,”svg')。createSVGRect),
'touch':!!(window | | |(window.DocumentTouch&&documentinstanceof DocumentTouch)| | | navigator.msMaxTouchPoints)中的“ontouchstart”,
“transitions”:testCss(“transitionProperty”、“Transition”),
'vw':testCss('width','width:100vw')
};
函数测试CSS(sPropStandard、sPropPrefixed、sCss){
var isSupported=false;
如果(sCss==未定义){
//检查属性支持
var aProps=[
sPropStandard,
“Webkit”+sPropPrefixed,
“Moz”+sPropPrefixed,
“ms”+sPropPrefixed,
“O”+sPropPrefixed
];

对于(var i=0;i,您完全可以推出自己的穷人现代化产品。但是,如果您真的这么做,我建议将所有检查封装在浏览器验证对象中,以便于使用。我的做法如下:

//检查浏览器功能
变量浏览器={
“3d”:testCss(“透视”、“透视”),
“addEventListener:!!window.addEventListener,
“动画”:testCss('animationName','animationName'),
“canvas”:!!window.CanvasRenderingContext2D,
“渐变”:testCss(“背景图像”,“背景图像:线性渐变(黑色,白色)”,径向渐变(黑色,白色)”,
'svg':!!(document.createElements&&document.createElements('http://www.w3.org/2000/svg“,”svg')。createSVGRect),
'touch':!!(window | | |(window.DocumentTouch&&documentinstanceof DocumentTouch)| | | navigator.msMaxTouchPoints)中的“ontouchstart”,
“transitions”:testCss(“transitionProperty”、“Transition”),
'vw':testCss('width','width:100vw')
};
函数测试CSS(sPropStandard、sPropPrefixed、sCss){
var isSupported=false;
如果(sCss==未定义){
//检查属性支持
var aProps=[
sPropStandard,
“Webkit”+sPropPrefixed,
“Moz”+sPropPrefixed,
“ms”+sPropPrefixed,
“O”+sPropPrefixed
];

对于(var i=0;我不能优雅地降级它,这样就不需要检查它了吗?为什么不看看Modernizer的源代码呢?你不能优雅地降级它,这样就不需要检查它了吗?为什么不看看Modernizer的源代码呢?代码:test=document.createElement('test'));test.style.cssText='background:-webkit线性渐变(到顶部,#123,#456);background:-moz线性渐变(到顶部,#123,#456);background:-o线性渐变(到顶部,#123,#456);background:-ms线性渐变(到顶部,#123,#456);background:线性渐变(到顶部,#123,#456);if(!(test.style.cssText.indexOf('gradient'))//更改样式或任何东西的代码对我来说都很好。:)代码:test=document.createElement('test');test.style.cssText='background:-webkit线性渐变(到顶部,#123,#456);background:-moz线性渐变(到顶部,#123,#456);background:-o-linear渐变(到顶部,#123,#456);background:-ms线性渐变(到顶部,#123,#456);背景:线性渐变(到顶部,#123,#456);(if(!(test.style.cssText.indexOf('gradient')>-1))//更改样式的代码或任何适合我的代码。:)
    var str1 = 'background-image:';
    var str2 = 'gradient(linear,left top,right bottom,from(#9f9),to(white));';
    var str3 = 'linear-gradient(left top,#9f9, white);';

    var css =
      // legacy webkit syntax (FIXME: remove when syntax not in use anymore)
      (str1 + '-webkit- '.split(' ').join(str2 + str1) +
       // standard syntax             // trailing 'background-image:'
       prefixes.join(str3 + str1)).slice(0, -str1.length);

    var elem = createElement('div');
    var style = elem.style;
    style.cssText = css;

    // IE6 returns undefined so cast to string
    return ('' + style.backgroundImage).indexOf('gradient') > -1;