[英]Move element up against window scroll in a directive then apply fixed position
I'm using an angular directive and binding the scroll on the window element. 我正在使用angular指令并将滚动条绑定到window元素上。 On scroll, I want to move the element against the direction of the scroll up. 在滚动时,我想将元素逆着向上滚动的方向移动。 Then, when boundingRect
reaches 50, I want to apply the fixed positioning. 然后,当boundingRect
达到50时,我想应用固定位置。 It's not working... What am I missing? 它不起作用...我想念什么?
Directive 指示
app.directive('liftToTop', ['$window', function($window){
return {
restrict: 'A', //attributes only
link: function(scope, element, attrs) {
const w = angular.element($window),
topClass = attrs.liftToTop,
initialOffset = element.offset().top;
//bind the the scroll event
w.bind('scroll', function(){
console.log(this.pageYOffset);
let currentTop = element[0].getBoundingClientRect().top; //get current pos
if(currentTop > 50) {
//move element up/down against the scroll direction
element.css('top', -1 * this.pageYOffset + 'px');
element.removeClass(topClass);
}
//once current rect reaches 50, apply fixed
if(currentTop === 50) {
element.addClass(topClass);
}
});
}
};
}]);
CSS CSS
.then-fixed-to-top-10 {
position:fixed;
top: 50px;
}
Markup 标记
<h1 lift-to-top="then-fixed-to-top-10">{{hello}}</h1>
Here's non-working Plnkr 这是无效的Plnkr
https://plnkr.co/edit/n4dQDzwK5T6e3TqWGlR3?p=preview https://plnkr.co/edit/n4dQDzwK5T6e3TqWGlR3?p=preview
If I understood you correctly, the the main problem was the way you measured scroll value. 如果我对您的理解正确,则主要的问题是测量滚动值的方式。 Also, the last if statement had to be changed from ===
to >
: 另外,最后的if语句必须从===
更改为>
:
let currentTop = $(window).scrollTop(); //get current pos
if(currentTop < 50) {
//move element up/down against the scroll direction
element.css('top', -1 * this.pageYOffset + 'px');
element.removeClass(topClass);
}
//once current rect reaches 50, apply fixed
if(currentTop > 50) {
element.addClass(topClass);
}
To make the h1
stick without jumping to the left just add width:100%
to the class: 要使h1
棍子不跳到左侧,只需将class的width:100%
添加:
.then-fixed-to-top-10 {
position: fixed;
top: 50px;
width: 100%;
}
I needed to divide the initial offset by two to meet halfway. 我需要将初始偏移量除以2才能达到一半。 I also needed to set the moving div width to equal the parent div to prevent jumping. 我还需要将移动div的宽度设置为等于父div的宽度,以防止跳跃。
Directive 指示
app.directive('liftToTop', function($window){
return {
restrict: 'A',
link: function(scope, element, attrs){
const w = angular.element($window),
topClass = attrs.liftToTop,
initialOffset = element.offset().top;
w.bind('scroll', function(){
let currentTop = w.scrollTop(); //get current pos
if(currentTop < initialOffset / 2) {
//move element up/down against the scroll direction
element.css('top', -1 * this.pageYOffset + 'px');
element.removeClass(topClass);
}
//once current rect reaches 50, apply fixed
if(currentTop > (initialOffset / 2)) {
element.addClass(topClass);
element.removeAttr('style');
}
});
}
};
});
CSS CSS
.fixed-to-top-10 {
position:fixed;
top: 0px;
width: 400px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.