我试图触发 Knockout BindingHandler 作为 document.body.scrolltop 值大于或等于特定值的直接结果。我试图根据声明创建一个可观察的。首先,这可能吗?或者我应该将布尔结果作为计算的一部分进行更新?
scrollPosition: KnockoutObservable<boolean> = ko.observable(document.body.scrollTop >= 200)
我也试过:
scrollPosition: KnockoutComputed<boolean> = ko.computed(() => {
if (document.body.scrollTop >= 100) {
return true;
}
else {
return false;
}
});
其余相关代码为:
HTML
<a href="javascript:void(0);" id="topLink" data-bind="topScroll: scrollPosition"><i class="glyphicon glyphicon-stats"></i></a>
CSS
#topLink {
position: fixed;
bottom: 40px;
right: 40px;
background: rgba(72,72,72,1);
width: 50px;
height: 50px;
display: none;
text-decoration: none;
-webkit-border-radius: 35px;
-moz-border-radius: 35px;
border-radius: 35px;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
绑定处理程序
ko.bindingHandlers.topScroll = {
update: function (element, valueAccessor) {
// This will be called once when the binding is first applied to an element,
// and again whenever any observables/computeds that are accessed change
// Update the DOM element based on the supplied values here.
var value = valueAccessor();
if (ko.unwrap(value)) {
$(element).css("display", "block");
}
}
};
我的目标是在顶部滚动超出特定值时显示返回顶部样式链接。有人可以指出我哪里出错了吗?