jQuery check if browser support position: fixed

后端 未结 7 619
灰色年华
灰色年华 2021-01-04 09:13

How do I check if browser supports position:fixed using jQuery. I assume I have to use $.support I think, but how?

Thank you for your time.

相关标签:
7条回答
  • 2021-01-04 09:41

    I've created another check if position:fixed is really supported in browser. It creates fixed div and try to scroll and check if the position of div changed.

    function isPositionFixedSupported(){
        var el = jQuery("<div id='fixed_test' style='position:fixed;top:1px;width:1px;height:1px;'></div>");
        el.appendTo("body");
    
        var prevScrollTop = jQuery(document).scrollTop();
        var expectedResult = 1+prevScrollTop;
        var scrollChanged = false;
    
        //simulate scrolling
        if (prevScrollTop === 0) {
            window.scrollTo(0, 1);
            expectedResult = 2;
            scrollChanged = true;
        }
    
        //check position of div
        suppoorted = (el.offset().top === expectedResult);
    
        if (scrollChanged) {
            window.scrollTo(0, prevScrollTop);
        }
    
        el.remove();
    
        return suppoorted;
    }
    

    This function was tested in Firefox 22, Chrome 28, IE 7-10, Android Browser 2.3.

    0 讨论(0)
  • 2021-01-04 09:44

    I find that mobile safari (specifically iOS 4.2 via the iOS Simulator on OSX) refuses to scroll anywhere unless you wait a few miliseconds. Hence the false positive.

    I wrote a quick jquery plugin to work around it:

    (function($) {
      $.support.fixedPosition = function (callback) {
        setTimeout(
          function () {
            var container = document.body;
            if (document.createElement && container && container.appendChild && container.removeChild) {
              var el = document.createElement('div');
              if (!el.getBoundingClientRect) return null;
              el.innerHTML = 'x';
              el.style.cssText = 'position:fixed;top:100px;';
              container.appendChild(el);
              var originalHeight = container.style.height,
                  originalScrollTop = container.scrollTop;
              container.style.height = '3000px';
              container.scrollTop = 500;
              var elementTop = el.getBoundingClientRect().top;
              container.style.height = originalHeight;
              var isSupported = !!(elementTop === 100);
              container.removeChild(el);
              container.scrollTop = originalScrollTop;
              callback(isSupported);
            }
            else {
              callback(null);
            }
          }, 
          20
        );
      }
    })(jQuery);
    
    0 讨论(0)
  • position:fixed apparently works for all block elements in Mobile Safari (4.3.2) except body, so the CFT answer (http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED) should have this in it:

    var isSupported = (container.scrollTop === 500 && elementTop === 100);

    0 讨论(0)
  • 2021-01-04 09:48

    The feature-test Position fixed support , mentioned above, returns a false-positive on Opera Mini (which does not support position: fixed).

    0 讨论(0)
  • 2021-01-04 10:02

    The most reliable way would be to actually feature-test it. Browser sniffing is fragile and unreliable.

    I have an example of such test in CFT http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED. Note that the test should be run after document.body is loaded.

    0 讨论(0)
  • 2021-01-04 10:03
    function fixedcheck () {
        var fixedDiv = $('<div>').css('position', 'fixed').appendTo('body');
        var pos1 = fixedDiv.offset().top;
        $(window).scrollTop($(window).scrollTop() + 1);
        var pos2 = fixedDiv.offset().top;
        fixedDiv.remove();
        return (pos1 != pos2)
    }
    
    /* Usage */
    $(document).ready(function () {
        if (!fixedcheck()) alert('Your browser does not support fixed position!')
    });
    
    0 讨论(0)
提交回复
热议问题