Skip to content

Instantly share code, notes, and snippets.

@rssilva
Created March 15, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rssilva/9d0f1427e33c3c71e044036d9a7b7afa to your computer and use it in GitHub Desktop.
Save rssilva/9d0f1427e33c3c71e044036d9a7b7afa to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style media="screen">
.scrolling-indicator {
position: fixed;
top: 0;
right: 0;
background-color: red;
padding: 1em;
color: white;
opacity: 0;
transition: opacity 1s;
}
.scrolling-indicator::after {
content: 'scrolling'
}
.scrolling-indicator.active {
opacity: 1;
transition: none;
}
button {
position: fixed;
top: 0;
left: 0;
padding: 1em;
background-color: green;
color: white;
border: none;
font-size: inherit;
font-family: inherit;
}
</style>
</head>
<body>
</body>
<script type="text/javascript">
let html = `
<div class='scrolling-indicator'></div>
<button>remove event listener</button>
`;
for ( let i = 0; i < 999; i += 1 ) {
html += `<p>${i}</p>`;
}
document.body.innerHTML = html;
let indicator = document.createElement( 'div' );
indicator.classList.add( 'scrolling-indicator' );
document.body.appendChild( indicator );
let timeout = null;
function onscroll () {
clearTimeout( timeout );
indicator.classList.add( 'active' );
timeout = setTimeout( () => {
indicator.classList.remove( 'active' );
}, 100 );
}
window.addEventListener( 'scroll', onscroll, {
passive: true
});
document.querySelector( 'button' ).addEventListener( 'click', () => {
window.removeEventListener( 'scroll', onscroll, {
passive: true
});
alert( 'event listener should have been removed' );
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment