最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mouse-Over event Lit-ElementPolymer - Stack Overflow

programmeradmin0浏览0评论

Is there a way to create a custom event in in Lit-Element/Polymer, such as a mouse-over event? I've been searching for this a while now, but I can seem to find a way of doing it. I know about events in Lit-Element, like @click, but nothing about mouse-over events.

Is there a way to create a custom event in in Lit-Element/Polymer, such as a mouse-over event? I've been searching for this a while now, but I can seem to find a way of doing it. I know about events in Lit-Element, like @click, but nothing about mouse-over events.

Share Improve this question asked Mar 12, 2019 at 22:06 Ian GuimarãesIan Guimarães 4314 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This can be done using lit-html @event bindings.

For the mouseover event type, use @mouseover="${this.handleMouseover}"

For more information on lit-element event listeners, see https://lit-element.polymer-project/guide/events

Live example:

<script src="https://unpkg./@webponents/webponentsjs@latest/webponents-loader.js"></script>
<script type="module">
  import { LitElement, html, css } from 'https://unpkg./lit-element/lit-element.js?module';
  
  class MyElement extends LitElement {
    static get styles() {
      return [
        css`
          span {
            padding: 4px 8px;
            border: 2px solid orange;
          }
        `
      ];
    }
    
    render() {
      return html`
        <span
          @mouseover="${this.handleEvent}"
          @mouseleave="${this.handleEvent}">
          Hover me
        </span>
      `;
    }
    
    handleEvent(e) {
      console.log(`Event type: ${e.type}`);
    }
  }
  customElements.define('my-element', MyElement);
</script>
<my-element></my-element>

So I just figured it out, and just going to post here if anyone has the same difficulty.

You have to use CustomEvents, here some code example:

in your element's firstUpdate method you should add a new EventListener

firstUpdated(){
    this.addEventListener('mouseover', this.mouseOverHandler);
}

and declare the method

   mouseOverHandler(){
       console.log('Hello');
}

Simple as that!!!

发布评论

评论列表(0)

  1. 暂无评论