如何使用 JavaScript 调用此函数?
我只是使用基本级别的 javascript。今天,我发现了以下内容,用于在新数据添加到 DIV 时向下滚动 DIV 层。我无法理解如何调用该函数。是否要使用window.onload
函数来使用?或任何其他。我应该在哪里声明 DIV 名称?
代码如下。
var chatscroll = new Object();
chatscroll.Pane =
function(scrollContainerId)
{
this.bottomThreshold = 25;
this.scrollContainerId = scrollContainerId;
}
chatscroll.Pane.prototype.activeScroll =
function()
{
var scrollDiv = document.getElementById(this.scrollContainerId);
var currentHeight = 0;
if (scrollDiv.scrollHeight > 0)
currentHeight = scrollDiv.scrollHeight;
else
if (objDiv.offsetHeight > 0)
currentHeight = scrollDiv.offsetHeight;
if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold)
scrollDiv.scrollTop = currentHeight;
scrollDiv = null;
}
更新1:
<script type="text/javascript">
var chatscroll = new Object();
var chatScrollPane = new chatscroll.Pane('div1');
chatScrollPane.activeScroll()
chatscroll.Pane = function (scrollContainerId) {
this.bottomThreshold = 25;
this.scrollContainerId = scrollContainerId;
}
chatscroll.Pane.prototype.activeScroll = function () {
var scrollDiv = document.getElementById(this.scrollContainerId);
var currentHeight = 0;
if (scrollDiv.scrollHeight > 0)
currentHeight = scrollDiv.scrollHeight;
else
if (objDiv.offsetHeight > 0)
currentHeight = scrollDiv.offsetHeight;
if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold)
scrollDiv.scrollTop = currentHeight;
scrollDiv = null;
}
</script>
I am just working with basic level of javascripts. Today I found the below and for scrolling down DIV layer when new data adds to DIV. I couldn't understand how to Call the function. Is it to be used using window.onload
function? or any other. And where should I declare the DIV name?
Code follows.
var chatscroll = new Object();
chatscroll.Pane =
function(scrollContainerId)
{
this.bottomThreshold = 25;
this.scrollContainerId = scrollContainerId;
}
chatscroll.Pane.prototype.activeScroll =
function()
{
var scrollDiv = document.getElementById(this.scrollContainerId);
var currentHeight = 0;
if (scrollDiv.scrollHeight > 0)
currentHeight = scrollDiv.scrollHeight;
else
if (objDiv.offsetHeight > 0)
currentHeight = scrollDiv.offsetHeight;
if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold)
scrollDiv.scrollTop = currentHeight;
scrollDiv = null;
}
Update 1:
<script type="text/javascript">
var chatscroll = new Object();
var chatScrollPane = new chatscroll.Pane('div1');
chatScrollPane.activeScroll()
chatscroll.Pane = function (scrollContainerId) {
this.bottomThreshold = 25;
this.scrollContainerId = scrollContainerId;
}
chatscroll.Pane.prototype.activeScroll = function () {
var scrollDiv = document.getElementById(this.scrollContainerId);
var currentHeight = 0;
if (scrollDiv.scrollHeight > 0)
currentHeight = scrollDiv.scrollHeight;
else
if (objDiv.offsetHeight > 0)
currentHeight = scrollDiv.offsetHeight;
if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold)
scrollDiv.scrollTop = currentHeight;
scrollDiv = null;
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
chatscroll.Pane
被设计用作构造函数。您可以构造这样的实例:如果将返回值分配给变量,则返回值将变得可重用。
您传入的
scrollContainerId
将是您要使用此对象的 HTML 文档中的DIV
元素的 ID(id
属性) 。您不需要在
window.onload
中声明它,但这肯定不会有什么坏处。构造函数所做的就是创建一个新对象,将this
值设置为该新对象,创建并设置其中的bottomThreshold
和scrollContainerId
属性,然后在构造函数完成时返回这个新对象。只需确保在文档完全解析之前永远不要调用
activeScroll
函数,因为它实际上会进入文档来检索和操作元素。chatscroll.Pane
is designed to be used as a constructor. You would construct an instance like this:The returned value becomes reusable if you assign it to a variable.
The
scrollContainerId
you pass in will be the ID (id
attribute) of theDIV
element in your HTML document that you want to use this object with.You shouldn't need to declare it in your
window.onload
, but that certainly won't hurt. All the constructor is doing is creating a new object, setting thethis
value to that new object, creating and settingbottomThreshold
andscrollContainerId
properties therein, then returning this new object when the constructor is finished.Just make sure you never call the
activeScroll
function until after the document is fully parsed, since that actually goes into your document to retrieve and manipulate elements.