简体   繁体   中英

jQuery based on mobile or desktop

So I have following js:

 jQuery('.rhmps_aj').click(function(e) {
    ...
    success: function(data){
        //Desktop
        jQuery('.desktop_less').hide();
        jQuery('.desktop_more').show();
        //Mobile
        jQuery('.mobile_less').hide();
        jQuery('.mobile_more').show();

 });

Can someone tell me how I can "disable" certain functions based on whether the content is being viewed via desktop or mobile?

For example, with desktop, I want to disable "mobile" function and vice versa for mobile view.

You can check the width of the screen size. If the screen size is less than that width you can perform the function you want.

Example:

if ($(window).width() < 600) {       // if width is less than 600px
   MobileFunctions();                 // execute mobile function
}
else {                              // if width is more than 600px
   DesktopFunctions();               // execute desktop function
}

I think screen.width might do the trick.

$<!DOCTYPE html>
<html>

<head>
  <script data-require="jquery@2.1.4" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>

<body>
  <script>
    var width = screen.width;
    $(document).ready(function() {
      if (width < 600)
        runMobile();
      else
        runDesktop();
    });

    var runMobile = function() {
      alert("Mobile");
    }

    var runDesktop = function() {
      alert("Desktop");
    }
  </script>
</body>

</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM