2017-11-22 11 views
0

要素のオフセットをどのように検出するのですか? そして、他の要素のプロパティ..Javascript - 要素のoffsetTopの変更を検出するにはどうすればよいですか?

は、ここで私はdivテストした上MutationObserverを配置しようとするスニペットですが、私は(それにいくつかのテキストを足すことで)より大きな上記のヘッダdivを作るとき、観察者はトリガーされることはありません。

// get the test element 
 
var testElement = document.getElementById('test'); 
 
// build the observer 
 
var observer = new MutationObserver(function(mutations) { 
 
    console.log("Observer triggered! YES!!"); 
 
}) 
 
// place it, listen to attributes and datas changes 
 
.observe(testElement, { 
 
    attributes: true, 
 
    characterData: true, 
 
}); 
 

 

 
// --- test --- 
 
console.log("The test div is at " + testElement.offsetTop + "px from the top."); 
 
console.log("Writting in the header div (to make it bigger).."); 
 

 
document.getElementById("header").innerText = "Header text changed"; 
 

 
console.log("The test div is at " + testElement.offsetTop + "px from the top after header text changed.");
<div id="header"></div> 
 
<div id="test"></div>

答えて

0

それの位置を確認するために厄介であるポールを使用して、あるいは単に要素をタップ以外にそれを行う簡単な方法はありません。

このブログがあります:Jquery Plug-in

それはIEとFirefoxのための優れたソリューションを提供しますが、ウェブキットと公正ません。

+1

私はポールに受け入れられません。 あなたのお手伝いをありがとうございます。 – Yairopro

関連する問題