簡體   English   中英

如何使用JavaScript調用此函數?

[英]How do I invoke this function with JavaScript?

我只是處理基本級別的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;
    }

更新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>

chatscroll.Pane旨在用作構造函數。 您將構建一個這樣的實例:

new chatscroll.Pane('somescrollContainerId');

如果將返回值分配給變量,則返回值將變為可重用。

var chatScrollPane = new chatscroll.Pane('somescrollContainerId');

您傳入的scrollContainerId將是HTML文檔中要與此對象一起使用的DIV元素的ID( id屬性)。

你不應該在window.onload聲明它,但這肯定不會受到傷害。 所有構造函數都在創建一個新對象, this值設置為該新對象,在其中創建並設置bottomThresholdscrollContainerId屬性,然后在構造函數完成時返回此新對象。

只需確保在文檔完全解析之后才調用activeScroll函數,因為它實際上會進入文檔以檢索和操作元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM