document.body.scrolltop 値が特定の値以上であることの直接的な結果として、Knockout BindingHandler をトリガーしようとしています。ステートメントに基づいてオブザーバブルを作成しようとしました。まず、これは可能ですか?または、計算の一部としてブール値の結果を更新する必要がありますか?
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;
}
BindingHandler
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");
}
}
};
私の目的は、トップスクロールが特定の値を超えると、トップに戻るスタイルのリンクを表示することです。誰かが私が間違っているところを指摘できますか?