简体   繁体   English

"如何获取 div 的实际滚动高度(例如,内部文本大小)"

[英]How to get the real scroll height of div (say, inner text size)

I'm trying to make a auto-scrolling div that go to its top when it reaches the end.我正在尝试制作一个自动滚动的 div,当它到达末尾时它会到达顶部。 But it doesn't work...但它不起作用...

function    scrollPannel()
{
    var pannel = document.getElementById('pannel');
    if (typeof scrollPannel.count == 'undefined')
    {
        scrollPannel.count = 0;
    }
    else
    {
        scrollPannel.count += 2;
    }
// trouble is here
    if ((scrollPannel.count - pannel.scrollHeight) > pannel.clientHeight)
    {
        scrollPannel.count = 0;
    }
    pannel.scrollTop = scrollPannel.count;
    setTimeout('scrollPannel()', 500);
}

HTML: HTML:

<div id='pannel'  style="height:200px;overflow:auto" onmouseover="sleepScroll()">
    <p>...</p><!-- long text -->
</div>

And after, I will need to find how to stop scrolling when "onmouseover" occures.之后,我需要找到如何在“onmouseover”发生时停止滚动。

EDIT: I did not explained the problem clearly.编辑:我没有清楚地解释这个问题。 In fact, I have tried something like:事实上,我已经尝试过类似的东西:

if (scrollPannel.count > pannel.scrollHeight)
{
    scrollPannel.count = 0;
}

The problem is that scrollHeight seems greater than div inner text.问题是 scrollHeight 似乎大于 div 内部文本。 So it makes a lot of time to return to the top.因此,返回顶部需要很多时间。

So I need an element property of which I could use the value to compare with my count variable.所以我需要一个元素属性,我可以使用它的值与我的计数变量进行比较。 However I don't know Javascript a lot and I could not find anything.但是我不太了解 Javascript,也找不到任何东西。 I hope it is as well simple as I think of it.我希望它和我想的一样简单。

Try: 尝试:

// calculate max scroll top position (go back to top once reached)
var maxScrollPosition = element.scrollHeight - element.clientHeight;
// example
element.scrollTop = maxScrollPosition;

That should do what you need. 那应该做你需要的。

You could try using the scrollHeight property. 您可以尝试使用scrollHeight属性。

https://developer.mozilla.org/en-US/docs/Web/API/element.scrollHeight https://developer.mozilla.org/en-US/docs/Web/API/element.scrollHeight

The solution I have involves jQuery, hope that's not a problem. 我的解决方案涉及jQuery,希望这不是问题。

JavaScript: JavaScript的:

var timeout;
function scrollPannel()
{
    var divHeight = $("#pannel").height() / 2;
    var scrollCount = $("#pannel").scrollTop();
    var scrollHeight = $("#inside").height() - 20 - divHeight;
    scrollCount += 2;

    if ((scrollCount - scrollHeight) > 0)
    {
        scrollCount = 0;
    }
    $("#pannel").scrollTop(scrollCount);
    timeout = window.setTimeout(scrollPannel(), 100);
}

function scrollStop() {
    window.clearTimeout(timeout);
}

HTML: HTML:

<div id='pannel' onmouseover="scrollStop();" onmouseout="scrollPannel();">
    <p id="inside"></p><!-- long text -->
</div>

Explanation: jQuery's .height() of the inside element <p> gives us the actual height you're looking for, but it's not enough for reaching the bottom, since that happens before we reach the element's height. 说明:内部元素<p> jQuery的.height()给出了你正在寻找的实际高度,但它不足以到达底部,因为这是在我们达到元素高度之前发生的。 A little investigation shows that the "top" of scrollTop() is about half way inside the original div 's height. 一个小小的调查显示, scrollTop()的“顶部”大约是原始div高度的一半。 You may need to play around with divHeight to get the exact results you're looking for. 您可能需要使用divHeight来获得您正在寻找的确切结果。

Of course, I also included a method for stopping and continuing scrolling. 当然,我还提供了一种停止和继续滚动的方法。

Good luck! 祝好运!

你应该使用scrollHeight属性,但要调用它,你需要使用这样的索引:

$('#pannel')[0].scrollHeight;

If you set the scrollTop and scrollLeft to really high silly values they only ever get set as their maximum allowed values.如果您将 scrollTop 和 scrollLeft 设置为非常高的愚蠢值,它们只会被设置为其最大允许值。 You can then use them to work out the scroll center.然后,您可以使用它们来计算滚动中心。

See snippet example.请参阅片段示例。

 var mB = document.getElementById('myBox'); var mR = document.getElementById('myResult'); // Set the top and the left to silly values mB.scrollTop = 99999999; mB.scrollLeft = 99999999; // They will only end up being set as their max mR.innerHTML = "maxTop="+mB.scrollTop+"<br>maxLeft="+mB.scrollLeft; // Now take the max values and divide by 2 to scroll back to middle. mB.scrollTop = mB.scrollTop/2; mB.scrollLeft = mB.scrollLeft/2;
 #myBox{ overflow:auto; } #myContent{ border:1px solid black; background-color:red; }
 <div id='myBox' style='width:300px;height:300px'> <div id='myContent' style='width:500px;height:800px;line-height:800px;'><center>I am the center of the content</center></div> </div> <div id='myResult'></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM