Display either the Web or Mobile modal upon page load based on the screen size

For my design project, I have crafted two unique modals that will pop up when their corresponding button is clicked. However, I have set it up so that each button is only visible on certain screen sizes using CSS @media query.

Now, I am looking to make a change where if the screen size is equal to or greater than 700px, the modal will automatically display when the page loads. If the screen is less than 700px, the alternative modal will be displayed without the need for clicking a button.

<div class="discountweb">
 <button class="btn btn-default btn-sm" data-modal-url="/_process/ajax_fetch_modal_content?type=snippet&name=Popup Discount Web">
 Discount Test Web
 </button>
</div>
<div class="discountmob">
 <button class="btn btn-default btn" data-modal-url="/_process/ajax_fetch_modal_content?type=snippet&name=Popup Discount Mobile">
 Discount Test Mobile
 </button>
</div>
<style>
 @media only screen and (min-width:700px) {
  .discountmob {
   display: none;
  }
 }
@media only screen and (max-width: 699px) {
 .discountmob {
  display: block;
 }
 .discountweb {
  display: none !important;
 }
}
</style>

Your assistance with this matter would be greatly appreciated.

Answer №1

Solved the issue by implementing an event listener that triggers when the page finishes loading. This listener calculates the size of the webpage and then automatically clicks the appropriate button to activate a popup based on the screen size.

    <script>
        window.addEventListener('load', (event) => {
            var windowSize = $(window).width();
            if (windowSize >= 699) {
            document.getElementById('discountweb').click();
            } else if (windowSize <= 700) {
            document.getElementById('discountmob').click();
            }

        })
    </script>
    <div class="discountweb">
    <button class="btn btn-default btn-sm" id="discountweb" data-modal-url="/_process/ajax_fetch_modal_content?type=snippet&name=Popup Discount Web">
                    Test Discount for Web
                </button>
    </div>
    <div class="discountmob">
    <button class="btn btn-default btn" id="discountmob" data-modal-url="/_process/ajax_fetch_modal_content?type=snippet&name=Popup Discount Mobile">
                    Test Discount for Mobile
                </button>
    </div>
    <style>
            .discountmob{
                display: none;
            }
            
            .discountweb {
                display: none;
            }
            
    </style>

Answer №2

One way to modify the css file is by making these changes:

@media only screen and (min-width:700px) {
    .discountmob {
       display: none;
    }
   .discountweb {
       display: block;
   }
@media only screen and (max-width: 699px) {
   .discountmob {
       display: block;
   }
  .discountweb {
       display: none;
  }
}

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

Tips for automatically closing a dropdown menu when it loses focus

I'm having some trouble with my Tailwind CSS and jQuery setup. After trying a few different things, I can't seem to get it working quite right. In the code below, you'll see that I have placed a focusout event on the outer div containing th ...

What is the best way to create a horizontal scrolling feature for a two-row layout using Bootstrap?

When using one row, the script works successfully. Here is an example of the code: html : <div class="testimonial-group"> <div class="row"> <div class="col-md-4">1</div> <div class="col-md-4">2</div> < ...

Is there a way to confirm if all div elements have a designated background color?

I have a scenario where I have dynamically added several divs with a specific class to my webpage. These divs change color when the user hovers over them. I am trying to trigger a specific function once the last div has been set to a particular backgroun ...

The HTML5 video element is not functioning properly on iOS devices

<video preload="true" controls="true" style="width:100%;" id="video-banner" poster="poster-img.png"> <source src="video.mp4" type="video/mp4"> Your browser does not support HTML5 video. </video> I have the following code to displ ...

Is there a way I can replicate this expandable div?

I have successfully implemented a script that expands and collapses. However, I am facing an issue when trying to use two of these scripts on the same page. I attempted to simply add "_two" to all instances of "accordion," but it doesn't seem to be f ...

Is there a solution for the noticeable delay in loading fonts on a web page?

I've encountered an issue in my React project where a Google font I'm using seems to briefly load as a different font before switching to the correct one. How can I prevent this from happening? This is how I'm loading the font in public/ind ...

Several dropdown menus within the same container, working independently of each other

I recently encountered an issue with a drop-down navigation bar. When I have multiple drop-downs and click on one, it opens as expected. However, if I then proceed to click on another drop-down while the first one is already open, both of them remain open ...

Struggling to forward JSON data via AJAX to Django without relying on jQuery

My goal is to utilize AJAX in conjunction with Django, but I want to achieve this without relying on jQuery. So far, I have made progress in sending URL encoded data from AJAX to Django successfully. However, I am currently facing an issue where I am unabl ...

What is preventing me from using the JavaScript toggle button multiple times?

I am currently trying to implement a JavaScript toggle button in two different sections on the same page, but I am encountering difficulties. I attempted to change the IDs as well, but it didn't seem to work. Is it not possible to achieve this, or am ...

What are some effective strategies for preventing CSS duplication?

What is the best way to prevent repeating CSS code like this: div#logo div:nth-child(1) div { width: 30%; height: 30%; background-color: white; } div#logo div:nth-child(3) div { width: 30%; height: 30%; background-color: white; ...

Achieving uniform cell height distribution in Foundation XY-grid using flexbox

When utilizing the Foundation XY-grid, I encountered an issue with distributing cell heights evenly within a nested grid-y inside a cell using the .auto class on the parent cell. In a scenario with 2 cells in a grid-y, the desired outcome is 50%/50%. This ...

Toggle the visibility of a div depending on the user's selection

My HTML select element has a few options to choose from: <select class="form-control" data-val="true" data-val-number="The field CodeSetup must be a number." id="CodeSetup" name="CodeSetup"> <option selected="selected" value="0">AllCodes< ...

How do I display a table created using data from another SQL table in a database?

I have been assigned the task of displaying a table on screen that is created using SQL and PHP, however, the structure of the SQL table is not suitable for my needs. The original source table looks like this: ╔═════════════ ...

Guide to configuring a bootstrap checkbox using the razor CheckBoxFor() method

Struggling to implement Bootstrap styled checkboxes in my ASP .NET MVC form. Despite hours of searching for a solution, I have yet to find one that works for me. Below is my HTML razor code, but the checkbox is not displaying. <div cl ...

It is not possible to trigger an input click programmatically on iOS versions older than 12

Encountering a challenge in triggering the opening of a file dialogue on older iOS devices, particularly those running iOS 12. The approach involves utilizing the React-Dropzone package to establish a dropzone for files with an added functionality to tap ...

VSCODE's intellisense feature seems to be ignoring CSS code within JSX files

I've been puzzling over why Visual Studio Code isn't recognizing CSS syntax inside JSX files. While I'm typing, CSS IntelliSense doesn't provide suggestions for CSS - only Emmet suggestions.https://i.stack.imgur.com/FegYO.png Despite t ...

PHP not displaying radio button values

My latest project involved creating a quiz program using only PHP. The program consists of 4 different questions and radio buttons, and when an option is selected, the value of the radio button (A, B, C, or D) should be printed. I have set up 4 PHP varia ...

What is the best way to apply CSS to an image within a list item that is within an unordered list nested inside a specific

I've encountered an issue in my html file related to a navigation bar situated at the top of my page. The code snippet that initiates this navigation bar is as follows: <div class="“topNav"> <ul> <li><img src= ...

Align the text in the center of the button vertically

Struggling to vertically center text on a button? I understand the frustration! I've been working on this issue for a whole day with no success. My setup involves NEXT.js and TailwindCSS. <main> <div class='flex justify-center ite ...

The web server experiences a layout and functionality breakdown caused by an issue with the config.js file following the uploading process

I recently created a website. However, when I uploaded it to my hosting company's server and tested it on different browsers, the layout appeared broken. Some of the CSS files loaded properly, but certain elements and styles were not in their correct ...