检查对 css 属性属性的支持

Checking the support for a css property attribute

本文关键字:属性 支持 css 检查      更新时间:2023-09-26

我想测试浏览器中是否支持特定的css属性属性。对于 css 属性,我可以这样做

document.createElement("detect").style["-webkit-overflow-scrolling"] === ""

但是,如果我必须检查特定的类或属性怎么办。例如,我想测试对

position:fixed

我该怎么做(除了使用现代化)?请帮忙。

function isFixedSupported() {
  var isSupported = null;
  if (document.createElement) {
      var el = document.createElement("div");
      if (el && el.style) {
          el.style.position = "fixed";
          el.style.top = "10px";
          var root = document.body;
          if (root && root.appendChild && root.removeChild) {
              root.appendChild(el);
              isSupported = el.offsetTop === 10;
              root.removeChild(el);
          }
      }
  }
  return isSupported;
}
var canUseFixed = isFixedSupported(); //true:false

小提琴