Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Bootstrap:affix.js-当我用js调用它时,我无法得到另一个元素的高度并将其放入对象中_Jquery_Twitter Bootstrap_Height_Navbar_Affix - Fatal编程技术网

Jquery Bootstrap:affix.js-当我用js调用它时,我无法得到另一个元素的高度并将其放入对象中

Jquery Bootstrap:affix.js-当我用js调用它时,我无法得到另一个元素的高度并将其放入对象中,jquery,twitter-bootstrap,height,navbar,affix,Jquery,Twitter Bootstrap,Height,Navbar,Affix,这是我的html代码: <div class="masthead"> <img class="img-responsive" data-src="holder.js/990x150/auto" alt="Generic placeholder image"> </div> <!-- /container --> <div id="menu-gora" class="navbar navbar-inverse affix-top"&g

这是我的html代码:

<div class="masthead">
    <img class="img-responsive" data-src="holder.js/990x150/auto" alt="Generic placeholder image">
</div>
<!-- /container -->
<div id="menu-gora" class="navbar navbar-inverse  affix-top">
它很好用。但它不适用于:

top: $('.masthead img').height()
它在通过30像素后开始“粘贴”。总是我需要它的响应能力,我不知道我做错了什么:/


当我使用
console.log($('.masthead img').height())
读取值时,它会显示正确的值。问题出在哪里?

根据bootstrap文档,获得动态高度的“正确”方法是在函数中返回值,如

$('#menu-gora').affix({
    offset: {
        top: function () {
            return (this.top = $('.masthead img').height());
       }
    }
});
然而!这不考虑动态调整大小的内容,因此,例如,如果您有一个旋转木马或手风琴,它将改变粘贴元素所在位置的高度,则必须重新计算新高度。据我所知,没有办法动态更改偏移量。现在我所做的只是在大小改变时重新应用粘贴(我的粘贴菜单上方有未知数量的折叠)

$('#menu-gora').affix({
    offset: {
        top: function () {
            return (this.top = $('.masthead img').height());
       }
    }
});