0

我只想在不涉及触摸设备时才包含fancybox。到目前为止,这是我的代码:

<script>
    yepnope([{
        test : Modernizr.touch,
        nope  : './js/lib/fancybox/jquery.fancybox.pack.js',
        callback : {
            "jquery.fancybox.pack.js": function () {
                console.log("fancybox loaded!");
            }
        }
    }]);
</script>

此代码放置在结束正文标记之前。我收到错误消息TypeError: k.apply is not a function,但这对我没有帮助。

我的问题:

  • 我可以用这个相对路径加载js文件吗?
  • 我的相对路径从哪里开始?从yepnope所在的路径?
  • 如何有条件地加载 JS 和 CSS 文件?

更新:

现在我尝试了另一种方法:

<script>
Modernizr.load([
    {
        test  : Modernizr.mq('screen and (max-width: 31.25em)'),
        yep  : {
            'photoswipe' : ['/js/klass.min.js', '/js/code.photoswipe-3.0.5.min.js']
        },
        nope : {
            'fancybox' : ['/js/lib/fancybox/jquery.fancybox.pack.js', '/js/lib/fancybox/jquery.fancybox.css']
        },
        callback : {
            'photoswipe': function (url, result, key) {
                var myPhotoSwipe = $("a.fancy").photoSwipe({ enableMouseWheel: false , enableKeyboard: false });
            },
            'fancybox': function (url, result, key) {
                $('a.fancy').fancybox();
            }
        }
    }
]);
</script>

我收到以下错误TypeError: a.split is not a function。我做错了什么?

4

2 回答 2

1

Q: Can I load the js file with this relative path?

Yes, provided you have a structure similar to :

 index.html
--js/
----lib/
------modernizr.custom.js
--------fancybox/
----------jquery.fancyboxy.pack.js

See next answer to explain this...

Q: Where does my relative path starts? From the path where yepnope lies?

Modernizr takes the path from the position of the html page, not from the position of the modernizr load script (since it creates that script reference with that path in the html).

Q: How can I conditionally load a JS and a CSS file?

Pass in an array to nope instead of the single string for just the JavaScript :

nope : ['./js/lib/fancybox/jquery.fancybox.pack.js', './styles/fancybox/jquery.fancybox.pack.css']

Take a look at the docs, they're pretty good on this... http://modernizr.com/docs/#load

于 2013-09-27T09:05:14.930 回答
0

相对路径./folder是可能的(/开头不是)。您也不能将键与数组一起使用。数组或单独的键,如下所示:

<script>
    Modernizr.load({
        test: Modernizr.mq('screen and (max-width: 31.25em)'),
        yep: {
            'photoswipe-klass' : './js/klass.min.js',
            'photoswipe-js' : './js/code.photoswipe-3.0.5.min.js'
        },
        nope: {
            'fancybox-js' : './js/lib/fancybox/jquery.fancybox.pack.js',
            'fancybox-css' : './js/lib/fancybox/jquery.fancybox.css'
        },
        callback: {
            'photoswipe-js': function (url, result, key) {
                var myPhotoSwipe = $("a.fancy").photoSwipe({ enableMouseWheel: false , enableKeyboard: false });
            },
            'fancybox-js' : function (url, result, key) {
                $('a.fancy').fancybox();
            }
        }

    });
</script>
于 2013-09-27T13:39:15.007 回答