首页 > 解决方案 > 为什么 touch 方法不能接收到 className 的参数?

问题描述

由于我发现它在传递函数时new className(param)并不直接引用 an ,因此我一直在尝试将 HTML 类传递给而不更改。但它不断检测.instance(param){}constructorinstance methodparam

这是我的代码:

class Slider {
  'use strict';

  istouchStart(e) {
    console.log(e.target);
    this.coordX = e.touches[0].clientX;
    return this.coordX;
  }
  constructor(target) {
    // Variables
    var _ = this;
    console.log(target);
    $(target).stop(true, true).on({
      touchstart:_.istouchStart,
      touchmove:_.istouchMove,
      touchend:_.istouchEnd
    });
  }
}
const image = new Slider('.inline-grid');
  

  div {
    position: relative;
  }
  .inline-grid {
    margin: 0 auto;
    width: 2560px;
    border: 1px solid black;
    display: flex;
    flex-flow: row;

    overflow: hidden;
  }
  .wrap {
    width: 100%;
    display: flex;
    flex-flow: row;
  }
  .cell {
    display: flex;
    flex-flow: column;
    flex-shrink: 0;
    width: 100%;
    height: 200px;
  }
  .orange {
    background-color: orange;
  }
<div class="inline-grid">
  <div class="wrap">
    <div class="cell orange"></div>
  </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我将如何.inline-grid从该istouchStart方法中获得?

这是使用return和箭头函数的第二次尝试,但是这个函数在没有显示错误的情况下不起作用。:

static istouchStart() {
  return (e) => {
    console.log(e.target);
    this.coordX = e.touches[0].clientX;
  }
}

标签: javascripthtmlclassvariablesprototype

解决方案


推荐阅读