测试浏览器是否支持样式

时间:2014-03-14 10:36:14

标签: javascript jquery

我可以执行以下操作来检查浏览器是否支持列计数css3属性,然后使用我自己的代码:

if (!('WebkitColumnCount' in document.body.style
        || 'MozColumnCount' in document.body.style
        || 'OColumnCount' in document.body.style
        || 'MsColumnCount' in document.body.style
        || 'columnCount' in document.body.style))
    {//my own code here}

但是如何检查背景图像动画支持?

这种带有css3的更改图像源仅适用于Chrome浏览器。

0%{background-image: url(image-1);}
50%{background-image: url(image-2);}
100%{background-image: url(image-3);}

所以,我想知道是否有任何技术我们可以测试浏览器是否支持它?

更新

我只是尝试了这个甚至没有检查@keyframes样式支持的代码:

if (('@keyframes' in document.body.style || '@-webkit-keyframes' in document.body.style))
{
   //if(!('from' in @keyframes || 'from' in @webkit-keyframes)){
   //code in here
   alert('test');
   //}
}

所以我甚至不能测试浏览器支持的@keyframes吗?


解决方案:

我是从mdn

找到的
var animation = false,
    animationstring = 'animation',
    keyframeprefix = '',
    domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
    pfx  = '';

if( elm.style.animationName !== undefined ) { animation = true; }    

if( animation === false ) {
  for( var i = 0; i < domPrefixes.length; i++ ) {
    if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
      pfx = domPrefixes[ i ];
      animationstring = pfx + 'Animation';
      keyframeprefix = '-' + pfx.toLowerCase() + '-';
      animation = true;
      break;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

除了在评论中提及 modernizer 之外,我们还可以首先在 vendor prefixes 中添加背景动画以获取更多内容浏览器报道。喜欢:

#animationdivTest { 
   animation: animatedBackground 20s linear infinite;
   -ms-animation: animatedBackground 20s linear infinite;
   -moz-animation: animatedBackground 20s linear infinite;
   -webkit-animation: animatedBackground 20s linear infinite;
}

我猜你正在做什么动画


在javascript中对此进行直接检查可能是

/* check a doc.fragment div or test div on page */
var el = document.getElementById('animationdivTest');
/* create a function here to test for ALL the vendor prefixes 
( this is where modernizer comes in v handy )

 one example - */
if( el.style['-webkit-animation'] !== undefined ) {
   document.getElementsByTagName("html")[0].className +=' cssanimation'; 
}

我们的Css可以定制:

.cssanimation #animationdiv { 
       animation: animatedBackground ...

我们可以在这里进行实时比赛 - http://jsbin.com/yamepucu/1/edit

不确定对你已经知道的事情有多大帮助,希望有些人能做到。


更新:显示style.animation的测试如何与style.background-image的检查相结合(检查背景图片告诉我们@keyframes是否已经过已申请

Try live demo

<强> HTML

  <div id="animationdivTest">Testing...</div>
  <div id="animationdiv"></div>

<强> CSS

@-webkit-keyframes bgAnimation {
  0% {
    background-image: url('http://www.new4.co.uk/flip/flip-3.gif');
  }
  40% {
    background-image: url('http://www.new4.co.uk/flip/flip-2.gif');
  }
  70% {
    background-image: url('http://www.new4.co.uk/flip/flip-1.gif');
  }
  100% {
    background-image: url('http://www.new4.co.uk/flip/flip-0.gif');
  }

}

#animationdivTest, .cssbgAnimation #animationdiv {

  width: 90px;
  height: 240px;
  -webkit-animation-name: bgAnimation;
  -webkit-animation-duration: 1s;
  -webkit-animation-iteration-count: infinite;

}

#animationdiv:after { content:"Sorry not supported"; }  
.cssbgAnimation #animationdiv:after { content:""; }
.cssbgAnimation #animationdivTest { position:absolute; left:-999px; top:-9999px; }

示例JS 在函数循环中添加以检查此次其他供应商前缀

var el = document.getElementById('animationdivTest');
function hasBgAnimSupport() {
  var does =false, 
  animVendorprefixes = ['animation','-ms-animation','-moz-animation','-webkit-animation'];

  for(i=0, len = animVendorprefixes.length; i<len; ++i) {
    if( el.style[animVendorprefixes[i]] !== undefined ) { 
      if( el.style['background-image'] !== undefined ) {
        does=true; break;
      }
    }
  }
  return does;
 } 


if(hasBgAnimSupport() ) { 
   document.getElementsByTagName("html")[0].className +=' cssbgAnimation';
}

AFAIK - 这是我们可以检查的最接近和未来的方式

相关问题