$ anchorScroll无法正常运行

时间:2015-05-15 07:22:31

标签: angularjs anchor-scroll

点击按钮,我希望页面滚动特定的div bottom

我第一次点击,页面会转到最顶层,初始网址http://localhost:8000变为http://localhost:8000/#/bottom为什么它不会转到我提到的div )。

第二次点击按钮,网址变为 http://localhost:8000/#/bottom#bottom它会转到div bottom这就是我想要的)。

我该如何处理?

代码段

$location.hash('bottom');
$anchorScroll();

提前致谢

如果需要,请询问更多解释。 。

2 个答案:

答案 0 :(得分:0)

我不知道你是否还需要帮助,但是你检查过this个问题吗?

无论如何,我还使用一个按钮来做锚定滚动:

<button ng-click="app.gotoAnchor('destination')">GO!</button>
(...)
<div id="destination" style="height:1px;"></div>

我刚刚使用了AngularJS文档examples的组合和之前提到的SO问题来实现这个功能:

function gotoAnchor(anchor){
      if ($location.hash() !== newHash) {
        // set the $location.hash to `newHash` and
        // $anchorScroll will automatically scroll to it
        $timeout(function() {
          $location.hash(anchor);
        });
      } else {
        // call $anchorScroll() explicitly,
        // since $location.hash hasn't changed
        $anchorScroll();
      }
    }

成为$timeout召唤使这项工作变得更好的人。

希望这有帮助。

答案 1 :(得分:-3)

<a href="#bottom">BOTTOM</a>
<div style="height:700px"> this is boby </div>
<a href="https://www.facebook.com/vietlink365" id="bottom">JOIN NOW!</a>