1

Is it possible to subtract 1 each time I were to resize my browser smaller then 1600x1020?

I know i need to start with a resize function but how do I go about store the new value where I can use in a new function?

 var resizeNum = 100;
 if (window.width() < 1600 && width.height()){
   resizeNum - 1
  }

Not sure how to go about this.

user992731
  • 3,400
  • 9
  • 53
  • 83

5 Answers5

2
var resizeNum = 100;
$(window).resize(function() {
    if(($(this).width() < 1600) && ($(this).height())) {
        resizeNum -= 1;
    }
});

This will call every time the window is resized. As it is resized, resizeNum will decrease by 1 each time.

ayyp
  • 6,590
  • 4
  • 33
  • 47
2

Since you clearly want to know when the browser is less than 1600x1020 I check both height and width below. Also there are 4 ways of subtracting 1 from a variable:

//Sets number to -1 after any other use of the variable
var number = 0;
number--; //number = -1;

var number = 0;   
number-= 1; //number = -1;

var number = 0;   
number = number - 1; //number = -1;

//Sets number to -1 before any other use of the variable
var number = 0;   
--number; //number = -1;

DEMO: jsFiddle

JS:

var resizeNum = 100;

$(window).resize(function () {
    if ($(window).width() < 1600 || width.height() < 1020) {
        resizeNum--;
    }
    console.log(resizeNum);
});

Please remove the console.log when you are using this code it is just to prove it works.

abc123
  • 17,855
  • 7
  • 52
  • 82
1

you can do it like this with using jquery you need to add jquery file first.

// global variable
var resizeNum = 100;

// to check browser small than 1600 x 1020
var flag = 0;

$(document).ready(function(){
    decrementNum();
});

function decrementNum()
{
    var wW = $(window).width();
    var wH = $(window).height();

    if(wW < 1600 && wH < 1020 && flag == 0)
    {
        resizeNum--;
        flag = 1;
    }
    else if(wW > 1600 && wH > 1020)
    {
        flag = 0;
    }
}

$(window).resize(function(){
     decrementNum();
});
Jay Patel
  • 5,783
  • 1
  • 17
  • 20
0

There are two ways to do this. Either, you can declare the var at whatever your outermost layer is (certainly not in the function itself - possibly as far out as the page) or you can learn to use jQuery (which uses some rather nicely designed and fully unscoped variables). Also, the line needs to be resizeNum = resizeNum-1 rather than just resizeNum -1

Ben Barden
  • 2,001
  • 2
  • 20
  • 28
0

go through this http://api.jquery.com/width. it might be bit of help

use this

$(window).width();   // returns width of browser viewport 
$(document).width(); // returns width of HTML document

and also this

parseInt(resizeNum)-1
CodeHunter
  • 378
  • 1
  • 3
  • 16