gpt4 book ai didi

javascript - 使用 knockout.js 将 Flash 新元素添加到 dom 中的数组

转载 作者:行者123 更新时间:2023-11-30 11:16:48 30 4
gpt4 key购买 nike

我有一个逐个播放数组,它使用 knockout 和一个新元素进行更新。无论如何,在添加字体以获得视觉效果时,是否可以将字体的颜色更改为红色并变回白色。我可以使用下面的绑定(bind)轻松地在单个属性上执行此操作,但我如何在添加到 dom 的新对象上完成此操作。

ko.bindingHandlers.flash = {

init: function (element, valueAccessor) {},

update: function (element, valueAccessor) {

var updated = valueAccessor().updated;

if (updated()) {
$(element).addClass('flash');
setTimeout(function () {
$(element).removeClass('flash');
}, 1000);

updated(false);
}
}
};

CSS

.flash {
color: red;
-webkit-transition: color 0.4s ease;
-moz-transition: color 0.4s ease;
-o-transition: color 0.4s ease;
transition: color 0.4s ease;
}

HTML

<div class="accordion-inner period-play-by-play" data-bind="foreach: plays">
<div class="play" data-bind="css: {'home-team': $data.IsHomeTeam, 'away-team': !$data.IsHomeTeam, 'modified': $data.isNew}">
<b data-bind="text: $data.Time + ' '"></b>
<span data-bind="text: $data.Team + ' ' + $root.actionName($data.Action)"></span>
<span data-bind="text: $root.actionTypeName($data.ActionType)"></span>
<span>by <i data-bind="text: $data.Name"></i></span>
<!-- ko if: $data.Lead != '' -->
<div class="pull-right" data-bind="text: $data.Lead"></div>
<!-- /ko -->
</div>
</div>

最佳答案

您可以使用 afterAdd foreach 绑定(bind)来为新添加的元素设置动画。无需创建自定义绑定(bind)(如 flash)

更多信息请访问 knockout foreach

var vm = {
plays: ko.observableArray([{
IsHomeTeam: Math.random() >= 0.5,
isNew: true,
Time: new Date().toLocaleString(),
Team: 'default team',
Action: 'default action',
ActionType: 'default action type',
Name: 'default name',
Lead: Math.random() >= 0.5
}]),
actionName(name) {
return name;
},
actionTypeName(name) {
return name;
},
flashAnimate(element) {
$(element).addClass('flash');
setTimeout(function() {
$(element).removeClass('flash');
}, 1000);
},
addNew(vm) {
vm.plays.push({
IsHomeTeam: Math.random() >= 0.5,
isNew: true,
Time: new Date().toLocaleString(),
Team: 'new team',
Action: 'new action',
Name: 'new name',
Lead: Math.random() >= 0.5
});
}
};

ko.applyBindings(vm);
.play {
-webkit-transition: color 0.4s ease;
-moz-transition: color 0.4s ease;
-o-transition: color 0.4s ease;
transition: color 0.4s ease;
display: flex;
flex-direction: column;
margin: 10px;
padding: 10px;
border: 1px solid;
}

.flash {
color: red;
}
<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.4.2/knockout-min.js"></script>
<div class="accordion-inner period-play-by-play" data-bind="foreach: { data: plays, afterAdd: flashAnimate }">
<div class="play" data-bind="css: {'home-team': $data.IsHomeTeam, 'away-team': !$data.IsHomeTeam, 'modified': $data.isNew}">
<b data-bind="text: $data.Time + ' '"></b>
<span data-bind="text: $data.Team + ' ' + $root.actionName($data.Action)"></span>
<span data-bind="text: $root.actionTypeName($data.ActionType)"></span>
<span>by <i data-bind="text: $data.Name"></i></span>
<!-- ko if: $data.Lead != '' -->
<div class="pull-right" data-bind="text: $data.Lead"></div>
<!-- /ko -->
</div>
</div>
<button style="margin: 0 10px" data-bind="click: addNew">Add New</button>

关于javascript - 使用 knockout.js 将 Flash 新元素添加到 dom 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251965/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com