JavaScriptでスクロールバーを無効にする方法は?
-
05-07-2019 - |
質問
Internet Explorer 7のみでモーダルウィンドウを表すdivを表示するときは、ブラウザのスクロールバーをロックする必要があります。
グーグルで document.body.style.overflow = 'hidden'
を使用できることがわかりましたが、これはIE7では機能しません。また、 document.body.scroll =" no"
を試してみましたが、これはスクロールバーの上にマウスを置いた後でのみ機能します:-S
より良いアプローチを知っている人はいますか?
サンクス
解決
さまざまな質問(他のコメントの質問を含む)に答えるために、間違った位置決め方法を使用していると思います。
position:fixed
を試してください。絶対ビューポートに対する相対値を除き、基本的には position:absolute
と同じです。つまり、ユーザーがスクロールしても、アイテムは画面上の同じ場所に留まります。
これを念頭に置いて、 position:fixed
オーバーレイをレイアウトできます。その中で、 position:absolute
(または、必要に応じて fixed
を再度使用できます-違いはありませんはありません)モーダルボックス。
他のヒント
モーダルオーバーレイdivを本文に合わせて設定します。スクロールしても、すべてがその下に隠れているため、何もできません。
overflow:hidden
を使用してスクロールバーを非表示にすることもできます。これにより、ユーザーはスクロールバーを表示せず、スクロールするようになりません:)
これはあなたを助けることができます:
documentOBJ = {
/*Width and Height of the avaible viewport, what you'r seeing*/
window : {
x : function(){return (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth; },
y : function(){return (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight;}
},
/*Scroll offset*/
scroll : {
x : function(){return ( document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft; },
y : function(){return ( document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop; }
},
/*Height and width of the total of the xhtml in a page*/
page : {
x : function(){return (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; },
y : function(){return (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight; }
},
pointer : {}
}
所属していません StackOverflow