how to call function when width is below 600px using jQuery and call it again when windows size changes?


I haven't worked very much on front-end jQuery framework, so I would like to know how can i check the width of windows on load and call a function & how to call that same function again when size of window changes.

Suppose I want to call a function below 600px of window size on page load

if(windows.size < 600px)
{
   //call a function
}

How to achieve this?


Asked by:- neena
0
: 2792 At:- 5/8/2018 12:41:53 PM
jQuery javascript jquery screen width change







1 Answers
profileImage Answered by:- Sam

You can use $(window).width() to check the current width of the screen using jQuery and fire function's which need

if($(window).width() < 600) //don't need to write px in the end , only numbers
{
   //call a function
}

Suppose you need to call a function CheckWidth() on page load and whenever window size changes, you have to write code like this

function CheckWidth(){

 //do something if window size is belo 600
 if($(window).width() < 600) 
 {
   //code
 }
 else
 {
   //else do this
 }
}

//call function on page load
CheckWidth();

//call function whenever windows size changes
$(window).on('resize',function(){

   CheckWidth();
})

That's it, you are done.

1
At:- 5/9/2018 7:53:08 AM
thank you for your help, works for me. 0
By : neena - at :- 5/9/2018 2:34:33 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use