[英]Position on specific div on content load
I'm developing a Angularjs frontend with a Node.js backend where I can received all the data. 我正在开发一个带有Node.js后端的Angularjs前端,我可以在其中接收所有数据。 I get an array of json objects who represents several dates like this: 我得到了一个json对象数组,这些对象代表这样的几个日期:
[
...
{date: "30/11"}, /* today */
{date: "1/12"},
{date: "2/12"},
{date: "3/12"},
...
]
I would like to display the array like this but the first day on the actual view should be "today" and be able to scroll left/right to display older/newer days. 我想显示这样的数组,但是实际视图的第一天应该是“ today”,并且能够向左/向右滚动以显示较早/较新的日子。
+----------------------------+
| today 1/12 2/12 3/12 |
|+-----+ +----+ +----+ +----+|
|| | | | | | | ||
|| | | | | | | ||
|+-----+ +----+ +----+ +----+|
| |
| Scrollbar |
| <________________________> |
+----------------------------+
Actually I display the array from the start with ng-repeat. 实际上,我从ng-repeat开始就显示数组。 I'm able to detect "today" with a comparison and add a class to the specific div BUT I don't know how to align the screen on the "today" div. 我可以通过比较来检测“今天”,然后将一个类添加到特定的div,但我不知道如何在“今天”的div上对齐屏幕。 Do you have any idea about how I can do this ? 您是否知道我该怎么做?
You can use scrollIntoView()
. 您可以使用scrollIntoView()
。
Here's a sample. 这是一个样本。 The scrollIntoView
should be in the a directive. scrollIntoView
应该位于指令中。 I've put it in the controller just to show how it's supposed to work. 我将其放在控制器中只是为了说明它应该如何工作。
Script 脚本
angular.module("app", []).controller("LineCtrl", function ($scope) {
var tds = []
for (var i = 0; i < 250; i++)
tds.push(i);
$scope.tds = tds;
setTimeout(function () {
document.getElementsByClassName('today')[0].scrollIntoView();
}, 1000)
});
HTML 的HTML
<div ng-app="app">
<div ng-controller="LineCtrl">
<table>
<tr>
<td ng-repeat="td in tds" ng-class="{ 'today': $index === 50 }">{{ td }}</td>
</tr>
</table>
</div>
</div>
Code Snippet 代码段
angular.module("app", []).controller("LineCtrl", function($scope) { var tds = [] for (var i = 0; i < 250; i++) tds.push(i); $scope.tds = tds; setTimeout(function() { document.getElementsByClassName('today')[0].scrollIntoView(); }, 1000) });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="LineCtrl"> <table> <tr> <td ng-repeat="td in tds" ng-class="{ 'today': $index === 50 }">{{ td }}</td> </tr> </table> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.