Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue mousemove only after mousedown

How can I trigger a mousemove only if the element is clicked first? I'm trying to utilize this for an audio player timeline.

.player__time--bar(@mousedown="setNewCurrentPosition($event)")
    .slider(role="slider" aria-valuemin="0" :aria-valuenow="currentPosition" :aria-valuemax="trackTotalDuration" aria-orientation="horizontal")
        .player__time--bar-current-position(:style="{width:  (100 / (trackTotalDuration / currentPosition)) + '%'}")

The method:

setNewCurrentPosition(e) {
    let tag = e.target
    // if the click is not on 'slider', grab div with class 'slider'
    if (e.target.className === 'player__time--bar') tag = e.target.firstElementChild
    else if (e.target.className === 'player__time--bar-current-position') tag = e.target.parentElement
    const pos = tag.getBoundingClientRect()
    const seekPos = (e.clientX - pos.left) / pos.width
    this.currentPosition = parseInt(this.trackTotalDuration * seekPos)
    // updates the time in the html
    this.$refs.player.currentTime = this.currentPosition
},
like image 923
Giacomo Avatar asked Jan 08 '18 14:01

Giacomo


People also ask

How many times MouseMove event updating after mouseDown 265 Vue 2?

2 mousemove event updating only once after mousedown 265 Vue 2 - Mutating props vue-warn 279 Vue v-on:click does not work on component 9 Simulate click on/change value of aria's (netflix) slider programatically

How do I get the mouse to move an element?

You will want to have a mousedownlistener on your element that sets a variable to indicate dragging started. Put a listener on the window to catch mouseupanywhere and unset the variable. You can put mousemoveon the element if you are only interested in dragging that happens inside the element.

How do I use the mouse event?

The main mouse event that you’ll generally be using is the click event. When you call this, you simply pass in the name of a function to be called upon clicking the element. Here’s an example: In this example, v-on:click="showAlert" is used in the HTML side.

How do you catch mouse movement in JavaScript?

Put a listener on the window to catch mouseupanywhere and unset the variable. You can put mousemoveon the element if you are only interested in dragging that happens inside the element. Otherwise you can put the mousemovelistener on windowso you catch it everywhere.


1 Answers

You will want to have a mousedown listener on your element that sets a variable to indicate dragging started. Put a listener on the window to catch mouseup anywhere and unset the variable.

You can put mousemove on the element if you are only interested in dragging that happens inside the element. Otherwise you can put the mousemove listener on window so you catch it everywhere.

new Vue({
  el: '#app',
  data: {
    dragging: false,
    x: 'no',
    y: 'no'
  },
  methods: {
    startDrag() {
      this.dragging = true;
      this.x = this.y = 0;
    },
    stopDrag() {
      this.dragging = false;
      this.x = this.y = 'no';
    },
    doDrag(event) {
      if (this.dragging) {
        this.x = event.clientX;
        this.y = event.clientY;
      }
    }
  },
  mounted() {
    window.addEventListener('mouseup', this.stopDrag);
  }
});
.dragstartzone {
  background-color: red;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app">
  <div class="dragstartzone" @mousedown="startDrag" @mousemove="doDrag">Start dragging here</div>
  <div>X: {{x}}, Y: {{y}}</div>
</div>
like image 60
Roy J Avatar answered Oct 22 '22 05:10

Roy J