To stop mobile screen rotation using jQuery, you can prevent the default behavior of the device's orientation change event. Here's how you can achieve this:

javascript
$(document).ready(function() { // Detect orientation change event $(window).on('orientationchange', function(event) { // Prevent default orientation change behavior event.preventDefault(); // Lock the screen orientation to the current orientation lockScreenOrientation(); }); // Function to lock the screen orientation function lockScreenOrientation() { // Check if the screen orientation API is supported by the browser if (screen.orientation && screen.orientation.lock) { // Lock the screen orientation to the current orientation screen.orientation.lock(screen.orientation.type); } else if (window.screen && window.screen.lockOrientation) { // For older browsers, use the deprecated lockOrientation method window.screen.lockOrientation(screen.orientation.type); } } });

In this code, we use jQuery to detect the orientationchange event when the device's orientation changes (i.e., the user rotates the screen). We then call the event.preventDefault() method to prevent the default orientation change behavior.

Next, we call the lockScreenOrientation function to lock the screen orientation to the current orientation. The lockScreenOrientation function checks if the screen orientation API is supported by the browser and uses the appropriate method (screen.orientation.lock or window.screen.lockOrientation) to lock the screen orientation.

Please note that locking the screen orientation might not work on all devices and browsers. Some devices may have restrictions on locking screen orientation for security reasons. Also, locking the screen orientation can be a controversial feature, as it can be seen as interfering with the user's control over their device. Before implementing this feature, consider the user experience and the reasons for preventing screen rotation. It is generally best to allow users to rotate their screens as needed for the best viewing experience.

Have questions or queries?
Get in Touch