4

私は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>
4

1 に答える 1

5

chatscroll.Paneコンストラクターとして使用するように設計されています。次のようなインスタンスを作成します。

new chatscroll.Pane('somescrollContainerId');

戻り値を変数に割り当てると、戻り値は再利用可能になります。

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

scrollContainerId渡すのは、このオブジェクトを使用するHTMLドキュメント内の要素のID(属性)idになります。DIV

で宣言する必要はありませんがwindow.onload、それでも問題はありません。コンストラクターが実行しているのは、新しいオブジェクトを作成し、thisその新しいオブジェクトに値を設定し、その中にプロパティを作成して設定bottomThresholdscrollContainerId、コンストラクターが終了したときにこの新しいオブジェクトを返すことだけです。

ドキュメントが完全に解析されるまで関数を呼び出さないように注意してください。これはactiveScroll、実際にドキュメントに組み込まれて要素を取得および操作するためです。

于 2011-12-12T17:13:53.313 に答える