Vue.js is my first reactive library and I'm having trouble with methods and child elements. I suspect I'm missing something obvious.
In my example, the rendered listitems have method handlers for hover (mouseenter & mouseleave) events. Each listitem is a parent of a <video> element, paused by default. When the mouse enters a listitem, its child <video> plays (elem.play()). The video pauses when the mouse leaves the listitem.
Full example: https://jsfiddle.net/matbergman/hcgtkbwt/8/
<ul class="contentList">
<li v-for="item in items" v-on:mouseover="videoPlay()" v-on:mouseleave="videoPause()">
<video loop="loop" v-bind:src="item.video"></video>
<p>
{{item.description}}
</p>
</li>
</ul>
<script>
var vm = new Vue ({
el: ".contentList",
data: {
items: [
{
video : "http://206.130.101.116/misc/video01.mp4",
description : "Video 1"
},
{
video : "http://206.130.101.116/misc/video02.mp4",
description : "Video 2"
},
{
video : "http://206.130.101.116/misc/video03.mp4",
description : "Video 3"
}
]
},
methods: {
videoPlay: function() {
console.log("play");
vm.$el === document.getElementById('example')
// Play the child video, something like: this.getElementsByTagName("video")[0].play();
},
videoPause: function() {
console.log("pause");
// Pause the child video, something like: this.getElementsByTagName("video")[0].pause();
}
}
});
</script>
My method recognizes the mouse events. How do I apply the play() method to the child <video>? In jQuery it would be something like:
$("li").mouseenter(function() {
this.getElementsByTagName("video")[0].play();
});
But the scope of this is of course defined by my $el variable instead of the element with the attached event like I'm used to. Would the play() method be passed somehow as a property of a component?