スクロールバーの幅を取得する

function getScrollbarWidth() {

	// Creating invisible container
	const outer = document.createElement('div');
	outer.style.visibility = 'hidden';
	outer.style.overflow = 'scroll'; // forcing scrollbar to appear
	outer.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps
	document.body.appendChild(outer);

	// Creating inner element and placing it in the container
	const inner = document.createElement('div');
	outer.appendChild(inner);

	// Calculating difference between container's full width and the child width
	const scrollbarWidth = (outer.offsetWidth - inner.offsetWidth);

	// Removing temporary elements from the DOM
	outer.parentNode.removeChild(outer);

	return scrollbarWidth;
}

参照元 Getting scroll bar width using JavaScript


レシピブック(レシピ登録アプリ)
手書きノート(メモ登録アプリ)

投稿日

カテゴリー:

ブログランキング・にほんブログ村へ

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です