[英]Scrolling works only after refreshing the page
On my first page I have some buttons (promo1, promo2, promo3,...) who respectively lead to the element with the id "promo1" or "promo2" or "promo3",... on the second page. 在我的第一页上,我有一些按钮(promo1,promo2,promo3等),它们分别指向第二页上标识为“ promo1”或“ promo2”或“ promo3” ...的元素。
So when I land to the second page I want to scroll directly to the element I choosed with the button. 因此,当我进入第二页时,我想直接滚动到使用按钮选择的元素。 So I pass the id like that:
所以我像这样传递ID:
mysite.com/secondpage/:promo1(/2/3, depends on the button.)
and I get the id by using $routeParams
from angularJs
. 我用得到的ID
$routeParams
从angularJs
。 But I have a really huge problem: the scroll stop on middle of the page (maybe because it stop when the element is viewable on screen, let me know), but if I refresh the page, the scroll do what I ask for and stop on top on the element! 但是我有一个非常大的问题:滚动停止在页面中间(也许是因为当元素在屏幕上可见时它停止了,让我知道),但是如果刷新页面,滚动会执行我要求的并停止在元素之上!
Here is the code I use at the moment: 这是我目前使用的代码:
$scope.goToAnchor = function(){
// This is just one of the test I've made, does not work better.
// if ($routeParams.anchor == ':promoFamily' || $routeParams.anchor == ':promoGroup' || $routeParams.anchor == ':promoCard'){
// $scope.anchor = $routeParams.anchor.replace(':','#');
// $('html, body').animate({
// scrollTop: $($scope.anchor).offset().top
// }, 200);
// }
// Code I use:
if ($routeParams.anchor == ':promoFamily' || $routeParams.anchor == ':promoGroup' || $routeParams.anchor == ':promoCard'){
$location.hash("#");
$location.hash($routeParams.anchor.replace(':',''));
$anchorScroll();
}
};
$scope.goToAnchor();
Event if you have only some hints, be free to leave a comment, it will help me as long as i'am working on this for 2 days now... 如果您仅提供一些提示,请随时发表评论,只要我现在在此工作2天,它将对我有帮助...
This is because your elements are not fully loaded when your goToAnchor function is being called. 这是因为在调用goToAnchor函数时,元素没有完全加载。 Use
$timeout
to call $scope.goToAnchor()
after 1 second(change it according to your page load time). 1秒钟后使用
$timeout
调用$scope.goToAnchor()
(根据您的页面加载时间进行更改)。
First, inject $timeout
in your controller. 首先,在控制器中注入
$timeout
。 Then, 然后,
$timeout(function() {
$scope.goToAnchor();
}, 1000);
Inject $timeout in your controller, and use it like below. 在您的控制器中注入$ timeout,然后像下面那样使用它。
$timeout(function() {
$scope.goToAnchor();
});
This will help calling the function after rendering the full view. 这将有助于在渲染完整视图后调用该函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.