css - knockout js CSS 绑定(bind)

标签 css knockout.js knockout-2.0

我在我的元素中使用 Knockout js,我需要向特定标签添加多个 css 类名。下面是我的代码,我有三个不同的类,请建议我如何将它添加到 knockout css 绑定(bind)中。

<img data-bind="attr: { src:ProfileImageSrcName }" class="tabUser profile-Image tabpic" />

最佳答案

你可以是 css像下面这样绑定(bind)。

var vm = {
  profileImageSrcName: ko.observable('http://cumbriaskills.wdfiles.com/local--files/files:images/metro_128_chrome.png'),
  isProfilePic: ko.observable(true),
  isTab: ko.observable(true),
  toggleProfile: function() { vm.isProfilePic(!vm.isProfilePic()); },
  toggleTab: function() { vm.isTab(!vm.isTab()); }
};

ko.applyBindings(vm);
.tabUser { width: 100px; height: 100px; object-fit: contain; }
.profile-image { border-radius: 100% } 
.tabPic { box-shadow: 0 2px 6px rgba(0,0,0,0.4) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<img data-bind="attr: { src: profileImageSrcName }, 
                css: { 
                  'profile-image': isProfilePic, /* single class */
                  'tabUser tabPic': isTab  /* multiple classes */
                }" />
<br><br>
<button data-bind="click: toggleProfile">Toggle "profile-image" Class</button><br>
<button data-bind="click: toggleTab">Toggle "tabUser" and "tabPic" Classes</button>

关于css - knockout js CSS 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071635/

相关文章:

javascript - 文本不随机刷新的问题

knockout.js - 导航到相同模块并使用不同数据查看时 Durandal 重新绑定(bind)

javascript - 在 Node.js 中安装 Postgres 模块时出错以及其他 2 个 node.js 问题

javascript - 在 Knockout JS 中处理多维数组的最佳方法是什么?

jquery - div 在另一个 div 之上滑动

html - IE窗口缩小时DIV溢出外层DIV(Restore-Down)

html - 是否可以在没有任何 CSS 的情况下设置样式?(仅使用类似应用程序的 photoshop)

javascript - Knockout - 更新 View 模型中的 observableArray

knockout.js - ko 计算未显示在 View 中

javascript - knockout JS : Checking if items in nested sortable were reordered