0

Is there a way in JavaScript or jQuery to call a function when the window is resized under 480px?

VisioN
  • 143,310
  • 32
  • 282
  • 281
Aessandro
  • 5,517
  • 21
  • 66
  • 139
  • duplicate of http://stackoverflow.com/questions/662726/jquery-window-resized-from-dom-fire-event?rq=1 Use window.outerWidth or window.innerWidth to get dimensions – Dmitry Pashkevich Jun 14 '12 at 08:45
  • 1
    @DmitryPashkevich That question is asking something completely different to this one. – Anthony Grist Jun 14 '12 at 08:49
  • Essentially the questions (and answers) are the same. Here's another *extremely similar* thread http://stackoverflow.com/questions/2996431/javascript-detect-when-a-window-is-resized?rq=1 Personally, I get pissed when I search for something on SO and get a bunch of nearly identical threads... – Dmitry Pashkevich Jun 14 '12 at 09:54

4 Answers4

6

With jQuery you can do it as follows:

$(window).on("resize", function() {
    if ($(this).width() < 480) {     // or $(this).height() for height check
        // function();
    }
});
VisioN
  • 143,310
  • 32
  • 282
  • 281
0

You can use resize event on Window (http://api.jquery.com/resize/), then check the size of the window, and if its under 480px, call your method.

ruffen
  • 1,695
  • 2
  • 25
  • 51
0

Did you ask for

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script>
        var counter=1;
        $(window).resize(function()
        {
            alert($(this).width());
            if ($(this).width() < 1000)
            {
                counter = incCounter(counter);
            }
            alert("counter = " + counter);
        });
        function incCounter(intvari)
        {
            backValue = intvari;
            backValue++;
            return backValue;
        }
    </script>
</head>
<body>
    Text
</body>
</html>

?

Reporter
  • 3,897
  • 5
  • 33
  • 47
  • 1
    the function needs to be called as soon as the window is resized under 480px. Thanks – Aessandro Jun 14 '12 at 08:47
  • basically I have a jQuery accordion and I want to make it work only for smartphones, so I was thinking of writing a function that would call the jQuery accordion only under a certain width. – Aessandro Jun 14 '12 at 08:48
  • I updated my answer. Honestly your question is a little bit too vargue. – Reporter Jun 14 '12 at 09:25
0
var w=$(window).width();
if(w<480)
{
alert(w);
}
David
  • 15,894
  • 22
  • 55
  • 66
saun4frsh
  • 383
  • 1
  • 4
  • 21