How to detect lack of position:fixed in a generic way?

前端 未结 2 1390
广开言路
广开言路 2021-01-06 04:32

On mobile devices such as the iPad, I would like to disable a feature that only works if position:fixed is supported. Is there a way to detect these devices without using th

2条回答
  •  旧巷少年郎
    2021-01-06 05:21

    Run the following function to test for position:fixed support.

    function () {
      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;
    }

    From http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED

提交回复
热议问题