Mad*_*er. 4 html javascript declaration function invocation
我只是处理基本级别的javascripts.今天我找到了以下内容,并在新数据添加到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;
}
Run Code Online (Sandbox Code Playgroud)
更新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>
Run Code Online (Sandbox Code Playgroud)
chatscroll.Pane旨在用作构造函数.您将构建一个这样的实例:
new chatscroll.Pane('somescrollContainerId');
Run Code Online (Sandbox Code Playgroud)
如果将返回值分配给变量,则返回值将变为可重用.
var chatScrollPane = new chatscroll.Pane('somescrollContainerId');
Run Code Online (Sandbox Code Playgroud)
在scrollContainerId你传递将是ID(id的属性)DIV,你想使用这个对象的HTML文档中的元素.
你不应该在你的声明中声明它window.onload,但肯定不会受到伤害.所有构造函数做的是创建一个新的对象,设置this值到新的对象,创建和设置bottomThreshold和scrollContainerId属性在其中,那么当构造完成后返回这个新对象.
只需确保activeScroll在文档完全解析之后才调用该函数,因为它实际上会进入您的文档以检索和操作元素.