我想重定向到同一页面中 DOM 的某些元素。 我使用了以下代码, JS:
$scope.scrollTo = function (id) {
$location.hash(id);
$anchorScroll();
};
HTML:
<div>
<div id="chartdiv15" class="chartdiv"></div>
<div id="chartdata15" class="chartdata"></div>
</div>
和:
<md-input-container>
<label>Menu</label>
<md-select ng-model="menu">
.....
<md-option ng-click="scrollTo('chartdiv15')">click</md-option>
</md-select>
</md-input-container>
所以这个过程使我能够指向链接( http://localhost:3000/#/graphics#chartdiv15 )
它到达该元素并在一秒钟内重定向到页面顶部,就好像我什么也没做一样...... 有人可以帮我解决这个奇怪的问题吗? 谢谢:)
最佳答案
您是否尝试过不使用 $location.hash(id);但只有 $anchorScroll(id);
根据$anchorscroll的文档:
When called, it scrolls to the element related to the specified hash or (if omitted) to the current value of $location.hash(), according to the rules specified in the HTML5 spec.
在我的项目中,我只使用 $anchorScroll(id),它效果很好,但当然 URL 没有更新。
如果您也希望更新 URL,也许您应该使用:
$location.hash(id);
$anchorScroll(); // <- no id here
很抱歉没有直接回答您的问题,但我认为我们无法仅通过提供的数据来帮助您,如果您添加一个重现该问题的 plunker,那么提供建议会更容易。
关于javascript - $anchorScroll 指向链接但无法访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168492/