gpt4 book ai didi

javascript - AngularJS $anchorScroll yOffset 在子元素中

转载 作者:数据小太阳 更新时间:2023-10-29 05:38:20 24 4
gpt4 key购买 nike

据我了解 $anchorScroll yOffset 在子元素中是不可能的:“为了使 yOffset 正常工作,滚动应该发生在文档的根部而不是某些子元素上。” https://docs.angularjs.org/api/ng/service/ $anchorScroll

示例(修改自 AngularJS 文档):我想单击一个链接并在滚动到的内容上方包含单词“between”。

index.html

<body ng-app="anchorScrollOffsetExample">
<div class="fixed-header" ng-controller="headerCtrl">
<a href="" ng-click="gotoAnchor(x)" ng-repeat="x in [1,2,3,4,5]">
Go to inner-anchor {{x}}
</a>
</div>
<div id="anchor" class="anchor">
<div ng-repeat="x in [1,2,3,4,5]">
between
<div id="innerAnchor{{x}}" class="anchor" >Inner-Anchor {{x}} of 5</div>
</div>
</div>
</body>

样式.css

  .anchor {
border: 2px dashed DarkOrchid;
padding: 10px 10px 200px 10px;
max-height:500px;
overflow-y: auto;
}

脚本.js

angular.module('anchorScrollOffsetExample', [])
.run(['$anchorScroll', function($anchorScroll) {
$anchorScroll.yOffset = 500;
}])
.controller('headerCtrl', ['$anchorScroll', '$location', '$scope',
function ($anchorScroll, $location, $scope) {
$scope.gotoAnchor = function(x) {
var newHash = 'anchor' + x;
if ($location.hash() !== newHash) {
$location.hash('innerAnchor' + x);
} else {
$anchorScroll();
}
};
}
]);

http://plnkr.co/edit/yFj9fL3sOhDqjhMawI72?p=preview

有没有一种在 AngularJS 中(最好没有 jQuery 或其他库)执行此操作的好方法,而无需在“之间”移动到我正在滚动到的 DIV 内部?

最佳答案

为什么不使用 anchor 标签?

<body ng-app="anchorScrollOffsetExample">
<div class="fixed-header" ng-controller="headerCtrl">
<a href="" ng-click="gotoAnchor(x)" ng-repeat="x in [1,2,3,4,5]">
Go to inner-anchor {{x}}
</a>
</div>
<div id="anchor" class="anchor">
<div ng-repeat="x in [1,2,3,4,5]">
<!-- Add an anchor above the text, and we scroll here instead of the div -->
<a name="innerAnchor{{x}}"></a>
between
<div class="anchor" >Inner-Anchor {{x}} of 5</div>
</div>
</div>
</body>

关于javascript - AngularJS $anchorScroll yOffset 在子元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30551159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com