- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个隐藏的子导航,高度设置为 0。该 div 内部是子导航的几个部分。
我获取被单击部分的名称,然后获取该 div 的内部高度。然后使用该高度,将 .sub_navigation
高度从 0 动画化到该值。然而,由于某种原因,第一次点击(获取值)时,它关闭了,太高了,第二次就完美了。
你会如何解决这个问题?
<小时/>Angular(从 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:(
最佳答案
内部高度documentation说:
The value reported by
.innerHeight()
is not guaranteed to be accurate when the element's parent is hidden. To get an accurate value, you should show the parent first, before using.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'});
....
关于javascript - 如何第一次获得正确的div innerHeight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26044964/
当用户滚动到底部时,我正在尝试执行某些操作。 这段代码可以在桌面上运行。 但是在我的 Android 4.3 Google Chrome 和 iPhone Safari 上,它没有将 if 语句解析为
我正在使用 jQuery 2.0.3 问题: $(window).innerHeight() 返回零。 但是 window.innerHeight 返回正确的高度。 控制台中的行为相同。 此问题仅存在
我有一个隐藏的子导航,高度设置为 0。该 div 内部是子导航的几个部分。 我获取被单击部分的名称,然后获取该 div 的内部高度。然后使用该高度,将 .sub_navigation 高度从 0 动画
我有一个事件监听器,如下所示: window.addEventListener("resize", function(data){ console.log(data); // Do s
我有一些依赖于window.innerHeight的计算。但据我发现,这在 IE9 之前的任何 IE 中都不可用。我看过的所有其他选项甚至都没有接近我使用 window.innerHeight 时得到
背景 我正在尝试优化我的 JavaScript,减少对 DOM 的调用并使用更少的计算值。 问题 使用值window.innerHeight时,该值是每次计算还是来自初始计算? 示例 是这个吗 let
相当于window.innerHeight的jquery是什么 我试过了 $(window).innerHeight() 和 $(window).height() 但是两者都没有给我我想要的值。他们返
有人可以解释一下为什么设置 height 吗?视口(viewport)属性: 没用?window.innerWidth报告 480px 但是 window.innerHeight 210,不仅仅是
当我设置width=device-width视口属性时,它作为-window.innerWidth由设备相应地更改,但是当我更改height=device-height时它不起作用-window.in
我正在使用 bootstrap 编写一个响应式网站,我只想要一个与屏幕实际大小相同的元素。 每当我执行 height/width: 100% 时,浏览器都会给我一个滚动条,因为它会将我的视口(view
我正在这里处理一些 javascript svg 图形,我发现我真的不能依赖 Firefox 上 window.innerHeight(或outerHeight)的值。我正在运行 Ubuntu 12.
我正在使用 window.onresize 来监听视口(viewport)大小的变化,这样我就可以适本地缩放我的内容(这是一个游戏 UI,所以使用 window.onresize + css scal
我正在测试我的一个 friend 注意到的一个测试用例。他在循环中使用 window.innerheight,fps 出现了非常显着的下降。 这是我创建的用于测试它的 jsperf 的链接 http:
我正在实现一个以 html/css/js 为中心的叠加层,即使用户在页面上放大和缩小时它也保持居中。 在 iOs Chrome、iOs Safari、Default Android 和 Android
我的测试手机之一是 Galaxy S3 Mini。这部手机的规范说它是 800x480 分辨率。 但是,当我在我的 HTML 游戏中执行以下操作时: window.innerWidth window.
window.innerHeight 是的,它将返回移动设备上浏览器的高度值。然而,当用户尝试通过捏合来放大或缩小时,问题就会出现(在某些浏览器上)。该值将无法正确调整,而是仍然返回页面的完整长度。
CSS 代码: #jpspm { position: relative; top: -83px; left: -1px; padding-top: 3px; p
我正在尝试根据它的高度确定 div (.content) 的顶部/底部填充,并根据窗口的加载和调整大小重新计算它。这应该与旁边的另一个 div (.character) 很好地居中对齐。 我试过使用
对于某些人来说,这可能是一个非常愚蠢的问题,但我肯定无法理解这背后的问题。代码相当简单,如下所示: HTML: ####
我有一个 header ,其高度等于窗口的 innherheight。不过,这会使页眉的子元素处于相同的位置,就好像页眉的高度未定义一样。我想将一个特定的 child 放在标题的底部,而不管它的高度如
我是一名优秀的程序员,十分优秀!