首页 > 代码库 > [AngularJS] Using $anchorScroll
[AngularJS] Using $anchorScroll
If you‘re in a scenario where you want to disable the auto scrolling, but you want to control the scrolling manually, you can use the anchorscroll service, and then just invoke that after some hash has changed.
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body ng-app="egghead" ng-controller="AppCtrl as app"><a ng-click="app.goToAnchor(elm)" ng-repeat="elm in app.elms">{{elm}}</a><div id="{{elm}}" ng-style="app.createStyle($index)" ng-repeat="elm in app.elms"> {{elm}}</div><script src="//cdn.jsdelivr.net/tinycolor/0.9.16/tinycolor.js"></script><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script><script src="app.js"></script></body></html>
var app = angular.module("egghead", []);app.config(function ($anchorScrollProvider) { $anchorScrollProvider.disableAutoScrolling();})app.controller("AppCtrl", function ($location, $anchorScroll) { var app = this; app.elms = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); //https://github.com/bgrins/TinyColor#color-combinations var colors = tinycolor.analogous("steelblue", app.elms.length, app.elms.length); app.goToAnchor = function (elm) { $location.hash(elm);
//call when you when it scroll $anchorScroll(); } app.createStyle = function (index) { var color = colors[index]; //grabs a tinycolor of the array return { backgroundColor: color.toHexString(), borderBottom: "3px solid black", height: "100px" }; }})
[AngularJS] Using $anchorScroll
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。