Created
June 29, 2023 13:25
-
-
Save Loosie94/5b6f059e6c2929ee465b863ab9586502 to your computer and use it in GitHub Desktop.
Alpine.js isMobile function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Alpine.js isMobile function</title> | |
</head> | |
<body x-data="globalData()"> | |
<script> | |
function globalData() { | |
return { | |
isMobile: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent), | |
// Init function will automaticly be fired | |
init() { | |
window.addEventListener('resize', this.updateIsMobile.bind(this)) | |
}, | |
// Update isMobile data on resize | |
updateIsMobile() { | |
this.isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) | |
}, | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment