Is it possible to execute javascript based on window size?

I have a responsive site where I use javascript to create a sticky sidebar.

I also use media queries to go from multi-column layout to single-column layout when the browser size is less than 768 pixels.

I need to figure out how to disable a sticky script menu in a single column layout. Essentially, I need something like a media query for a script statement.

This is the code I use to include the script:

<script> jQuery('#info').containedStickyScroll({ duration: 0, unstick: false }); </script> 

Is there something I can add to it to call it only if the window has a width or width of 768 pixels?

EDIT: I'm looking for a solution that will work if the user resizes the window on the fly.

+6
source share
2 answers

Try it.

 $(function(){ $(window).resize(function(){ if($(this).width() >= 768){ jQuery('#info').containedStickyScroll({ duration: 0, unstick: false }); } }) .resize();//trigger resize on page load }); 
+6
source

Try this code:

 var height = $(window).height(); //I'm assuming you mean height, you can try .width() if yo u need it if (height < 768) { jQuery('#info').containedStickyScroll({ duration: 0, unstick: false }); } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/914185/


All Articles