AngularJS:使用css媒体查询进行屏幕调整大小

问题描述 投票:1回答:2

我有一个现有的CSS,当屏幕调整到一定大小时,它会进行媒体查询并显示一个消息框。我希望消息框在5秒后消失。

知道如何在AngluarJs做到这一点?

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>
css angularjs
2个回答
0
投票

你可以这样做 :

    // make a directive in your app : 

    youApp.directive('hideme',function(){
       return{
          restrict:'A',
          link:function(scope,elem,att){
             var timeout = att.hideme*1; // *1 to convert it to integer:(
             setTimeout(function(){
                 elem.addClass('hidden');
              },timeout);
          }
       }
     });

     // in your css : 

       .hidden{
         display:none
     }

     // in your html:

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

0
投票

我做的与xe4me给出的答案略有不同。我想我仍然会发布我在这里所做的事情:

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>
© www.soinside.com 2019 - 2024. All rights reserved.