1
我試圖在某人通過表單中的輸入字段標記頁面時向上或向下滾動頁面。我希望當用戶選中時,當前關注的輸入元素將在頁面中間垂直居中。我爲此寫了一個指令,但它不起作用,就像我期望的那樣。如何將頁面滾動到輸入字段焦點上的中間位置
angular
.module('app')
.directive('formScroll', formScroll);
function formScroll($window) {
function link(scope, elem) {
elem.bind('focus', function() {
var offset,
elOffset = elem.offset().top,
elHeight = elem.height(),
windowHeight = $window.innerHeight;
offset = elOffset - ((windowHeight - elHeight)/2);
$window.scrollTo(0, offset);
});
}
}
能否請您詳細闡述*隱而不宣」像我期望的那樣工作*意味着什麼? –