current position:Home>Using H5 video tag in Vue to play local video in pop-up window
Using H5 video tag in Vue to play local video in pop-up window
2022-04-29 16:33:09【xuejiachen】
Reference resources h5 < video > Use of labels
In the development process, the following points need to be paid attention to
1. Pop up window opening and closing
Use... In the parent component .sync binding visible attribute , You can directly close the pop-up window in the child component to update the information passed in by the parent component visible value ;
// The parent component references the child component
<video-modal :visible.sync="showVideoModal">
// Sub components
<el-dialog :visible="visible" @close="closeModal">
Copy code
// Pop-up window closing method
closeModal() {
this.$emit("updata:visible", false);
}
Copy code
However, such use will cause the progress bar to remain in the last open position every time the video pop-up window is reopened , Therefore, you need to nest another... Outside the referenced sub component div, utilize v-if Make it re render every time you open it , So as to solve the problem of progress bar caching ;
<div v-if="showVideoModal">
<video-modal :visible.sync="showVideoModal">
</div>
Copy code
2. Introduction of local video resource path
The video to be played is a static resource in the project , Because pop-up window components need to support reusability , So the video path cannot be written dead , The following example customizes the video name ;
<video ref="video" class="play-video" controls="controls" autoplay="autoplay" >
<source :src="require('@/assets/videos/' + videoName + '.mp4')" type="video/mp4" />
</video>
Copy code
If the video is not there src Under the table of contents , It is public The writing method under the directory is slightly different ;
computed: {
src() {
// Need to be in js Part is defined by calculation properties
return process.env.BASE_URL + "videos/" + this.videoName + ".mp4";
}
},
Copy code
3. Automatically close the pop-up window after the video is played
monitor video Labeled ended Event and close the pop-up window ,vue The definition in the life cycle needs to add this.$nextTick, Otherwise, the corresponding dom Elements ;
mounted() {
this.$nextTick(() => {
// Automatically close the pop-up window after playing
const eleVideo = document.querySelector(".play-video");
eleVideo.addEventListener("ended", () => {
this.closeVideoModal();
}, false);
});
},
Copy code
4. Adaptive size of video in pop-up window
to video Set the appropriate width and height of the label , Using object-fit: contain; Attribute is enough .
Finally, attach the complete code
<template>
<el-dialog class="video-dialog" :visible="visible" :title="title" width="75%" append-to-body @close="closeVideoModal"> <video ref="video" class="play-video" controls="controls" autoplay="autoplay" > <source :src="src" type="video/mp4" /> </video> </el-dialog>
</template>
<script> export default { name: "VideoModal", props: { visible: { type: Boolean, default: false }, // Parent component parameter transfer pop-up window title title: { type: String, default: "" }, // The parent component passes the name of the video to be played videoName: { type: String, default: "" } }, computed: { src() { return process.env.BASE_URL + "videos/" + this.videoName + ".mp4"; } }, mounted() { this.$nextTick(() => { // Automatically close the pop-up window after playing const eleVideo = document.querySelector(".play-video"); eleVideo.addEventListener("ended", () => { this.closeVideoModal(); }, false); }); }, methods: { closeVideoModal() { this.$emit("update:visible", false); } } }; </script>
<style lang="scss" scoped> .play-video { object-fit: contain; width: 100%; height: 99.5%; } </style>
Copy code
copyright notice
author[xuejiachen],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204291633044360.html
The sidebar is recommended
- HTTP becomes HTTPS, self-made certificate
- Web front-end operation - tourism enterprise marketing publicity responsive website template (HTML + CSS + JavaScript)
- Self inspection list of a [qualified] front-end Engineer
- This principle in JavaScript and six common usage scenarios
- JavaScript this priority
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
guess what you like
Loki HTTP API usage
JavaScript - prototype, prototype chain
Front end experience
JavaScript -- Inheritance
HTTP cache
Filters usage of table in elementui
A JavaScript pit encountered by a non front-end siege lion
Grain College - image error when writing Vue with vscode
Utility gadget - get the IP address in the HTTP request
Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer
Random recommended
- Function of host parameter in http
- Use nginx proxy node red under centos7 and realize password access
- Centos7 nginx reverse proxy TCP port
- In eclipse, an error is reported when referencing layuijs and CSS
- Front end online teacher Pink
- Learn to use PHP to insert elements at the specified position and key of the array
- Learn how to use HTML and CSS styles to overlay two pictures on another picture to achieve the effect of scanning QR code by wechat
- Learn how to use CSS to vertically center the content in Div
- Learn how to use CSS to circle numbers
- Learn to open and display PDF files in HTML web pages
- The PHP array random sorting function shuffle() randomly scrambles the order of array elements
- JQuery implements the keyboard enter search function
- 16 ArcGIS API for JavaScript 4.18 a new development method based on ES modules @ ArcGIS / core
- 17 ArcGIS API for JavaScript 4.18 draw points, lines and faces with the mouse
- 18 ArcGIS API for JavaScript 4.18 obtain the length and area after drawing line segments and surface features
- Vue environment construction -- scaffold
- Build a demo with Vue scaffold
- Using vuex in Vue projects
- Use Vue router in Vue project
- 26 [react basics-5] react hook
- 07 Chrome browser captures hover element style
- WebGIS development training (ArcGIS API for JavaScript)
- Solution to the blank page of the project created by create react app after packaging
- 19. Html2canvas implements ArcGIS API for JavaScript 4 X screenshot function
- Introduction to JavaScript API for ArcGIS 13
- Development of ArcGIS API for JavaScript under mainstream front-end framework
- Nginx learning notes
- Less learning notes tutorial
- Vue2 cannot get the value in props in the data of the child component, or it is always the default value (the value of the parent component comes from asynchrony)
- LeetCode 217. Determine whether there are duplicate elements in the array
- I'll write a website collection station myself. Do you think it's ok? [HTML + CSS + JS] Tan Zi
- Front end browser debugging tips
- Application of anti chattering and throttling in JavaScript
- How to create JavaScript custom events
- Several ways of hiding elements in CSS
- node. Js-3 step out the use of a server and express package
- CSS matrix function
- Fastapi series - synchronous and asynchronous mutual conversion processing practice
- How to extend the functionality of Axios without interceptors
- Read pseudo classes and pseudo elements