首页 > 其他分享 >自定义事件

自定义事件

时间:2023-01-04 16:22:38浏览次数:43  
标签:return 自定义 type 事件 property data event containers

dom自定义事件

// 事件监听
document.addEventListener('drag:start', function(ev) {
  console.log(ev.target)
})

// 触发事件
const dom = document.getElementById("target")
const event = document.createEvent("Event")
event.initEvent("drag:start", true, true)
event.detail = {
  x: 100
}
dom.dispatchEvent(event)

事件描述

AbstractEvent

const canceled = Symbol('canceled');
export default class AbstractEvent {
  /**
   * Event type
   * @static
   * @abstract
   * @property type
   * @type {String}
   */
  static type = 'event';

  /**
   * Event cancelable
   * @static
   * @abstract
   * @property cancelable
   * @type {Boolean}
   */
  static cancelable = false;

  /**
   * AbstractEvent constructor.
   * @constructs AbstractEvent
   * @param {object} data - Event data
   */
  constructor(data) {
    this[canceled] = false;
    this.data = data;
  }

  /**
   * Read-only type
   * @abstract
   * @return {String}
   */
  get type() {
    return this.constructor.type;
  }

  /**
   * Read-only cancelable
   * @abstract
   * @return {Boolean}
   */
  get cancelable() {
    return this.constructor.cancelable;
  }

  /**
   * Cancels the event instance
   * @abstract
   */
  cancel() {
    this[canceled] = true;
  }

  /**
   * Check if event has been canceled
   * @abstract
   * @return {Boolean}
   */
  canceled() {
    return Boolean(this[canceled]);
  }

  /**
   * Returns new event instance with existing event data.
   * This method allows for overriding of event data.
   * @param {Object} data
   * @return {AbstractEvent}
   */
  clone(data) {
    return new this.constructor({
      ...this.data,
      ...data,
    });
  }
}

SensorEvent

export class SensorEvent extends AbstractEvent {
  /**
   * Original browser event that triggered a sensor
   * @property originalEvent
   * @type {Event}
   * @readonly
   */
  get originalEvent() {
    return this.data.originalEvent;
  }

  /**
   * Normalized clientX for both touch and mouse events
   * @property clientX
   * @type {Number}
   * @readonly
   */
  get clientX() {
    return this.data.clientX;
  }

  /**
   * Normalized clientY for both touch and mouse events
   * @property clientY
   * @type {Number}
   * @readonly
   */
  get clientY() {
    return this.data.clientY;
  }

  /**
   * Normalized target for both touch and mouse events
   * Returns the element that is behind cursor or touch pointer
   * @property target
   * @type {HTMLElement}
   * @readonly
   */
  get target() {
    return this.data.target;
  }

  /**
   * Container that initiated the sensor
   * @property container
   * @type {HTMLElement}
   * @readonly
   */
  get container() {
    return this.data.container;
  }

  /**
   * Draggables original source element
   * @property originalSource
   * @type {HTMLElement}
   * @readonly
   */
  get originalSource() {
    return this.data.originalSource;
  }

  /**
   * Trackpad pressure
   * @property pressure
   * @type {Number}
   * @readonly
   */
  get pressure() {
    return this.data.pressure;
  }
}

DragMoveSensorEvent

export class DragMoveSensorEvent extends SensorEvent {
  static type = 'drag:move';
}
const dragStartEvent = new DragMoveSensorEvent({
      clientX: startEvent.clientX,
      clientY: startEvent.clientY,
      target: startEvent.target,
      container,
      originalSource,
      originalEvent: startEvent,
    });

感知器

Sensor

感知器:鼠标感知器、触摸板感知器...

export default class Sensor {
  /**
   * Sensor constructor.
   * @constructs Sensor
   * @param {HTMLElement[]|NodeList|HTMLElement} containers - Containers
   * @param {Object} options - Options
   */
  constructor(containers = [], options = {}) {
    console.log(options);
    /**
     * Current containers
     * @property containers
     * @type {HTMLElement[]}
     */
    this.containers = [...containers];

    /**
     * Current options
     * @property options
     * @type {Object}
     */
    this.options = {...options};

    /**
     * Current drag state
     * @property dragging
     * @type {Boolean}
     */
    this.dragging = false;

    /**
     * Current container
     * @property currentContainer
     * @type {HTMLElement}
     */
    this.currentContainer = null;

    /**
     * Draggables original source element
     * @property originalSource
     * @type {HTMLElement}
     */
    this.originalSource = null;

    /**
     * The event of the initial sensor down
     * @property startEvent
     * @type {Event}
     */
    this.startEvent = null;

    /**
     * The delay of each sensor
     * @property delay
     * @type {Object}
     */
    this.delay = calcDelay(options.delay);
  }

  /**
   * Attaches sensors event listeners to the DOM
   * @return {Sensor}
   */
  attach() {
    return this;
  }

  /**
   * Detaches sensors event listeners to the DOM
   * @return {Sensor}
   */
  detach() {
    return this;
  }

  /**
   * Adds container to this sensor instance
   * @param {...HTMLElement} containers - Containers you want to add to this sensor
   * @example draggable.addContainer(document.body)
   */
  addContainer(...containers) {
    this.containers = [...this.containers, ...containers];
  }

  /**
   * Removes container from this sensor instance
   * @param {...HTMLElement} containers - Containers you want to remove from this sensor
   * @example draggable.removeContainer(document.body)
   */
  removeContainer(...containers) {
    this.containers = this.containers.filter((container) => !containers.includes(container));
  }

  /**
   * 在目标元素上触发感知事件
   * @param {HTMLElement} element - Element to trigger event on
   * @param {SensorEvent} sensorEvent - Sensor event to trigger
   */
  trigger(element, sensorEvent) {
    const event = document.createEvent('Event');
    event.detail = sensorEvent;
    event.initEvent(sensorEvent.type, true, true);
    element.dispatchEvent(event);
    this.lastEvent = sensorEvent;

    return sensorEvent;
  }
}

MouseSensor

鼠标感知器

export default class MouseSensor extends Sensor {
}

标签:return,自定义,type,事件,property,data,event,containers
From: https://www.cnblogs.com/zhuxiang1633/p/17025207.html

相关文章

  • GridView 自定义简单操作
    usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSy......
  • app直播源代码,vue 自定义指令过滤特殊字符
    app直播源代码,vue自定义指令过滤特殊字符 /** *@tagsinput只可以輸入数字、字母、汉字 *@examplev-emoji */exportdefault(app)=>{ app.directive('emoj......
  • 5.2常见事件演示
    ​  鼠标事件 <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title><style>......
  • pillow 创建图片并添加一些自定义信息
    fromPILimportImagevm=Image.new('RGBA',(dshape[1],dshape[0]))vm=Image.fromarray(np.array(ssddff,dtype=np.uint8))fromPIL.PngImageP......
  • Spring IOC官方文档学习笔记(六)之自定义bean的特性
    1.生命周期回调(1)如果我们想要介入bean的生命周期,可通过实现spring中的InitializingBean和DisposableBean接口来达到这一目的,spring会调用InitializingBean中的afterPro......
  • 消息队列和事件循环
    消息队列和事件循环系统来驱动浏览器页面浏览器页面是由消息队列和事件循环系统来驱动的。渲染进程专门有一个IO线程用来接收其他进程传进来的消息,接收到消息之后,会......
  • js 之二 事件对象
    事件对象/当事件的响应函数被触发时,浏览器每次都会讲一个事件对象作为实参传递响应函数;在事件对象中封装了当前事件相关的一切信息,比如:鼠标的,键盘的操作//当鼠标在are......
  • 第二十章《Java Swing》第4节:事件处理与监听器
    ​当程序员向窗体上添加了按钮等组件之后就能够操作这些组件,但在20.3小节的各个案例中,虽然在窗体上添加了一些按钮,但点击这些按钮并没有任何反应,因此这些按钮也就成了毫无意......
  • C# 委托事件简介
    简单理解C#委托事件,示例如下所示:usingSystem;///<summary>///---------------------委托步骤---------------------///第一步:声明委托///第二步:委托使用方法的......
  • 关于uniapp多层弹窗click事件点击唤起textarea键盘的问题
    原因由于textarea在原生组件中优先级高,导致click事件会优先触发textarea的点击中间遇到的问题由于使用cover-view将组件优先级实现了覆盖textarea的优先级,但是弹窗中的......