ウィンドウロード時やサイズ変更時に何らかの処理を行う機能があります。何かが起こるとすぐに私はwindow.resize
イベントリスナーをもう必要としないので、私はresize event listener
を削除しようとしましたが、うまく動作しません。 div.blubb
が一度オレンジ色に変更ウィンドウremove eventListener resize function
blubb = function(element) {
\t this.element = element;
this.blubber = function() {
document.querySelector('.data').innerHTML = window.innerWidth + 'px';
if(window.innerWidth < 800) {
this.element.style.backgroundColor = 'orange';
// here remove EventListener
// is not working
window.removeEventListener('resize', (function() { this.blubber() }).bind(this));
} else {
this.element.style.backgroundColor = 'powderblue';
}
};
this.blubber();
window.addEventListener('resize', (function() { this.blubber() }).bind(this));
};
blubb(
\t document.querySelector('.blubb')
);
.blubb {
width: 300px;
height: 300px;
background-color: powderblue;
}
<h1>Change visible area size and reload</h1>
<p>Should be once above 800px and once below 800px</p>
<div class="data"></div>
<div class="blubb"></div>
もしそうなら、私は私が試したイベントリスナ削除したいと思います:
window.removeEventListener('resize', (function() { this.blubber() }).bind(this));
をしかし、私はイベントが削除されていない参照してくださいサイズ変更します。私は間違って何をしていますか?
それはワットorks https://jsfiddle.net/22oayaLc/3/、ありがとうございます! – caramba