我正在寻找一个简单的,跨浏览器的"滚动到顶部"动画,我可以应用于链接.我不想要一个JS库,如jQuery/Moo等.
// jQuery Equivilant to convert to pure JS...
$('html, body').animate({scrollTop:0}, 400);
Run Code Online (Sandbox Code Playgroud)
对于那些应该在进入图书馆之前学会了JS 100%的人来说,我是一个完美的案例.:(
阅读AngularJS文档我还没弄清楚是否$anchorScroll
可以有一个持续时间/缓动选项来平滑滚动到元素.
它只说:
$location.hash('bottom');
// call $anchorScroll()
$anchorScroll();
Run Code Online (Sandbox Code Playgroud)
我不使用jquery而不想; 是否还有一种聪明而简单的方法来制作或扩展$anchorScroll
以使滚动更加流畅?
我刚刚开始使用AngularJS,我想知道这种方法在我提交表单时将页面滚动到第一个输入时出错.
以下是jQuery的方法:
$('html, body').animate({
scrollTop: $("--- #ID OF THE FIRST INPUT WITH ERROR ---").offset().top
}, 2000);
Run Code Online (Sandbox Code Playgroud)
如何在Angular中做到这一点?
HTML
<form class="form" novalidate>
<input type="text" class="nom-du-projet" ng-model="fields.nom" required />
<p ng-show="fields.nom.$invalid && !fields.nom.$pristine">The name is required.</p>
<input type="text" ng-model="fields.cible" />
...
<button type="submit" ng-click="submit(fields)">Add</button>
</form>
Run Code Online (Sandbox Code Playgroud)
JS
$scope.submit = function(fields){
console.log(fields);
$http
.post('/xxxx', fields)
.success(function(response) {
// success
})
.error(function(response) {
// scroll to field error
});
}
Run Code Online (Sandbox Code Playgroud) 我已经设置了ng-repeat = objectId的id,即
<div class="mainListLabel " ng-if="profile==1" id={{obj.boeId}}
ng-repeat="obj in mainList"
ng-click="getEmployeeInformation(obj.boeId)">{{obj.boedisplayName}}</div>
Run Code Online (Sandbox Code Playgroud)
当我尝试做的时候
document.getElementById("1163").value, I get undefined
Run Code Online (Sandbox Code Playgroud)
或当我这样做
angular.element('#1163').val(), I get ""
Run Code Online (Sandbox Code Playgroud)
我假设这是因为脚本文件是在分配动态id之前执行的,有什么办法我可以从元素id中获取值