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

时间:2014-06-16 04:47:00

标签: css angularjs

我有一个现有的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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>