简体   繁体   English

针对角度ng-show和ng-hide的类

[英]targeting a class for angular ng-show and ng-hide

I have created a basic angular slider i am contemplating whether i show use ng-class or ng-show as it stands i am trying to create a fade transition on each json object change. 我已经创建了一个基本的角度滑块,我正在考虑是否显示ng-classng-show ,我正试图在每个json对象更改上创建一个淡入淡出过渡。

i am currently using ng-show and ng-hide, i am struggling to a class to achieved a css transition, is this even possible, secondly is it practice ? 我目前正在使用ng-show和ng-hide,我正在努力上一堂课以实现CSS转换,这是否有可能,其二是实践吗? Plunkr Plunkr

var timer;
$scope.startAuto = function() {
    timer = $interval(function(){
        $scope.jobNotification = ($scope.jobNotification + 1) % $scope.jobs.length;
    }, 5000);
};

$scope.isActive = function (index) {
    return $scope.jobNotification === index;

 };

$scope.showJobNotification = function (index) {
    if (timer){
        $interval.cancel(timer);
        $scope.startAuto();
    }
    $scope.jobNotification = index;
};

HTML HTML

<ul class="tickrSlider">
            <li data-ng-repeat="job in jobs" ng-class="{'jobActive' :isActive($index)}" ng-show="isActive($index)">
                <h1>{{job.jobTitle}}</h1>
                <p>{{job.sector}}</p>
                <a href="/"></a>
            </li>
        </ul>

There's nothing wrong with your use of Angular here, you just need a little CSS: 在这里使用Angular并没有错,您只需要一点CSS:

ul {
  position:relative;
  height:100px;
}

nav {
  position:relative;
}

li {
  position:absolute;
  top:0;
  opacity:0.0;
  transition: opacity 2s;
}

.job-active {
  opacity:1.0;
}

The basic idea is to give the unordered list some height and make its position relative so that it will define a top for the list items and block out some screen space for them. 基本思想是使无序列表具有一定的高度,并使它的位置相对,以便为列表项定义一个顶部,并为它们屏蔽一些屏幕空间。 Then the Nav element will lie underneath the list, and the list items will all be laid out on top of each other. 然后,Nav元素将位于列表下方,并且列表项将全部放在彼此的顶部。 You have to be careful to make sure that the height of the list is always equal to or greater than the tallest list item, or else the item will run over into the Nav. 您必须小心确保列表的高度始终等于或大于最高列表项,否则该项将越过Nav。 This is because the list items use absolute positioning to draw on top of each other. 这是因为列表项使用绝对定位来相互绘制。 Removing list items from the layout using display:none won't work because the opacity transition would never show. 使用display:none从布局中删除列表项将不起作用,因为不透明度过渡将永远不会显示。

If you want to avoid defining the height of the list, you could experiment with CSS animation and key frames instead of transitions. 如果要避免定义列表的高度,可以尝试CSS动画和关键帧而不是过渡。

Here's my fork of your Plunker for experimentation . 这是我为实验准备的叉子

Please note that the convention in CSS is to use hyphens (-'s) to separate logical words, rather than camelCase . 请注意,CSS中的约定是使用连字符(-)分隔逻辑词,而不是camelCase I changed ng-class="jobActive" to ng-class="job-active" in the HTML as well. 我也将HTML中的ng-class="jobActive"更改为ng-class="job-active"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM