【问题标题】:Grid/Tile view with angularjs使用 angularjs 的网格/平铺视图
【发布时间】:2016-11-04 09:31:32
【问题描述】:

我有 100 个宽度为 100 像素的 divs,可以放入宽度为 250 像素的父级。无论高度如何,我都需要将divs 显示在行中,如图所示。我试过用 css 解决这个问题,遗憾的是我理解了现实。

有没有什么 angularjs 插件我可以使用?

我听说过jquery masonry,有没有更好的选择?

正如@Divyanshu Maithani 要求解决我当前的问题, 请参阅下面的 plunker 链接,我在其中尝试使用 angular-masonry 解决我的问题

<div masonry>
    <div ng-repeat="item in items" class="masonry-brick item">
      {{item.name}}
      <button class="toggle-button" ng-click="item.show=!item.show">show</button>
      <div ng-if="item.show" class="hidden-box">
        This is a hidden box for {{$index+1}}th item.
      </div>
    </div>
  </div>

Plunker DEMO

我也在寻找其他选项,因为我不想用像 angular-masonry 这样的 jquery 插件来结束我的问题。任何帮助将不胜感激。谢谢

【问题讨论】:

  • 这是一个 Angular js masonary github.com/passy/angular-masonry 插件,可以很好地与 Angular js 配合使用
  • 虽然我设法通过 PHP(垂直计数 3x3 - 不像你的那样水平)来编程这样的东西,但我的投票(作为建议)是用于砌体的。我在一个项目的实验阶段使用了它,它是一个非常有用的插件,可以满足您的需求。我不会搜索更多,我怀疑你会找到更多。

标签: html css angularjs jquery-masonry


【解决方案1】:

AngularJS Masonry。您可以在主页上看到演示。使用masonry-brick 类很简单:

<div masonry>
    <div class="masonry-brick" ng-repeat="brick in bricks">
        <img ng-src="{{ brick.src }}" alt="A masonry brick">
    </div>
</div>

更新

为了使您的masonry-brick 不会相互重叠,您需要在showhide 内部div 时使用reload masonry。我在ng-click 上添加了一个function 这样做,这也将broadcast 一个masonry.reload 事件重新加载。

检查this工作代码。

JS

$scope.showItem = function(index) {
    $scope.items[index].show = !($scope.items[index].show);
    $scope.$broadcast('masonry.reload');
}

HTML

<div masonry>
    <div ng-repeat="item in items" class="masonry-brick item">
      {{item.name}}
      <button class="toggle-button" ng-click="showItem($index)">show</button>
      <button class="toggle-button" ng-click="remove($index)">X</button>
      <div ng-if="item.show" class="hidden-box">
        This is a hidden box for {{$index+1}}th item.
      </div>
    </div>
</div>

编辑

似乎存在问题,因为masonry 会立即重新加载,而masonry 过渡的动画需要时间。我在this plunk 中添加了一些过渡持续时间和$timeout

【讨论】:

  • 但是我的块是可扩展的。这意味着我的 div 后面有一个隐藏和显示块。所以在使用砖石时它会重叠
  • 这应该可以解决问题。
  • 感谢您的回答。这看起来像是 95% 的解决方案。但是当我自发单击显示按钮多个按钮时遇到问题
  • $scope.$broadcast('masonry.reload'); 更新不好。
  • 如果我们快速点击显示按钮,隐藏的 div 会重叠
【解决方案2】:

这实际上可以通过纯 CSS 和 HTML 实现。 CSS 3 规范提供了column-gapcolumn-width 属性,可以定义列之间的间隙。

流行的浏览器已经开始支持相同的(-webkit-column-width, -webkit-column-gap, -moz-column-width, -moz-column-gap 都支持)。

所以宽度和间隙可以写成如下方式:

#columns {
  -webkit-column-width: 220px;
  -webkit-column-gap: 15px;
  -moz-column-width: 220px;
  -moz-column-gap: 15px;
  column-width: 220px;
  column-gap: 15px;
}

我制作了一个小提琴,展示了一个工作示例。在 Chrome、Firefox 和 Safari 上完美运行。

https://jsfiddle.net/nashcheez/3cf9qrap/3/

【讨论】:

【解决方案3】:

这是您要查找的示例。点击link

HTML/视图

 <div ng-repeat="item in items" class="item">
          {{item.name}}
   <button class="toggle-button" ng-click="item.show=!item.show">show</button>
          <div ng-if="item.show" class="hidden-box">
            This is a hidden box for {{$index+1}}th item.
          </div>
        </div>

希望对你有用。

【讨论】:

  • 两个div之间有空格,你的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 2012-01-18
相关资源
最近更新 更多