gpt4 book ai didi

jquery - AngularJS滚动到元素防止浏览器跳转

转载 作者:行者123 更新时间:2023-12-01 00:06:20 25 4
gpt4 key购买 nike

我编写了一个包含 block 列表的应用程序。

每个 block 都包含到其他 block 的链接。当点击某个 block 的链接时,例如 #/home/page-19,页面会根据当前位置向下/向上动画。

这一切目前都有效,但是当单击 anchor 并且路线更新时,浏览器跳到顶部然后向下动画,我需要它从当前位置开始动画。

我编写了一个指令,将 PreventDefault 添加到所有 anchor 。

请参阅下面的 JS fiddle 。 http://jsfiddle.net/ygNWF/10/

精简代码:

HTML:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>

<body ng-app="app" ng-controller="appController">
<script type="text/ng-template" id="home-template">
<div class="page" id="page-17" >
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-18">Page 18</a></div>
</div>
<div class="page" id="page-18">
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-19">Page 19</a></div>
</div>
<div class="page" id="page-19">
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-20">Page 20</a></div>
</div>
<div class="page" id="page-20">
<div class="page-inner" ng-style="style()" resize><a href="#/home/page-17">Page 17</a></div>
</div>
</script>

<div class="wrapper">
<div class="view" ng-view scroll></div>
</div>
</body>

javascript:

var app = angular.module('app', []);

/*
Controllers
*/
app.controller( 'appController', function( $scope ) {

});
app.controller( 'routeController', function( $scope, $routeParams ) {
//When route change, check if page has been updated and run animateScroll directive
if(typeof $routeParams !== 'undefined' && $routeParams.page.length > 0){
$scope.animateScroll($routeParams.page);
}
});

/*
Page routes
*/
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/home/:page', {
templateUrl: 'home-template',
controller: 'routeController'
})

.otherwise({
redirectTo: '/home/'
});
}]);

/*
Directives
*/
app.directive('scroll', function ($routeParams, $timeout) {
/*
Scroll to element ID
*/
return {
restrict: 'A',
link: function(scope, elm, attrs) {
scope.animateScroll = function(page) {
$timeout(function(){
console.log('test');
if(jQuery('#' + page).length == 1){
jQuery('html, body').animate({
scrollTop: jQuery('#' + page).position().top
});
};
}, 1);
};
}
};
});

app.directive('resize', function ($window) {
return function (scope, element) {
var w = angular.element($window);

scope.getWindowDimensions = function () {
return { height: w.height() };
};

scope.$watch(scope.getWindowDimensions, function (dimensions) {
scope.windowHeight = dimensions.height;
scope.style = function () {
return {
'min-height': scope.windowHeight + 'px'
};
};
}, true);

w.bind('resize', function () {
scope.$apply();
});
}
});

app.directive('a', function() {
return {
restrict: 'E',
link: function(scope, elem, attrs) {
elem.on('click', function(e){
e.preventDefault();
});
}
};
});

页面布局示例:

enter image description here

最佳答案

我不确定滚动是否应该与路由混合在一起。我最好做这样的事情:1.使用ng-click代替href,以及href="javascript:"(如果支持IE8则需要)2. 使用向下滚动函数接收元素的 id。

下面是一个例子

http://jsfiddle.net/naS4U/

关于jquery - AngularJS滚动到元素防止浏览器跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20072390/

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