2008年11月30日日曜日

スクロール量取得関数

以降は、共通関数群です。
まず、水平スクロール量を取得する関数です。
マウスカーソル位置を取得する際に使っています。


cc_huu_tools_bp_20081024.getHorizontalScroll = function() {
  if (window.innerWidth) {
    // All browsers but IE
    return window.pageXOffset;
  } else if (document.documentElement && document.documentElement.clientWidth) {
   // IE6 when there is a DOCTYPE
    return document.documentElement.scrollLeft;
  } else if (document.body.clientWidth) {
   // IE4, IE5, and IE6 without a DOCTYPE
    return document.body.scrollLeft;
  }
};


ブラウザやブラウザのバージョンによって、取得方法が違いますので、プロパティの有無で判断し、それぞれ適切なものを返します。

次は、垂直スクロール量を取得する関数です。


cc_huu_tools_bp_20081024.getVerticalScroll = function() {
  if (window.innerWidth) {
    // All browsers but IE
    return window.pageYOffset;
  } else if (document.documentElement && document.documentElement.clientWidth) {
   // IE6 when there is a DOCTYPE
    return document.documentElement.scrollTop;
  } else if (document.body.clientWidth) {
   // IE4, IE5, and IE6 without a DOCTYPE
    return document.body.scrollTop;
  }
};


こちらも、ブラウザやブラウザのバージョンによって、取得方法が違いますので、プロパティの有無で判断し、それぞれ適切なものを返します。

0 件のコメント: