Detect support for background-attachment: fixed?

前端 未结 7 1792
花落未央
花落未央 2020-12-31 02:09

Is there a way to detect browser support for background-attachment: fixed?

Edit: Although this feature is widely supported on desktop browsers it is poorly supported

7条回答
  •  太阳男子
    2020-12-31 03:07

    You might look at document.body.style and make sure that

    • there's a property there called "backgroundAttachment", and
    • you can set it to "fixed", and it retains its value when you do so.

    Chrome, FF, Opera, and Safari all ignore attempts to set the property to an invalid value. IE9 throws an exception when you try. So if either one happens, that value definitely isn't supported. (If the browser just blindly sets the value and retains it, then it still might not work. But at that point, you really can't the browser to tell you much anyway.)

    function supportsFixedBackground() {
        try {
            var style = document.body.style;
            if (!("backgroundAttachment" in style)) return false;
            var oldValue = style.backgroundAttachment;
            style.backgroundAttachment = "fixed";
            var isSupported = (style.backgroundAttachment === "fixed");
            style.backgroundAttachment = oldValue;
            return isSupported;
        }
        catch (e) {
            return false;
        }
    }
    

    I don't bother with IE6 anymore, and don't have another browser handy that doesn't support fixed backgrounds, so i'm unable to test setting "fixed".

提交回复
热议问题