JavaScript - window.innerHeight - Safari Browser

1

The window.innerHeight does not work in the Safari browser.

When I scroll on the page and the element arrives at a certain place on the page, it adds a class to HTML, in all other browsers that code works. However in Safari I could not make the elements do not get the specified class inside the code. I believe the problem is in this window.innerHeight, I need some help to solve this problem.

(function(){

//Declarando as variáveis
var target = document.querySelectorAll('.border-bottom');
var classAnimacao = 'animacao-border-bottom';
var offset = window.innerHeight * 3/4; //Calculo da tamanho da tela para animação começar
var animes = document.querySelectorAll('[data-anime]');


//Função scrollTop
function scrollTop() {
    //Distancia do topo da tela
    var documentTop = document.documentElement.scrollTop;
    // Alvo (border-bottom)
    target.forEach(function(element){
        var itemTop = element.offsetTop;

        if (documentTop > itemTop - offset) {
            element.classList.add(classAnimacao);
        } else {
            element.classList.remove(classAnimacao);
        }            
    });

    animes.forEach(function(element){
        var anime = element.offsetTop;

        if(documentTop > anime - offset) {
            element.classList.add('animation');
        } else {
            element.classList.remove('animation');
        }
    });
}
// Adiciona uma vez quando atualizada a página
scrollTop();
// Ativando função no rolar do scroll (rolar da página)
document.addEventListener('scroll', debounce(function(){
    scrollTop();
}, 200));
}());
    
asked by anonymous 26.08.2018 / 22:57

1 answer

0

The problem is not in innerHeight , Safari recognizes this property.

The document.documentElement.scrollTop property is not recognized by Safari and will always return 0 .

To get the scroll in Safari use window.pageYOffset . So you can use both methods for compatibility both in Safari and in other browsers:

var documentTop = document.documentElement.scrollTop || window.pageYOffset;

With this, the browser will return the value of the compatible method. The || operator will return what is valid, the right OR the left.

    
28.08.2018 / 00:56