VueJS: Get left, top position of element?

后端 未结 2 1692
耶瑟儿~
耶瑟儿~ 2021-02-09 12:36

While converting legacy jquery code, I stuck at getting position of element.

My goal is display floating div above a tag.

Here is my

2条回答
  •  梦如初夏
    2021-02-09 13:15

    this.markInfoBusStop.markerInfoStyle.left = left + 'px';
    

    Above isn't reactive.

    1. use Vue.set

    See Doc

    2. use computed

    for example (you still need to customize to fit your needs.)

    data

    {
     left: 200,
     top: 200,
    }
    

    method

    onMouseEnterBusStop: function(ev) {
      this.left = ev.clientX;
      this.top = ev.clientY;
    }
    

    computed

    markerInfoStyle: function(){
      return {
        left: this.left + 'px',
        top: this.top + 'px'
      }
    }
    

提交回复
热议问题