javascript Modernizr.load 的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12566914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:32:02  来源:igfitidea点击:

Use of Modernizr.load

javascriptjquerymodernizr

提问by Android Learner

I am using Modernizr for conditional loading of resources. My code is

我正在使用 Modernizr 来有条件地加载资源。我的代码是

<link rel="stylesheet" type="text/css" media="all" href="stylesheet/style.css" />
<script type="text/javascript" src="javascript/jQuery/jquery-1.8.1.min.js"></script>
<script src="javascript/stickyheader/jquery.fixedheadertable.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="javascript/stickyheader/defaultTheme.css" />
<script type="text/javascript" src="javascript/modernizr/modernizr.2.6.2.js"></script>
<script type="text/javascript">
    Modernizr.load([ {
        // If browser supports touch
        test : Modernizr.touch,
        //Load iPad related resorces 
        yep : [ 'javascript/ipad-default.js', 'javascript/touchscroll.js', 'javascript/ipad-scroll.js',
                'javascript/mobile.js' ],
        // Load common resorces 
        load : ['javascript/ipad-default.js']
    } ]);
</script> 

This is working fine. But I am wondering if I can load all resources in Modernizr.loadwhen I test for Modernizr.touch.

这工作正常。但我想知道我是否可以在Modernizr.load测试Modernizr.touch.

To be clear I want to load all resources within Modernizr.load.

明确地说,我想加载Modernizr.load.

How can I do this? And is this a good approach?

我怎样才能做到这一点?这是一个好方法吗?

回答by Bruno Sch?pper

Yes you can. It definitely is a good approach to use a resource loader for a web application. However, I found the page rendering to be a little shattering when loading all CSS through Modernizr.

是的你可以。将资源加载器用于 Web 应用程序绝对是一种好方法。但是,我发现在通过 Modernizr 加载所有 CSS 时,页面渲染有点崩溃。

// You can load CSS just like JS
Modernizr.load("stylesheet/style.css", [
  {
    test : Modernizr.touch,
    yep : [ 'javascript/touchscroll.js', 'javascript/ipad-scroll.js', 'javascript/mobile.js' ],
    load : [ 'javascript/ipad-default.js' ] // No need to specify this in 'yep' too
  }]);

Because Modernizr.loadis built on yepnope.js, the yepnope documentationis a little more interesting for resource loading than the Modernizr tutorials. If you don't mind yet another framework, I can recommend requirejs. This one really helps to decouple and load your components.

因为Modernizr.load基于yepnope.jsyepnope 文档对于资源加载比 Modernizr 教程更有趣。如果你不介意另一个框架,我可以推荐requirejs。这真的有助于解耦和加载您的组件。