测试浏览器是否支持该样式

2024-03-20

我可以执行以下操作来检查浏览器是否不支持列计数 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);}

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

Update

我只是尝试像这样的代码,它甚至没有检查 @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 https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations/Detecting_CSS_animation_support

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;
    }
  }
}

以及提到现代化者 http://modernizr.com/download/在评论中 - 我们可以首先添加供应商前缀 http://caniuse.com/#search=animation让背景动画获得更多的浏览器覆盖率。喜欢 :

#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 http://jsbin.com/yamepucu/1/edit

不确定这对你已经知道的有多大帮助,希望某些部分能做到。


Update:展示如何测试style.animation可以与检查结合起来style.background-image ( 检查背景图像可以告诉我们 @keyframes 是否已应用 )

尝试现场演示 http://jsbin.com/hinidape/1/edit

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 - 这是我们可以检查的最接近且面向未来的方法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试浏览器是否支持该样式 的相关文章

随机推荐

  • Str_Detect 使用跨列检测多列

    我想根据以下结果创建一个新专栏str detect跨多列使用across 例如 在下面的测试数据中 我想在以 job 开头的列中搜索 No job 如果在任何列中检测到该字符串 则返回 1 如果未检测到 则返回 0 test data lt
  • 使用 Keras 预测进行 Python 多处理

    Context Keras 模型 链接在这里 https drive google com file d 1f0WGCv11uObPziySE2wl6hXYKfyjqXBQ view usp sharing 为了 MWE 需要并行预测大量测
  • 像调用break一样短路Array.forEach

    1 2 3 forEach function el if el 1 break 我怎样才能使用新的来做到这一点forEachJavaScript 中的方法 我试过了return return false and break break崩溃和
  • 2020 年如何在 Windows 版 Git 中永久使用“LF”行结尾?

    我们的存储库使用LF 我的 Git for Windows 安装使用按原样签出 提交 Unix 风格的行结尾 但我在 IDE 中签出的每个文件中仍然出现错误 因为它仍然收到CRLF一直以来 即使它确实支持LF并配置为使用LF通过签到 edi
  • 使用 shell 脚本将人类可读的时间转换为 EPOCH

    我有一个人类可读的时间 08 18 2016 09 18 25 我希望使用 shell 脚本将其转换为纪元时间 我尝试过date s 但我收到错误 日期 无效日期 08 18 2016 09 32 42 将日期时间转换为纪元的规范方法是使用
  • Android Studio 2.3 更新后,Android 应用程序未在模拟器中加载

    我刚刚将 Android Studio 版本上传到 2 3 现在在模拟器中测试我的应用程序时遇到问题 它是一个 Nexus 5X 模拟器 上面加载了 Android 7 0 Nougat API 24 ABI armeabi v7a 我有一
  • Android 中的对象 XML 映射

    我正在开发一个基于客户端 服务器模型的应用程序 其中客户端在 Android 中 服务器在 PHP 中 我想将产品信息 例如名称 价格 说明 从客户端传输到服务器 我已读过编组 解组或序列化它可以实现 但所有教程和示例都是用 Java 编写
  • 业务逻辑层

    我正在使用 ASP NET 和 Telerik 控件 v2009 q2 来编程数据驱动的应用程序 我有一个名为 BLL 的类 它包含 几乎仅 静态类 这些类返回不同的对象 并以一些 id 作为参数 通常以列表形式返回对象组 我的问题是 总是
  • 两个日期范围之间有多少相等的天数,SQL

    我有包含日期 范围的表格 如下所示 DATE DATE2 14 03 2013 17 03 2013 13 04 2013 02 05 2013 我必须创建一个过程 返回等于两个日期范围的天数 一个在表中 另一个在表中 例如 我在表中的日期
  • g++ 版本 4.0.0.8 和 4.3.2 之间有什么区别?

    g 4 0 0 8 和 g 4 3 2 有什么区别 这两个是我在各种编程竞赛中见过的最常用的 C 编译器 我尝试用谷歌搜索 但一无所获 考虑到您对两者之间的 C 变化感兴趣 这实际上并不是一个 巨大的列表 4 0 0 8 只是 4 0 的补
  • 没有身份的 Cookie Asp.net core

    我目前正在开发一个不使用身份的项目 问题是这个项目应该有一个记住我的选项 允许用户自动重新连接到网站 我的问题是我找不到任何完整的教程来创建没有身份的 cookie 如果有人有很好的代码示例或教程 Thanks 在我的项目中 我使用 Ang
  • 您如何使用 TDD 来划分班级?

    我觉得自己对TDD相当熟练 甚至在公司里被认为是 TDD专家 但尽管如此 还是有一些情况我觉得不知道如何正确处理 所以我想听听别人的意见 我的问题如下 尽管一般来说 TDD 帮助我思考类的核心职责 并将所有其他职责提取到依赖类中 但有些情况
  • int 和 double 的均匀随机分布“基类”?

    我正在尝试创建一个用随机数填充列表的函数 并根据列表项的类型生成整数或浮点数 到目前为止 我已经想出了以下代码 并且它有效 template
  • Laravel 中使用 try 和 catch 进行错误处理 [重复]

    这个问题在这里已经有答案了 我想在我的应用程序中实现良好的错误处理 我强制使用此文件来捕获错误 应用 服务 PayUService try this gt buildXMLHeader Should be this gt buildXMLH
  • iOS 检测系统音量变化。私有 API 与否? AVSystemController_SystemVolumeDidChangeNotification

    可以听AVSystemController SystemVolumeDidChangeNotificationNSNotification 是否被视为 在 App Store 审核过程中 使用私有 API 在我的应用程序中 我需要显示和更新
  • MySql 中是否有用于添加列的“IF NOT EXISTS”子句?

    我需要在一些数据库上运行这个 MySql 代码 我怀疑其中一些数据库已经有了这个专栏 有没有类似的东西if not exists对于下面的代码 ALTER TABLE comments ADD COLUMN active int 1 NOT
  • Google Maps API V3 使用限制是每个网站访问者还是每个网络服务器?

    我对每天 2500 个地理编码请求的 API 使用限制是否感到困惑 http code google com apis maps documentation geocoding Limits http code google com api
  • 显示字段上内联 AJAX 调用的验证错误

    当 AJAX 调用返回验证失败时 我试图在字段 内联 上显示错误消息
  • 如何构造动态变量NAMES?

    我想循环数据 并创建动态地图 稍后可以将更多数据推入其中 例如 foreach item in bob john andy set item map end 这样以后我就可以这样做 bob map put foreach count som
  • 测试浏览器是否支持该样式

    我可以执行以下操作来检查浏览器是否不支持列计数 css3 属性 然后使用我自己的代码 if WebkitColumnCount in document body style MozColumnCount in document body s