Determine the currently active view on a mobile device

I am trying to determine whether the user is viewing the website vertically or horizontally on their mobile device or iPad in order to adjust the image scale accordingly.

For example:

If the user is viewing the page horizontally, I want the image style to be set as follows:

img{width:100%; height:auto;}

But if the user is viewing the page vertically, the CSS for the image should be:

img{width:auto; height:100%;}

How can I achieve this functionality using jQuery, JavaScript, media queries, or any other method so that I can apply the appropriate style based on the user's viewing orientation on their mobile or iPad?

Thank you.

Answer №1

If you want to determine the orientation using JavaScript, you can implement the following script:

detectOrientation();
    window.onorientationchange = detectOrientation;
    function detectOrientation(){
        if(typeof window.onorientationchange != 'undefined'){
            if ( orientation == 0 ) {
                 //Execute code for Portrait Mode
            }
            else if ( orientation == 90 ) {
                 //Execute code for Landscape Mode
            }
            else if ( orientation == -90 ) {
                 //Execute code for Landscape Mode
            }
            else if ( orientation == 180 ) {
                 //Execute code for Landscape Mode
            }
        }
    }

Snippet obtained from

Answer №2

Have you considered implementing responsive CSS with specific breakpoints?

Here's an example:

@media screen and (max-width: 123px /* adjust as needed */ ) {
  img{width:auto; height:100%;}
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clear existing markers from the map before inserting new markers

I have a map where initially the markers load coming from the database, Then there is a time-based Ajax request that fetches new records every minute. In my code snippet, I am using setMapOnAll(null) following the guidance from the Google Maps Documentati ...

Double-checked in p:commandButton, encountering a race condition with oncomplete

When I execute the server action using p:commandButton, I then refresh the form to display hidden fields and show a dialog. Attempting to refresh only the panels with hidden buttons yields the same result: After I call dialog.show(), a second refresh is t ...

Creating a Jquery function to clone a select element with an inline onchange event and handle multiple ajax requests

I am dealing with a select dropdown that gets populated with numbers 14 through an ajax request. Each time a number is chosen from the dropdown, two signs are displayed as options. These signs are fetched using an onchange function attached to the select e ...

Issues with inconsistent behavior of the select option in a form

Having some trouble with my form code. When I checked the web page, there seems to be an extra element that shouldn't be there. You can view the webpage . Inspecting it in browser developer mode, I found this extra element . Can you please help me ide ...

What is the process for altering the background color of a modal?

I'm trying to figure out how to change the background color of my modal once it's displayed, similar to this example: https://i.stack.imgur.com/fOUDZ.png. I want the background color to cover the entire page and not just the modal itself. When I ...

The element div is not permitted as a child of the element h5 in this particular scenario

My code snippet is as follows: $compile .= "<h5 data-count='".$acctemmpi. "' class='shortcode_accordion_item_title expanded_". $expanded_state . "'>" . $title . "<div class='ico'&g ...

A guide on utilizing jQuery to select a checkbox by accessing a Laravel variable

I am seeking advice on how to automatically select the checkbox linked to a student identified through my jQuery search function. To provide context, here is an example of my "Ajax" request. Currently, it showcases the search result in an HTML 'span&a ...

Validating file extensions using Jquery during file uploads

I'm working on a page where users can upload images using drag and drop, but I want to limit them to uploading only jpg files. Unfortunately, my knowledge of jquery is quite basic, so I'm struggling to figure out how to achieve this. Here's ...

What's the Best Way to Add a Border to My Two-Column Form?

I'm currently working on a reservation form for my website with a 2-column layout. In one column, I have tables, and I've been trying to add a divider between the columns but haven't found a solution yet. Despite researching ways to add a d ...

Despite implementing an event listener, the Google Maps API is failing to resize properly when created using AJAX

Currently, I am facing an issue with the Google Maps API as the map generated is not resizing to fit the div. Instead, it shows a large grey area which is quite frustrating for me. Can someone please assist me in resolving this problem? Below is the code ...

Position a dynamic <div> in the center of the screen

My goal is to design a gallery page with a list of thumbnails, where clicking on a thumbnail will open the related image in a popup div showing its full size. The issue I'm facing is how to center the popup div on the screen, especially when each pic ...

Discovering the exact measurement of "remaining space" in absolute units with flexbox techniques

I am faced with a dilemma that is leaving me uncertain. Within a flexbox layout, specifically column-oriented, there are three children: top, middle, and bottom. The challenge arises in the middle child where I must embed a third-party component that requ ...

An unexplained line break is being added to content loaded via ajax requests

When using either .ajax or .load to load content from an .html page and insert it into an element, I am experiencing a strange issue where a mysterious new line is appended after the content. It is not a break element, but more like a \n or \r ch ...

What is the best way to monitor the status of several Fine Uploader instances at once?

On my page, I have multiple instances of fineuploader. Before a user clicks the submit button, I need to ensure that none of the files are currently in progress for any instance of fineuploader. It's okay if the files are completed, deleted, or cancel ...

Checking Whether a Value Entered in an Input Field Exists in an Array Using jQuery

Can someone help me with checking if a specific value is in my array? I want to achieve something like that, any suggestions on how to do it? if(jQuery.inArray($('#ValueInputTitle').val, variableValueInput) !== -1) { console.log("is in arr ...

Ensure that each of the two divs maintains a 16:9 aspect ratio, and utilize one div to fill any remaining empty space through the

This layout is what I am aiming for: While I can achieve a fixed aspect ratio with a 16:9 image by setting the img width to 100%, I run into an issue where the height scaling of flexbox becomes non-responsive. The height remains constant, and only the fir ...

Skipping code in JavaScript/jQuery function on window load

I have created a function that applies specific CSS rules to elements when the window is fully loaded. Check out my code below: function applyHover() { $('.view_preloader .overlay').css('opacity',1); $('.view_pre ...

Discovering the pixel value of scrolled distance (scroll delta) using jQuery scroll event

Here's the issue I'm facing: $(window).scroll(function(e){ console.log(e); }) I'm trying to figure out how much I've scrolled in pixels. It seems like the scroll value may be influenced by the window size and screen resolution ...

I am experiencing an issue where the close button image on tap is not closing on the first tap on

Why does the page close on the second tap instead of the first tap when using an iPhone? The image changes on tap but doesn't close as expected. $("#privacy-close-btn").mouseenter(function() { $("#privacy-close-btn").css("display", "none"); $(" ...

Obtaining the field name from a SQL Server database by utilizing the field ID

When viewing my selections, I have several drop-down lists that display the ID and I would like the name to be shown to the user for clarity. However, when I list everything that has been added, it displays the chosen ID instead of the name. I attempted t ...