- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在检查一些动态生成的内容的滚动高度,以确定是否在内容中包含更多按钮。用户可以点击更多按钮来展开内容并查看里面的所有内容。然而,每次使用相同的函数时,有时它会说元素的 scrollHeight 为 0,这不用说会破坏这些元素的功能。我非常不明白为什么这个函数在 90% 的时间里都能正常工作,但有时会说元素没有 scrollHeight 而实际上它们显然有(它们占用页面上的空间)。
函数如下:
function hide_show_expand_button(length){
var current_div = '';
var current_span = '';
//console.log(length);
for(var i = 0; i < length; i++){
if(document.getElementById("message_span"+i)){
current_div = document.getElementById("items_content"+i);
current_span = document.getElementById("message_span"+i);
console.log(current_div.scrollHeight, "height of the div")
if(current_span.scrollHeight > 128 && current_div.scrollHeight < 170){
console.log("inside of the check for the conversation screen");
current_div.innerHTML +="<a href=\"#\" id='expand_colapse_button"+i+"' class=\"expand_colapse_button_conversation\" style=\"color:blue;\" onclick='expand_colapse(\""+ i + "\")'>More...</a>";
} else if(current_span.scrollHeight > 128 && document.getElementById("items_content"+i).scrollHeight > 200 ){
current_div.innerHTML+="<a href=\"#\" id='expand_colapse_button"+i+"' class=\"expand_colapse_button_attatchment\" style=\"color:blue;\" onclick='expand_colapse(\""+ i + "\")'>More...</a>";
} else if(current_span.scrollHeight > 128 && current_div.scrollHeight < 200){
current_div.innerHTML +="<a href=\"#\" id='expand_colapse_button"+i+"' class=\"expand_colapse_button\" style=\"color:blue;\" onclick='expand_colapse(\""+ i + "\")'>More...</a>";
}
}
}
}
如您所见,我正在检查 span 和 div 上的 scrollHeight,因为我生成了几种不同类型的单元格,我必须根据单元格类型将更多按钮放置在不同的位置.这个函数只在生成被检查内容的函数结束时被调用,我已经用 setTimeout()
调用尝试过它,因为它在它们完全完成之前通过这个函数的机会很小创建(我不知道那会如何发生,但谁知道)。
附言这一切都发生在 chrome ext 中。我不认为这与它有任何关系,但谁知道呢。元素上的位置也是相对的,因为我看到其他人发布了关于 position: absolute
的 scrollHeight 问题。
编辑更具体:
希望这会有所帮助,我尝试测量的单元格位于一个包含 div 中,该 div 可以通过几种不同的方式将其显示设置为无。上面发生的事情发生在我从容器导航离开(将显示设置为无)然后导航回它(将显示设置为阻止)的情况之一。您可以在 chrome ext 中以十几种方式执行此操作。但它只说他们没有 scrollHeight(或我尝试使用 getBoundingClientRect()
的任何高度,并且它为所有内容返回 0)对于一种情况,这就是我感到困惑的原因。
了解此信息可能也很有用,在导航回容器时,每次都会重建单元格,正如我上面所述,我在构建单元格的函数末尾调用了获取 scrollHeight 的函数。所以每次你回到容器时,我的函数也会被调用。
最佳答案
我建议查看这些(我找不到适用于 Chrome 的类似链接):
MSDN:http://msdn.microsoft.com/en-us/library/ms530302%28v=VS.85%29.aspx
莫兹:https://developer.mozilla.org/en/Determining_the_dimensions_of_elements
似乎 scrollHeight 仅在某些内容大到需要滚动时才有效,因此我假设 0 表示不需要滚动(所有内容都可见)。从上面的链接来看,也许您想要的是 clientHeight。
关于javascript - 检查元素的 scrollHeight 有时会返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8100099/
使用纯 Javascript 或 jQuery,我需要获得滚动元素的完整高度。但是 DOM 属性 scrollHeight is apparently not 100% reliable . 我设想暂
我正在创建一个指令,其中我需要三个值- 滚动到顶部 偏移高度 滚动高度 projectModule.directive('scroller', function ($window) { return
我已经使用 javascript 和 php 开发了一个聊天系统,但问题是当收到新消息或消息越来越多并且当收到新消息时它会滚动,用户必须滚动自定义才能转到底部,所以我尝试添加 javascript 以
我正在尝试让 css 过渡与我正在使用 Angular 7 开发的 Accordion 元素一起使用,以便在底部展开时使用。因为我自己不打算使用该元素,所以我使用 overflow-y: visibl
如何使用 css overflow:auto 确定分区的 scrollHeight? 我试过: $('test').scrollHeight(); $('test').height(); but th
$('#div1').scrollTop($('#div1')[0].scrollHeight) 我的意思是,为什么我们需要在所选元素后面指定 [0]。它说明什么?它似乎没有指示 div 元素的子元素
我正在尝试检索主体的 scrollHeight 以重置 id="background1"的 div 的大小。如果在我的外部 css 中我将该 id 设置为 position:absolute scro
我想在没有 jQuery 的情况下学习一些东西。这是我面临的挑战之一。 我有一个固定的 contenteditable div,当向 div 添加文本时,如果 scrollHeight 超过 clie
如 https://developer.mozilla.org/en/Determining_the_dimensions_of_elements 所述: If you need to know th
我正在检查并比较 body.offsetHeight 与 body.scrollHeight。 放大窗口大小总是会抛出相同的值,但缩小窗口有时会导致“跳跃”值,所以我的 Div 上会闪烁。在 Chro
我的问题分为两部分: 问题的第一部分: 测试一个: t1 = new Date().getTime(); for (i=0; i document.getElementById("di
我编写了这个简单的函数,它工作正常,但我正在努力让它变得更好一点。 我有一个文本区域,它会在滚动高度发生变化时调整大小,问题是当我按下回车键(即向下一行 -\n)时,高度会随着它的更新而跳变,因为滚动
我有一个 div在 td 里面标签(位于 tr 标签内)。这div有一个自定义指令需要 $watch scrollHeight元素的: My very long text
我有一段 JS 代码,判断是否有垂直滚动条,并将 CSS 类应用于元素。没什么大不了的。令我困惑的是,它似乎在做与我理解它应该做的相反的事情。这不是问题,除非它是一个错误并且在未来被修复。 代码: i
所以我正在检查一些动态生成的内容的滚动高度,以确定是否在内容中包含更多按钮。用户可以点击更多按钮来展开内容并查看里面的所有内容。然而,每次使用相同的函数时,有时它会说元素的 scrollHeight
我正在尝试检测 div 是否有可见的垂直滚动条 但是当尝试将 scrollHeight 与 clientHeight 进行比较时,我发现 scrollHeight 大了 1 if (div.sc
我有这个习惯 . 在.select_list里面我有一个 ul与 height:300px;和 overflow:hidden;当我将按钮悬停在 .select_list 中时正在向上或向下滚动。 我
有没有其他方法可以在不使用scrollHeight的情况下获取某个HTML元素的内部高度? 问题是,我有一个包含 100 条记录的表,在每条记录中,我使用 height=0 然后 height=scr
我有一个 container div,它有一个 innerdiv,后者又有多个 div 元素。最外面的容器 div 具有更大的高度,y 轴溢出。我正在尝试为 innerdiv 提供背景颜色,但这种样式
所以我正在尝试创建一种方法来检测 body 何时调整大小,然后我获取新的高度并通过邮件发送它。功能不正常的部分是当我缩小 body 尺寸时(当我删除(隐藏)一些元素使 body 缩小时)。这不会被检测
我是一名优秀的程序员,十分优秀!