ウインドウのスクロール量を取得する

サンプルソース

<script language="Javascript">
<!--
//通常の場合
alert(document.documentElement.scrollTop);
alert(document.documentElement.scrollLeft);

//IE(互換モード)の場合
alert(document.body.scrollTop);
alert(document.body.scrollLeft);
-->
</script>

解説

document.documentElement.scrollTopで縦のスクロール量を、document.documentElement.scrollLeftで横のスクロール量を取得します。

IE(互換モード)の場合、値が取得できないため、document.body.scrollTopdocument.body.scrollLeftを使用します。

関連する内容



Copyright (C) JavaScript@Workshop All rights reserved.