javascript - 在触摸事件上获取未定义的 "Cannot read property ' 偏移量

标签 javascript jquery angularjs

我正在使用 Angular 内置的颜色选择器,并且我一直在添加代码以使其在触摸屏上工作。但是,我似乎在代码的某些部分(即 mousedown 处理程序内部)遇到了障碍。每当我启动触摸事件时,都会收到无法读取未定义的属性“offset”错误。

这也会导致桌面网络浏览器无响应 - 我无法顺利单击并拖动选择器。

Plunker Demo

这是给我错误的代码块:

Farbtastic.prototype.mousedown = function(event) {
    // Capture mouse
    if (!this.dragging) {
      $(document).on('mousemove', this.mousemove)
        .on('mouseup', this.mouseup);
      this.dragging = true;
    }

    // Update the stored offset for the widget.
    this.offset = this.$container.offset();

    // Check which area is being dragged
    var pos = this.widgetCoords(event);
    this.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) > (this.square + 2);

    // Process
    this.mousemove(event);
    return false;
  };

我对这个问题真的很迷茫,所以非常感谢任何帮助。

最佳答案

您需要在页面加载后使用它:

$( window ).onload(function() {
  this.offset = this.$container.offset();
}).bind(this);

关于javascript - 在触摸事件上获取未定义的 "Cannot read property ' 偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36036361/

相关文章:

javascript - 从外部脚本中的函数捕获错误

javascript - 缺少上下文 JavaScript

javascript - 以 "/"结尾的剩余请求适用于 Chrome,不适用于 Firefox

javascript - Ramda 过滤器(如果不匹配)

javascript - 使用轮播滚动浏览日期

jquery - Bootstrap popover.toggle() 仅显示

javascript - 与 Controller 和自定义指令相关的 Angular JS

css - 在 ASP.NET/MVC5 中交付动态样式表的标准或快速方法是什么?

javascript - 在 javascript 中使用重写合并两个对象

javascript - JQuery.css(property, value) 导致 404 not found 错误