gpt4 book ai didi

javascript - 与 hammer.js 的 Knockout 绑定(bind)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:47 24 4
gpt4 key购买 nike

我正在尝试结合 Hammer.js 和 knockout.js,但似乎有些东西不起作用。

我使用了这个函数:

var events = ['tap', 'doubletap', 'hold', 'rotate',
'drag', 'dragstart', 'dragend', 'dragleft', 'dragright', 'dragup',
'dragdown', 'transform', 'transformstart',
'transformend', 'swipe', 'swipeleft', 'swiperight',
'swipeup', 'swipedown', 'pinch', 'pinchin', 'pinchout'];

ko.utils.arrayForEach(events, function (eventName) {
ko.bindingHandlers[eventName] = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var origParams = ko.utils.unwrapObservable(valueAccessor()),
params = typeof(origParams) == 'function' ? {handler: origParams} : origParams,
hammerObj = hammer(element, params.options),
args = params.delegate ? [eventName, params.delegate, params.handler] : [eventName, params.handler]

hammerObj.on.apply(hammerObj, args);
}
}


});

例如,我正在尝试为页面上的每个图像获取拖动事件,因此在我的 HTML 上我有:

<div class="row-fluid" data-bind="foreach: {data: picturesArray}">
<ul class="thumbnails" >
<li class="img-item" data-bind="
doubletap: img.doubletap.bind(img),
drag: img.drag.bind(img)">
<img class="one-img"
style="width: 100px; height: 100px;" data-bind="attr: { src: img.media.m }"/>
</li>
</ul>
</div>

但它不起作用。在我的 JS 文件中,绑定(bind)方法没有被调用。

最佳答案

我是这样解决的。

http://plnkr.co/edit/WJnPNsHQIKbbUu7oaD12?p=preview

Javascript

var events = ['tap', 'doubletap', 'hold', 'rotate',
'drag', 'dragstart', 'dragend', 'dragleft', 'dragright', 'dragup',
'dragdown', 'transform', 'transformstart',
'transformend', 'swipe', 'swipeleft', 'swiperight',
'swipeup', 'swipedown', 'pinch', 'pinchin', 'pinchout'];

ko.utils.arrayForEach(events, function (eventName) {
ko.bindingHandlers[eventName] = {
update: function(element, valueAccessor){
var BindingContext = valueAccessor()[0];
var EventToFire = valueAccessor()[1];

var options = {
dragLockToAxis: true,
dragBlockHorizontal: true
};

var hammerTime = new Hammer(element, options);
hammerTime.on(eventName, function(ev){
//Fire the event with the item it was bound to.
EventToFire(BindingContext);
});
}
}

});

HTML

<div data-bind="foreach: Elements">
<!-- send yourself and the binding event to the "drag" handler -->
<div class="square" data-bind="drag: [$data, $parent.Drag]">
<div data-bind="text: Test"></div>
</div>
</div>

关于javascript - 与 hammer.js 的 Knockout 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23290871/

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