编程语言
首页 > 编程语言> > javascript – Modernizr检查对象适合不给出预期的结果

javascript – Modernizr检查对象适合不给出预期的结果

作者:互联网

我正在使用Modernizr为对象拟合创建一个后备.该解决方案正在运行,但它不是仅将其应用于不支持对象的浏览器(如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');
}

无论我检查什么浏览器,它总是记录:即使在支持对象适配的浏览器上也不支持对象适合.

我该怎么办?

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

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

enter image description here

解决方法:

您必须使用旧版Modernizr.确保使用完整的Modernizr 3.x或带有CSS对象匹配功能的custom Modernizr 3.x build.

See example with custom Modernizr build on Codepen

标签:javascript,internet-explorer,modernizr
来源: https://codeday.me/bug/20190705/1389414.html