OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How Can I Get Javascript To Run On A Specific Screen Size?

  • Thread starter Thread starter Richard Perkins
  • Start date Start date
R

Richard Perkins

Guest
Below is some Javascript that I'm using for the mobile menu on my website. This Javascript stops content outside the menu from scrolling when the mobile menu is open and closes the menu when an item inside of it is clicked.

Code:
document.getElementById('cp_toggle03').onchange = function() {
 if (document.getElementById('cp_toggle03').checked) {
 document.body.style.overflow = "hidden";
  } else {
 document.body.style.overflow = "";
  }

}


var elements = document.getElementsByTagName('li');
var closeHamp = function() {
 document.getElementsByClassName('cp_menuicon')[0].click();
    };



for (var i = 0; i < elements.length; i++) {
 elements[i].addEventListener('click', closeHamp, false);
    }

Is there a way to set it so that this code only runs when on a specific screen size? I've tried using this Javascript to do so but haven't had any luck getting it to run propperly.

Code:
function execute(){
   if(screen.width <= 768) {
   
     }

If anyone has any suggestions as how I could get this Javascript to work on a specific screen size I'd appreciate you letting me know.

My Website
<p>Below is some Javascript that I'm using for the mobile menu on my website. This Javascript stops content outside the menu from scrolling when the mobile menu is open and closes the menu when an item inside of it is clicked.</p>
<pre><code>document.getElementById('cp_toggle03').onchange = function() {
if (document.getElementById('cp_toggle03').checked) {
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = "";
}

}


var elements = document.getElementsByTagName('li');
var closeHamp = function() {
document.getElementsByClassName('cp_menuicon')[0].click();
};



for (var i = 0; i < elements.length; i++) {
elements.addEventListener('click', closeHamp, false);
}
</code></pre>
<p>Is there a way to set it so that this code only runs when on a specific screen size? I've tried using this Javascript to do so but haven't had any luck getting it to run propperly.</p>
<pre><code>function execute(){
if(screen.width <= 768) {

}
</code></pre>
<p>If anyone has any suggestions as how I could get this Javascript to work on a specific screen size I'd appreciate you letting me know.</p>
<p><a href="https://morikoboshi.com/" rel="nofollow noreferrer">My Website</a></p>
Continue reading...
 

Latest posts

Online statistics

Members online
1
Guests online
5
Total visitors
6
Top