我有一个div元素,可以从一个函数动态获取两个不同的动画gif。
<div style="width:100px; height:100px" id="img0" data-bind="style : {backgroundImage: img()}"></div>
<button data-bind="click: function(data, event) { changeImg(); }">SWITCH IMG</button>
在加载页面时,将显示第一个动画gif,并且单击按钮时,也会正确显示第二个动画。
self.changeImg = function () {
self.imgIndex(!self.imgIndex());
};
self.img = function () {
if(self.imgIndex())
return "url(GIF1)";
else
return "url(GIF2)"
};
return self;
}
但是,如果尝试单击第二次,则不会显示动画,但只会显示gif的最后一帧。
片段:https://jsfiddle.net/rickhaar/0keu6prx/40/
在保持base64格式的情况下,每次更改图像时,是否有任何方法可以重新启动动画?
答案 0 :(得分:2)
这有点骇人听闻。每当imgIndex
值更改时,请将src
设置为空字符串,然后再将其设置为实际图像。然后动画重新开始**
function vm() {
var self = this;
self.img = ko.observable(image1);
self.imgIndex = ko.observable(true);
self.changeImg = function() {
self.imgIndex(!self.imgIndex());
};
self.imgIndex.subscribe(() => {
self.img(''); // or some transparent icon
if (self.imgIndex()) {
setTimeout(() => self.img(image1))
} else {
setTimeout(() => self.img(image2))
}
});
return self;
}
const image1 = "data:image;base64,R0lGODlhZABkALMPAP8DA3D/cFL/Uq7/ro//jwD/ABf/FzX/Nff/99f/18b/xub/5iX/JQn/Cf84OP///yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDIgNzkuMTYwOTI0LCAyMDE3LzA3LzEzLTAxOjA2OjM5ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXBNTT0iaHR0cD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8MlJq7046827/2AojmRpnmgVLEfrvnAsz3Rt3/IibELh/8CgcEgsGo/IIoDXSDqf0OhxqelJr9jssEHNCJrasBi60wTG6LSSp267u5gAYE6v2+/4vH7P7+sDKYGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3ugREAIfkEBQIADwAsBQADAFkACAAABDyQMedmpTbjfbv23CeGJGhmFqWubOu+cCzPr0TfeK7P9u7/wFovSCzihsaksoVcOpOSqHRKrVqv2Kx2GwEAOw==";
ko.applyBindings(vm());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<img style="width:100px; height:100px" id="img0" data-bind="attr : {src: img}" />
<button data-bind="click: function(data, event) { changeImg(); }">SWITCH IMG</button>
**一些条件:
我已从div
更改为img
,因为前者无效。我也经历了其他一些答案。
在瞬间,浏览器的图像未找到图标出现。您可以
通过将self.img
设置为一个小的透明图标而不是一个空字符串,可以避免这种情况:
self.img("/images/small-icon.png")
此代码段似乎工作正常。但是在jsfidle中,有时动画没有重启