将类添加到Knockout组件
Add Class to Knockout Component
我正在尝试创建一个knockout组件,它将在屏幕底部显示通知。这目前工作得很好,但我打算能够添加更多的动画通知。理想情况下,我想做的是有通知立即开始褪色超过5秒。5秒后,它将从通知数组中删除,因此元素将被删除。但是,如果用户将鼠标放在通知上,则淡出序列应该完全停止。但是,目前我不知道如何以这种方式访问通知的dom元素。谁能推荐一个解决方案?
<HTML绑定/strong>
<div class="notification-container" data-bind="foreach: notificationArray">
<notification params="data: $data"></notification>
</div>
通知JS
appViewModel.notificationArray = ko.observableArray([]);
appViewModel.setNotification = function( message ){
appViewModel.notificationArray.push({
'message':message
})
};
组件JS
ko.components.register('notification', {
viewModel: function(params) {
var data = params.data;
this.message = data.message || null;
this.timer = null;
this.removeNotification = function() {
appViewModel.notificationArray.remove(data);
};
this.timer = ( function(self) {
return setTimeout(function() {
self.removeNotification();
}, 5000);
})(this);
this.hover = function () {
clearTimeout(this.timer);
};
this.restart = function() {
this.timer = ( function(self) {
return setTimeout(function() {
self.removeNotification();
}, 5000);
})(this);
}
},
template: '<div class="notification show-notification" data-bind="event: { mouseover: hover, fastClick: hover, mouseout: restart }">'
+'<div class="notifications-close clickable right" data-bind="fastClick: removeNotification"><span class="icon icon-x"></span></div>'
+'<div class="notification-text" data-bind="html: message"></div>'
+'</div>'
});
——更新显示完成代码——
完成代码的最终结果是通知现在将从屏幕跳出来,并立即开始在5秒内消失。如果点击通知,将停止淡出/从数组中移除(由于移动支持,我删除了鼠标悬停事件)。希望这对其他人有帮助。
HTML绑定 -同上
通知JS -同上
自定义衰落绑定
ko.bindingHandlers.fadeVisible = {
init: function(element, valueAccessor) {
$(element).height(); //need to trigger re-paint in order for transition to fire
$(element).addClass('show-notification');
},
update: function(element, valueAccessor) {
var value = valueAccessor();
$(element).stop();
ko.unwrap(value) ? $(element).fadeIn(0) : $(element).fadeOut(5000);
}
};
更新组件JS
ko.components.register('notification', {
viewModel: function(params) {
var data = params.data;
this.message = data.message || null;
this.timer = null;
**this.paused = ko.observable(false);**
this.removeNotification = function() {
appViewModel.notificationArray.remove(data);
};
this.timer = ( function(self) {
return setTimeout(function() {
self.removeNotification();
}, 5000);
})(this);
this.pause = function () {
**this.paused(true);**
clearTimeout(this.timer);
};
},
template: '<div class="notification clickable" data-bind="fadeVisible: paused, fastClick: pause">'
+'<div class="notifications-close clickable right" data-bind="fastClick: removeNotification"><span class="icon icon-x"></span></div>'
+'<div class="notification-text" data-bind="html: message"></div>'
+'</div>'
});
有关CSS
.notification {
visibility: hidden;
max-height: 5.5rem;
overflow: hidden;
margin: 0.5rem;
border-radius: 0.25rem;
border: 1px solid rgb(72, 72, 72);
background: white;
box-shadow: -5px 5px 15px -3px rgba(0,0,0,0.75);
-webkit-transform: translate(100%, 0);
transform: translate(100%, 0);
-webkit-transition: -webkit-transform 0.3s cubic-bezier(0, 1.20, 0, 1.20);
transition: -webkit-transform 0.3s cubic-bezier(0, 1.20, 0, 1.20),transform 0.3s cubic-bezier(0, 1.20, 0, 1.20);
}
.show-notification {
visibility: visible;
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
}
您需要一个自定义绑定处理程序来处理褪色。如果你愿意,你也可以处理从绑定处理程序中删除,但我不能决定这是否是个好主意。
ko.components.register('notification', {
viewModel: function(params) {
var self = this;
var data = params.data;
this.message = data.message || null;
var timer = null;
this.removeNotification = function() {
appViewModel.notificationArray.remove(data);
};
this.hover = function() {
clearTimeout(timer);
};
this.restart = function() {
timer = setTimeout(function() {
self.removeNotification();
}, 5000);
}
this.restart();
},
template: '<div class="notification show-notification" data-bind="event: { mouseover: hover, fastClick: hover, mouseout: restart }, fadeOut:true">' + '<div class="notifications-close clickable right" data-bind="fastClick: removeNotification"><span class="icon icon-x"></span></div>' + '<div class="notification-text" data-bind="html: message"></div>' + '</div>'
});
ko.bindingHandlers.fadeOut = {
init: function(el) {
$(el).fadeOut(5000);
$(el).hover(function() {
$(el).stop().fadeIn(0);
},
function() {
$(el).fadeOut(5000);
});
}
}
appViewModel = {};
appViewModel.notificationArray = ko.observableArray([{
message: 'Hi there'
}]);
appViewModel.setNotification = function(message) {
appViewModel.notificationArray.push({
'message': message
})
};
ko.applyBindings(appViewModel);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="notification-container" data-bind="foreach: notificationArray">
<notification params="data: $data"></notification>
</div>
相关文章:
- Require JS with Knockout组件正在查找路径不正确的JS文件
- 使用Knockout组件时,请更换容器元件
- 在 Knockout 中初始化时调用组件函数
- 将嵌套的 Knockout.js 组件与 RequireJs 结合使用
- Requirejs带有Knockout错误:无法处理绑定“;组件:函数(){return f}"-匿名定义()
- 将dom节点传递到Knockout组件中(当使用RequireJS时)
- 通过AJAX调用将数据加载到Knockout组件中
- 异步加载Knockout组件中的模板
- 如何访问Knockout组件中的自定义元素
- knockout.js组件内容
- 如何访问foreach中Knockout组件中的数组成员
- 防止Knockout缓存加载的组件
- 从typescript模块中注册Knockout.js组件视图模型
- 将类添加到Knockout组件
- 使用没有视图模型. js文件的knockout组件
- 在页面上链接两个内部Knockout组件的最佳方式是什么?
- 当一个组件被加载到DOM中时,Knockout.js回调
- 在Knockout中向子组件传递参数
- 如何在不擦除/覆盖任何旧绑定的情况下将Knockout.js组件附加到页面
- Knockout组件视图在其ViewModel Observable更改时不更新