javascript - Jquery获取x个第一个元素的总高度

标签 javascript jquery angularjs

我想使用具有动态高度内容的tinker jquery 插件。 我需要知道三个第一元素的总高度,我该如何计算?

<div class="myWrapper" ticker>
 <div>
   <div ng-repeat="latest in latests" class="latest">
      <p ng-bind-html="latest.txt"></p>
   </div> 
 </div>                   
</div>

我的指令:

 restrict: 'A',
 link: function($scope, element) {

   var height = //I need the total height of the firsts three divs with class latest

         element.easyTicker({
            height: height',
            interval: 5000
         });
  }

最佳答案

由于您需要让 Angular 渲染元素,因此我会将指令放在转发器上,然后您可以检查 ng-repeat 子项中的 $last 属性范围。

<div class="myWrapper">
 <div>
   <div ng-repeat="latest in latests" class="latest" ticker-item>
      <p ng-bind-html="latest.txt"></p>
   </div> 
 </div>                   
</div>

指令

app.directive('ticker-item', function () {
    restrict: 'A',
    link: function ($scope, element) {
        var height = 0;
        /* wait for last element in ng-repeat */
        if ($scope.$last) {
            element.siblings(':lt(3)').each(function () {
                height += $(this).height();
            });
            element.closest('.myWrapper').easyTicker({
                height: height,
                interval: 5000
            });
       }
    }
});

如果插件未正确设置最后一个元素,请在指令中注入(inject) $timeout 并将插件初始化包装在 $timeout() 中。这将确保首先渲染所有中继器元素

关于javascript - Jquery获取x个第一个元素的总高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27002507/

相关文章:

jquery - RequireJS + 航点 : Object [object Object] has no method 'waypoint'

javascript - 如何检测浏览器选项卡当前是否可见并使用 Javascript 呈现?

javascript - 操作谷歌日历 DOM

javascript - 为什么带有 ng-if 的 div 会阻止其兄弟运行 Angular?

javascript - 如何将 KEYS 分配给 client.keys() 函数中的值?

Javascript通过id从数据中删除项目

javascript - js setTimeout 同步

javascript - IIS 中的 AngularJS 路由

javascript - 在返回 promise 的函数上使用 .success 时,未定义不是函数

JavaScript 变量在定义后更改为结果行