window.addEventListener('DOMContentLoaded', function() {
var elem = document.querySelector('.opacTop'),
body = document.body,
html = document.documentElement;
isVisible = true,
maxHeight = 200;
window.addEventListener('scroll', function() {
var scrollHeight = (html.scrollHeight || body.scrollHeight) - this.innerHeight,
scrollTop = html.scrollTop || body.scrollTop || 0,
value = 1 - 1 / Math.min(scrollHeight, maxHeight) * Math.min(scrollTop, maxHeight);
elem.style.opacity = value;
if(isVisible != !!value) {
elem.style.display = isVisible ? 'none' : '';
isVisible = !isVisible;
}
});
});
Вот так точно работать будет.