Exploring AngularJS: Implementing CSS media query for responsive screen resizing

I currently have a CSS code that triggers a message box through media query when the screen is resized to a specific size. I am looking for a way to make the message box disappear automatically after 5 seconds.

Do you have any suggestions on how to achieve this using AngularJS?

CSS:

@media all and (max-width: 1024px), all and (max-height: 760px) {
  div#bestview {
    display: block;
    position:fixed;
    z-index: 9999; /* above everything else */
    top:0;
    left:0;
    bottom:0;
    right:0;
    background-image: url(../img/transpBlack75.png);
  }
}

HTML:

<div id="bestview">The selected browser size is not optimal for.....</div>

Answer №1

Follow these steps to achieve the desired outcome:

    // Start by creating a directive within your application:

    myApp.directive('hideElement', function() {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                var timeoutValue = attrs.hideElement * 1;
                setTimeout(function() {
                    element.addClass('hidden');
                }, timeoutValue);
            }
        }
    });

    // Define the corresponding CSS style:

    .hidden {
        display: none;
    }

    // Implement the directive in your HTML code:

    <div hideElement="5000" id="bestChoice">The current selection is not the most suitable option for....</div>

Answer №2

My approach was a bit different from xe4me's answer. I decided to share what I did below:

CSS:

@media all and (max-width: 1024px),
       all and (max-height: 760px) {
    div#bestview {
      display: block;
      position: fixed;
      z-index: 9999; /* above everything else */
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background-image: url(../img/transpBlack75.png);
    }
  }

AngularJS:

app.directive('bestView', function($timeout) {
  return {
    restrict: 'A',
    link: function(scope, elem, attr, ctrl) {
      scope.$watch(
        function() {
          return elem.is(':visible');
        },
        function(newval) {
          if(newval == true) {
            $timeout(function() {
              elem.remove();
            }, 5000); //disappear after 5 seconds
          }
        }
      );
    }
  };
});

HTML:

<div id="bestview" best-view>The selected browser size is not optimal for.....</div>

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

Layout your Angular components with a column design using Flex Layout

Is there a way to adjust the width of a child component in an fxLayout set to column? Take this example for reference: https://stackblitz.com/edit/angular-fxlayout-custom-breakpoints?file=app%2Ftest.component.ts In the provided example, there are three f ...

Is the content within the div longer than the actual div itself when the width is set to 100%?

When working with a div of fixed width that contains only an input text box, and the width of the input is set to 100%, you may notice that it extends slightly beyond the boundaries of the div. For demonstration purposes, consider the following code: HTM ...

The functionality of Angular animate becomes compromised when there is a conflict between predefined CSS states and transition states

Explore this example CSS code: /* animations at the start */ .error-animation.ng-enter { -webkit-transition: 0.5s linear all; transition: 0.5s linear all; opacity: 0; } ...

How can I refresh the GUI in AngularJS after a Promise resolves?

My perspective is linked to a group of objects that display flawlessly. Among these objects, there is an async property that, upon completion, fails to update its value in the user interface. To trigger the asynchronous process, I iterate through each prop ...

The position of the jQuery VirtualKeyboard is not displaying correctly

I'm currently experiencing an issue with the placement of the keyboard while using the Mottie/Keyboard plugin. The images provided below illustrate my desired outcome and the current behavior: https://i.sstatic.net/GULve.png Despite my attempts, the ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

What method can I use to enlarge the carousel component from the flowbit-svelte library?

I have been working on a project that requires a Carousel, and I decided to incorporate the flowbit-svelte carousel into my svelte project. While the carousel is functioning well, I am facing an issue with the component size. It seems to be cutting off my ...

Each time forEach is called, it increments the previous result by 1

My service returns a response containing the following data: {"id":157336,"results":[ {"id":"53db3c790e0a26189a000d09","iso_639_1":"en","key":"ePbKGoIGAXY","name":"Trailer 3","site":"YouTube","size":1080,"type":"Trailer"}, {"id":"550df44b925141355 ...

Is there a way to recycle an image and ensure that the web browser only needs to download it once?

Is there a way to effectively reuse the same image multiple times on my website without inefficiently downloading it each time? ...

What can I do to prevent a panel from being pushed beneath two other panels when the window size is reduced?

These are my three panels: ___ _________ ___ | | | | | | | | | | | | | | | | | | | | | | | | |___| |_________| |___| When I resize the window slightly, the layout changes: ___ ...

Angular Object Error: Unhandled Exception

As someone who is new to Angular, I am still trying to grasp how it functions. However, I have encountered an issue early on that is causing the below code to produce an "Uncaught Object" error in the console and disrupt Angular functionality. It seems t ...

Eliminating documents subsequent to file upload

I need to send in my application and upload the necessary file for the submission. However, I can only upload the files after the application is successfully submitted. To achieve this, I have included the upload function within the success callback of the ...

Error handling in angularJS can be quite challenging at times,

Currently, I am experimenting with AngularJS to develop an application, and I have encountered a challenge related to the $scope context. This is the main file of my app: var app = angular.module('app', [ 'matchCtrl', &apos ...

JavaScript popup is no more visible on the webpage

Recently, I implemented a pop-up on my website that used cookies to prevent it from appearing every time a user visited a page. However, after making this change, the pop-up stopped showing altogether. Despite my best efforts in testing, researching, and s ...

Is there a way to eliminate the "x" in the upper right corner of a Foundation Joyride?

Edit 7/16/15: I decided to place the following code between the <li> tags within the joyride that required the removal of the "x" button: <script>$('.joyride-close-tip').remove();</script> While not the most elegant solution ...

Are AngularJS Controllers functions or classes in nature?

The AngularJS ng-controller documentation specifies that ng-controller "attaches a controller class to the view" (source: angularjs.org). In contrast, the AngularJS Controllers documentation states that "When a Controller is attached to the DOM via the ng- ...

Incorrect placement of navigation in HTML/CSS dimensions

I'm working on my new portfolio and I've run into an issue with the navigation placement that I can't seem to solve. The text should be aligned with the lines on the sides, but instead it's shifted right and down. I'm struggling t ...

Executing Angular function only once within NG-Repeat loop

Within my ng-repeat loop, I have the following code: <p style="color:red">Booked Ref : {{agencyref(contentzz.Ref)}}</p> The function within the ng-repeat is being called repeatedly due to Angular's behavior. This function contains an HTT ...

Swap out a button for another using JavaScript

I am facing a challenge where I need to replace an active button with a deactivate button. The issue is that when I click on the active button, it does change to the deactivate button as expected. However, clicking again does not switch it back to the acti ...

How can we transfer animation duration and `@keyframe` values through a function in CSS?

In my angular project, I am currently adding animations to text. However, I want to make these animations dynamic. For instance, I have a CSS class called .slide-in-top with an animation time set to 1.3s. Instead of this static value, I want to be able to ...