Trigger Knockout BindingHandler链接到document.body.scrollTop上的KnockoutObservable

时间:2016-05-10 10:06:34

标签: jquery html css knockout.js bindinghandlers

我试图触发Knockout BindingHandler作为document.body.scrolltop值大于或等于特定值的直接结果。我试图根据声明创建一个observable。首先,这可能吗?或者我应该将布尔结果更新为计算的一部分吗?

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");
        }
    }
};

我的目标是在topscroll超出特定值后显示返回顶部样式链接。有人可以指出我出错的地方吗?

2 个答案:

答案 0 :(得分:2)

  

......首先,这可能吗?或者我应该将布尔结果更新为计算的一部分吗?

     

scrollPosition = ko.observable(document.body.scrollTop >= 200)

您希望scrollPosition是动态的。您实际想要的是body.scrollTop是一个可观察的。但它不是。严格来说:没有,这是不可能的。

我建议的是在body元素上创建custom binding handler,以检测用户何时滚动(akin to this)并因此更新可观察的内容。

以下是您在视图中使用该代码的伪代码:

<body data-bind="changeOnScroll: shouldShowLink">

自定义绑定处理程序可以是这样的(伪代码):

ko.bindingHandlers.changeOnScroll = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here

        element.onscroll = function() {
            var obs = valueAccessor();
            obs(element.scrollTop > 200); // set the observable
        }
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // 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.

        // Nothing here, since it's a one-way binding from DOM to observable.
    }
};

我已经使用了onscroll,但如果你有可用于规范化事件处理的话,你也可以使用jQuery。

答案 1 :(得分:1)

我在找到Jeroen的答案之前,找到了一个我很满意的解决方案。我已将固定的true传递给BindingHandler数据绑定。

<a href="javascript:void(0);" id="topLink" data-bind="topScroll: true"><i class="glyphicon glyphicon-stats"></i></a>

然后将BindingHandler修改为以下内容......

ko.bindingHandlers.topScroll = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here
        $(window).scroll(function() {

            if ($(this).scrollTop() >= 150) {         // If page is scrolled more than 150px

                $(element).css("display", "block");  // show the arrow
            } else {
                $(element).css("display", "none");   // don't show the arrow
            }
        });

        $(element).click(function () {      // When arrow is clicked
            $('body,html').animate({
                scrollTop: 0                       // Scroll to top of body
            }, 'slow');
        });

    }    
};