Increment or decrease var in resize window

Asked

Viewed 65 times

1

Hello. I have a variable with the value 0 and a window in the size 1000. I would like you not to resize to smaller sizes, 999, 998, 997, the variable decreases to -1, -2... And in resize to higher, it increased, 1, 2... Some help?

  • 1

    Show what you are doing to get a better idea of the problem. It seems to me something simple but I may not have understood the problem.

3 answers

1

Creates a variable that stores this value and joins an event receiver for when the window changes size. You also need a way to fetch information of the current window size. I don’t quite understand in your question if you want to width (x) or the height (y). I put together an example that does this by calculating the width but with the height code as well:

var janela = 0;
function medeJanela(){
    var w = window,
        d = document,
        e = d.documentElement,
        g = d.getElementsByTagName('body')[0],
        x = w.innerWidth || e.clientWidth || g.clientWidth,
        y = w.innerHeight|| e.clientHeight|| g.clientHeight;
    janela = x - 1000;
    console.log(janela); // podes tirar isto depois, só para verificares
}
window.onresize = medeJanela;

This way the variable is updated when the window is resized.

0

You just have to decrement 1000 of the window value whenever you set the variable. For example:

var tamanhoJanela = getTamanhoJanela() - 1000;
// 1000 - 1000 =  0
// 997  - 1000 = -3
// 1003 - 1000 =  3

0

From what I understand it would be like this:

var tamanho = 1000;
$(window).resize(function() {
var h = $(window).height();
tamanho += h > tamanho ? 1 : -1;
console.log(tamanho);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.