Advertisement
chernov2000

Проверка на мобильное устройство на JS

May 15th, 2023 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. < charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. < src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  8. </head>
  9. <body>
  10. <form>
  11. <input type="text" id="input1" placeholder="Input 1">
  12. <input type="text" id="input2" placeholder="Input 2">
  13. </form>
  14. <div id="were">
  15. Content
  16. </div>
  17. <>
  18. function isMobileDevice() {
  19. return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
  20. };
  21.  
  22. if (isMobileDevice()) {
  23. $("#input1, #input2").on("focus", function () {
  24. $("html, body").animate({
  25. scrollTop: $("#were").offset().top
  26. }, 500);
  27. });
  28. }
  29. </script>
  30. </body>
  31. </html>
  32. //////////////////////
  33. function isTouchDevice() {
  34. return 'ontouchstart' in window || navigator.maxTouchPoints > 0 || window.navigator.msMaxTouchPoints > 0;
  35. }
  36.  
  37. if (isTouchDevice()) {
  38. console.log("This device has a touch screen");
  39. } else {
  40. console.log("This device doesn't have a touch screen");
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement