タグ別アーカイブ: 調べる

指定した要素を下にスクロールさせる/スクロールした値を取得する – scrollTop

指定した要素を下にスクロールさせる/スクロールした値を取得するには scrollTop プロパティを使用します。

scrollTop:取得する場合の書き方

取得する要素.scrollTop;

scrollTop:変更する場合の書き方

変更する要素.scrollTop = 変更する数理;

例1:#element が下にスクロールされている値をクリックして取得し表示する(#text へ表示)。

css で親の #example 要素よりも子要素が縦のサイズが大きく指定されていいます。overflow: auto; が #example には指定されている為縦にスクロールできるようになっています。

HTML

<div id="example">
    <div class="textbox">
        <p>↓ボタンをクリックでスクロール</p>
        <p>↓</p>
        <p>↓ボタンをクリックでスクロール</p>
        <p>↓</p>
    </div>
</div>
<button id="sc_btn">クリックで下50pxずつへスクロールします</button>

CSS

#example{
	width: 500px;
	height:200px;
	padding: 10px;
	background: #eee;
	border: solid 1px #666;
	overflow: auto;
}
.textbox{
	height: 1000px;
	margin: 0;
	padding: 0;
}
p{
	margin: 0 0 100px;
}

JavaScript

ボタン #sc_btn をクリックすると#example が下に何pxスクロールされたかを #text へ表示します。

window.onload = init;
function init(){

var element = document.getElementById('example');
var btn = document.getElementById('sc_btn');
var text = document.getElementById('text');

    btn.onclick = function(){
        var elementTop = element.scrollTop ;
        text.innerHTML = elementTop;
    };
    
};

サンプル

下のグレーのボックス #example の横にでているスクロールバーを下に動かし、ボタンをクリックしてみて下さい。現在のスクロールされている値が表示されます。

例2:#element の下のボタンをクリックして下に 50px ずつスクロールさせる。

HTML

<div id="example">
    <p>スクロールしてボタンをクリック→</p>
</div>
<button id="sc_btn">左へスクロールした値を取得します→</button>
<p id="text">0</p>

CSS

#example{
	width: 500px;
	height:100px;
	padding: 10px;
	background: #eee;
	border: solid 1px #666;
	overflow: auto;
}
p{
	width: 1000px;
	margin: 0;
	padding: 0;
}

JavaScript

ボタン #sc_btn をクリックすると #example のスクロール移動値が 50px ずつ足されて下へ移動します。

window.onload = init;
function init(){

var element = document.getElementById('example');
var btn = document.getElementById('sc_btn');

    btn.onclick = function(){
        element.scrollTop += 50;
    };
    
};  

サンプル

下のグレーのボックス #example 下のボタンをクリックすると下へ50pxずつスクロールします。

指定した要素を左側にスクロールさせる/スクロールした値を取得する – scrollLeft

指定した要素を左側にスクロールさせる/スクロールした値を取得するには scrollLeft プロパティを使用します。

scrollLeft:取得する場合の書き方

取得する要素.scrollLeft;

scrollLeft:変更する場合の書き方

変更する要素.scrollLeft = 変更する数理;

例1:#element が左にスクロールされている値をクリック(#sc_btn をクリック)して取得して表示する(#text へ表示)。

css で親の #example 要素よりも子の p 要素が横幅が大きく指定されていいます。overflow: auto; が #example には指定されている為横にスクロールできるようになっています。

HTML

<div id="example">
    <p>スクロールしてボタンをクリック→</p>
</div>
<button id="sc_btn">左へスクロールした値を取得します→</button>
<p id="text">0</p>

CSS

#example{
	width: 500px;
	height:100px;
	padding: 10px;
	background: #eee;
	border: solid 1px #666;
	overflow: auto;
}
p{
	width: 1000px;
	margin: 0;
	padding: 0;
}

JavaScript

ボタン #sc_btn をクリックすると#example が左に何pxスクロールされたかを #text へ表示します。

window.onload = init;
function init(){

var element = document.getElementById('example');
var btn = document.getElementById('sc_btn');
var text = document.getElementById('text');

    btn.onclick = function(){
        var elementLeft = element.scrollLeft;
        text.innerHTML = elementLeft;
    };
};  

サンプル

下のグレーのボックス #example の下にでているスクロールバーを左に動かし、ボタンをクリックしてみて下さい。現在のスクロールされている値が表示されます。

例2:#element の下のボタンをクリックして左に 50px ずつスクロールさせる。

css で親の #example 要素よりも子の p 要素が横幅が大きく指定されていいます。overflow: auto; が #example には指定されている為横にスクロールできるようになっています。

HTML

<div id="example">
    <p>スクロールしてボタンをクリック→</p>
</div>
<button id="sc_btn">ボタンをクリックで左へスクロールします→</button>

CSS

#example{
	width: 500px;
	height:100px;
	padding: 10px;
	background: #eee;
	border: solid 1px #666;
	overflow: auto;
}
p{
	width: 1000px;
	margin: 0;
	padding: 0;
}

JavaScript

ボタン #sc_btn をクリックすると #example のスクロール移動値が 50px ずつ足されて移動します。

window.onload = init;
function init(){

var element = document.getElementById('example');
var btn = document.getElementById('sc_btn');

    btn.onclick = function(){
        element.scrollLeft += 50;
    };
    
};   

サンプル

下のグレーのボックス #example 下のボタンをクリックすると左へ50pxずつスクロールします。

指定した要素の親要素のうち一番近い position の指定がある要素を取得する – offsetParent

指定した要素の親要素のうち一番近い、CSS の position が relative、absolute、fixed の何れかが設定されている要素を取得するには、offsetParent プロパティを使用します。

offsetParent:書き方

調べたい要素.offsetParent;

例:#element の親要素以上で position が設定されている要素を取得する。

HTML

<div>
    <section>
        <h1>exampleTitle</h1>
        <p id="example">Text1のテキスト</p>
        <p>Text2のテキスト</p>
    </section>
</div>

CSS

div{
	position:relative;
}
#example{
	margin: 15px;
	background: #ccc;
}

JavaScript

var element = document.getElementById('example');
var elementParent = element.offsetParent  ; 
console.log(elementParent);  

結果

一番近い要素で position:relative が設定されているのが div なので div要素が返されました。
#element の親要素以上で position が設定されている要素を取得した結果

指定した要素のドュメント左からの値を取得する – offsetLeft

指定した要素のドュメント左からの値を取得するには offsetLeft プロパティを使用します。ピクセル単位となます。

offsetLeft:書き方

調べたい要素.offsetLeft;

例:#element の左からの位置を取得する。

HTML

<div id="example">
    <p>Text1のテキスト</p>
    <p>Text2のテキスト</p>
</div>

CSS

body{
	margin: 0;
	padding: 0;
}
#example{
	margin: 15px;
}

JavaScript

var element = document.getElementById('example');
var elementleft = element.offsetLeft  ; 
console.log(elementleft); 

結果

margin の値が左側から15px となっているので、15px と値が返されました。
#element の左からの位置を取得した結果

指定した要素のドュメント上部からの値を取得する – offsetTop

指定した要素のドュメント上部からの値を取得するには offsetTop プロパティを使用します。ピクセル単位となます。

offsetTop:書き方

調べたい要素.offsetTop;

例:#element の上部からの位置を取得する。

HTML

<div id="example">
    <p>Text1のテキスト</p>
    <p>Text2のテキスト</p>
</div>

CSS

#example{
	margin: 15px;
}

JavaScript

var element = document.getElementById('example');
var elementTop = element.offsetTop  ; 
console.log(elementTop);  

結果

margin の値がトップから15px となっているので、15px と値が返されました。
#element の上部からの位置を取得した結果

指定した要素のスタイルシートで設定されたプロパティと値を取得する ー getComputedStyle

指定した要素のスタイルシートで設定されたプロパティと値を調べるには getComputedStyle メソッドを使用します。IE ではバージョン9以上で対応しています。

getComputedStyle:書き方

window.getComputedStyle(調べたい要素);

古いIEでの取得方法

古いIEでは以下のようにすることで取得できます。

調べたい要素.currentStyle;

参考:
Window.getComputedStyle() – Web API Interfaces | MDN
getComputedStyle について調べてたら深みにハマったのでメモ – IT戦記

例:#example に設定されている style を全て取得する。

HTML

<p id="example">Text1のテキスト</p>

JavaScript

var element = document.getElementById('example');
var elementstyle = window.getComputedStyle(element);
console.log(elementstyle);

結果


elementstyle の中にはスタイルに関する全ての情報が入っています。例2のようにスタイルのプロパティを指定して取得するとよいでしょう。

例2:#example に設定されている padding 値を取得する。

var element = document.getElementById('example');
var elementstyle = window.getComputedStyle(element);
console.log(elementstyle.padding);

結果


padding の値のみが返されました。

ノードの要素名を調べる ー nodeName

指定したノードの要素名を調べるには nodeName プロパティを使用します。ノードの集合などから特定のノードを取り出したい場合などに使用します。

nodeName:書き方

調べたいノード.nodeName;

nodeType では文字列で返されますが、異なる種類のノードがかえってくることがあります。
例外のノードの戻り値は以下サイトを参照下さい。

参考:Node.nodeName – Web API インターフェイス | MDN

例:#example のノードの要素名を取得する。

HTML

<div>
    <p id="example">Text1のテキスト</p>
    <p>Text2のテキスト</p>
    <p>Text3のテキスト</p>
</div>

JavaScript

var element = document.getElementById('example');
var elementname = element.nodeName;
console.log(elementname);

結果

#example のノードの要素名を取得した結果
p を返されているので、#example のノード名は P要素です。

ノードの種類を調べる ー nodeType

指定したノードの種類を調べるには nodeType プロパティを使用します。ノードの集合などから特定のノードを取り出したい場合などに使用します。

nodeType:書き方

調べたいノード.nodeType;

ノードタイプ一覧

nodeType で返されるのは、以下のあてはまるノードタイプの数値になります。

1 ← 要素ノード
2 ← 属性ノード
3 ← テキストノード
4 ← CDATAセクション
5 ← 実体参照ノード
6 ← 実体宣言ノード
7 ← 処理命令ノード
8 ← コメントノード
9 ← 文書ノード
10 ← 文書型宣言ノード
11 ← 文書の断片
12 ← 記述宣言ノード

参考:Node.nodeType – Web API インターフェイス | MDN

例:#example のノードタイプを取得する。

HTML

<div>
    <p id="example">Text1のテキスト</p>
    <p>Text2のテキスト</p>
    <p>Text3のテキスト</p>
</div>

JavaScript

var element = document.getElementById('example');
var elementType = element.nodeType;
console.log(elementType);

結果

#example のノードタイプを取得した結果
1 を返されているので、#example のノードタイプは要素ノードということになります。