2014-09-25 28 views
0

我有一个隐藏的子导航高度设置为0.该div的内部是子导航的几个部分。第一次如何获得正确的div innerHeight?

我得到被点击的部分的名称,然后得到该div的innerHeight。然后使用该高度,我将从0到该值的.sub_navigation高度设置为动画。但是,由于某种原因,第一次点击(获取值)时,它会关闭,太高,第二次完美。

你会如何解决这个问题?


角(从jQuery的转换)

// Controller for Nav 
app.controller('NavController', function(){ 
    // Property of Controller 

    this.clicked = function(menuItem) { 
     console.log(menuItem); 

     var contentHeight = $('.'+menuItem+'-content').innerHeight(); 
     var content_height = $('.'+menuItem+'-content').innerHeight(); 

     $('.sub_navigation').css({'height' : '0px'}); 
     $('.'+menuItem+'-content').siblings().css({'display' : 'none'}); 
     $('.'+menuItem+'-content').css({'display':'block', 'height':'auto'}); 
     $('.sub_navigation').animate({ 
      height: contentHeight 
     }); 

     console.log('content_height = '+content_height); 
     console.log(contentHeight); 
    }; 
}); 

的jQuery

$(document).delegate(".navigation-links a", "click", function(){ 
    var myContent = $(this).attr("data-content"); 
    var contentHeight = $("."+myContent+"-content").innerHeight(); 

    $("."+myContent+"-content").siblings().css({"display":"none"}); 
    $("."+myContent+"-content").css({"display":"block", "height":"auto"}); 
    $(".subNavigation").animate({ 
     height: contentHeight 
    }); 
}); 

如果你点击增长,第一时间高度为400,第二次是266 :(

回答

2

The in nerHeight documentation说:

通过.innerHeight()报告的价值是不能保证准确 当元素的父是隐藏的。要获得准确的值,您应在使用.innerHeight()之前先显示父母。

所以,尽管父母是可见的,也许元素本身不可见的事实使高度值不准确。

您是否尝试过更改订单?

//Make the sub menu visible first 
$('.'+menuItem+'-content').siblings().css({'display' : 'none'}); 
$('.'+menuItem+'-content').css({'display':'block', 'height':'auto'}); 

var contentHeight = $('.'+menuItem+'-content').innerHeight(); 
var content_height = $('.'+menuItem+'-content').innerHeight(); 

$('.sub_navigation').css({'height' : '0px'}); 
.... 
+0

甜啊,就是这样! :) 谢谢 – 2014-09-25 18:39:46

1

试图表明该菜单项同时获得高度:

this.clicked = function(menuItem) { 
    var menu = $('.'+menuItem+'-content'); 
    menu.show(); 
    var contentHeight = menu.outerHeight(); 
    menu.hide(); 
    ...