繁体   English   中英

带CSS动画的淘汰赛输入验证

[英]Knockout input validation w/ CSS animation

我为使用KnockoutJS的应用程序创建了仅数字输入验证器。 当用户按下非法按键(字母)时,我希望目标输入在每次非法按键上闪烁CSS动画。 按照目前的情况,它只会触发一次动画,我不确定为什么。

示例: http//codepen.io/anon/pen/oIamG/

有任何想法吗?

问题是您需要重置CSS动画。 为此,您需要删除动画,然后再次添加它。
将-webkit-animation属性移至输入,例如

input {
    border: 2px solid black;
    padding: 8px;
    /* 0.2s is used, just to see the effect, the animation needs to be  */
    -webkit-animation-duration: 0.2s;
    -webkit-animation-direction:forwards;
    -webkit-animation-timing-function:ease-in-out;
}

@-webkit-keyframes inputValidationBorder {
    0% { border: 2px solid red; }
    100% { border: 2px solid black; }
}

然后使用以下绑定:

ko.bindingHandlers.numeric = {
    init: function (element, valueAccessor) {
        $(element).bind('webkitAnimationEnd', function(){
            this.style.webkitAnimationName = '';
        });

        $(element).on('keydown', function (event) {
            // allow backspace, delete, tab, escape, enter/return and period.
            // if input is a letter then disable keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96    || event.keyCode > 105)) {
               element.style.webkitAnimationName= 'inputValidationBorder'; 
               event.preventDefault();
            }
        });
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM