javascript - Modernizr 检查对象是否适合未给出预期结果

标签 javascript internet-explorer modernizr

我正在使用 Modernizr 创建 object-fit 的回退。该解决方案有效,但不是仅将其应用于不支持 object-fit 的浏览器(如 IE),而是将代码应用于所有浏览器。这是为什么?

我的代码:

if ( ! Modernizr.objectfit ) {
    console.log('object fit is not supported');
    tpj('.featuredpost').each(function () {
        var $container = tpj(this),
                imgUrl = $container.find('.img-responsive').prop('src');
        if (imgUrl) {
            $container
                .css('backgroundImage', 'url(' + imgUrl + ')')
                .addClass('compat-object-fit');
        }
    });

    tpj('.big-post').each(function () {
        var $container = tpj(this),
                imgUrl = $container.find('.img-responsive').prop('src');
        if (imgUrl) {
            $container
                .css('backgroundImage', 'url(' + imgUrl + ')')
                .addClass('compat-object-fit');
        }
    });
}else{
    console.log('object is supported');
}

无论我检查什么浏览器,它总是记录:object fit is not supported 即使在支持 object-fit 的浏览器上也是如此。

我该怎么办?

Modernizr 被正确包含和加载(即使我将本地文件更改为 CDN 路径,我得到相同的结果):

<!-- Modernizr Library -->
<script type="text/javascript" src="js/modernizr.min.js"></script>

enter image description here

最佳答案

您必须使用旧版本的 Modernizr。确保您使用的是完整的 Modernizr 3.x 或 custom Modernizr 3.x build使用 CSS Object Fit 功能检查。

See example with custom Modernizr build on Codepen

关于javascript - Modernizr 检查对象是否适合未给出预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340353/

相关文章:

javascript - 为什么 parseInt 使用 Array#map 产生 NaN?

internet-explorer - Internet Explorer探查器

jquery - Modernizr 与 $(document).ready()

javascript - 动画卡片 CSS 3D 过渡 : solution for compatibility IE/Opera?

javascript - 如何使 React Portal 与 React Hook 配合使用?

javascript - Node.JS 停止工作并且控制台充满了点

javascript $.post 返回正确的结果,但无法捕获返回字符串中的所有字符

IE中两行的html输入按钮

javascript - IE 在拖动时平滑 map 平移

html - 那么...我们现在必须开始为 chrome 的日历添加 polyfilling 吗?