2

我正在使用 jQuery 和 mootools 库,我有这个错误

TypeError: offset is null

 [Parar en este error]  
 if (jQuery(window).scrollTop() > offset.top) {

这是代码:

<link rel="stylesheet" type="text/css" href="default.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="wp-styles.css" media="screen"/>
<link rel="stylesheet" href="css/ui.css" type="text/css" />
<link rel="stylesheet" href="css/horizontal.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
    jQuery.noConflict();
    (function(jQuery){

        var offset = jQuery("#bichito").offset();
        var topPadding = 200;
        jQuery(window).scroll(function() {
            if (jQuery(window).scrollTop() > offset.top) {
                jQuery("#bichito").stop().animate({
                    marginTop: jQuery(window).scrollTop() - offset.top + topPadding
                });
            } else {
                jQuery("#bichito").stop().animate({
                    marginTop: 0
                });
            };
        });
    })(jQuery);
</script>
<script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
<script type="text/javascript" src="Scripts/ajax.js"></script>
<script type="text/javascript" src="Scripts/mootools-1.2-core.js"></script>
<script type="text/javascript" src="Scripts/mootools-1.2-more.js"></script>
    <!--[if IE]>
        <script type="text/javascript" src="scripts/excanvas-compressed.js"></script>
    <![endif]-->
<!--[if lt IE 7]>
<script defer type="text/javascript" src="Scripts/pngfix.js"></script>
<![endif]-->

<script type="text/javascript" src="Scripts/mocha.js" charset="utf-8"></script>
<script type="text/javascript" src="Scripts/slideitmoo-1.1.js"></script>

<script type="text/javascript">


window.addEvent('domready', function(){
        $$('.comprar').each(function(btnCompra) {
                    btnCompra.addEvent('click',function(event){

. . . 还有更多的motools功能。如果我只使用它工作的 jquery 代码,与 mootools 函数相同,但两者都没有。提前致谢。

4

1 回答 1

0

如果在空集合上调用jQueryoffset()方法,则返回。undefined似乎您在加载 DOM 之前正在运行您的 jQuery 代码,并且您的jQuery("#bichito")元素此时不存在。

您可以像这样修复您的代码:

jQuery.noConflict();
(function(jQuery){ // <-- do you really need closure here?
    jQuery(function(){ // <-- wait DOM ready

        var offset = jQuery("#bichito").offset();
        var topPadding = 200;
        jQuery(window).scroll(function() {
            if (jQuery(window).scrollTop() > offset.top) {
                jQuery("#bichito").stop().animate({
                    marginTop: jQuery(window).scrollTop() - offset.top + topPadding
                });
            } else {
                jQuery("#bichito").stop().animate({
                    marginTop: 0
                });
            };
        });
    });
})(jQuery);
于 2013-05-30T08:50:58.210 回答