Vue implements the frame rate playback of the carousel

Vue implements the frame rate playback of the carousel

This article example shares the specific code of Vue to achieve the frame rate playback of the carousel for your reference. The specific content is as follows

need

Pass in an array containing directory names. Through this directory name, read all the pictures in the file directory and play them in a loop, forming an effect of playing a certain number of pictures every 1 second. After the last directory is played, jump to the first directory and play it in a loop.

Core: Use an API of webpack require.contex to read the file name in the directory. If you want to know more, you can check it out.

Code

HTML

<template>
 <div id="imgPlay" ref="container" :style="[style]">
 <img :src="imgsrc" :style="[{height:style.height,width:style.width}]">
 <div id="but">
  <button @click="start()">Start</button>
  <button @click="stop()">Stop</button>
 </div>
 </div>
</template>

javascript

<script>
export default {
 name: 'ZxImgPlay',
 data () {
 return {
  style:[
 width:"50px",
 height:"50px"
 ],
  interval: null, // timer id
  flag: true, // Timer switch setIntervalNumber: 0, // Current displayed image subscript imgsrc: "", // Image path to be displayed imgUrls: [], // All image paths frameRate: 0 // Frame rate }
 },
 computed: {},
 watch: {},
 created () { },
 mounted () {
 this.zxInit()
 },
 beforeDestroy () { },

 methods: {
 zxInit() {
 //This.DisplayParam is a set of things within the company, and the mixed object //this.DisplayParam.frameRate is an array ["Directory Name 1", "Directory Name 2"]
 // this.DisplayParam.imgUrls is a dead image. When there is no directory, the dead image is used. // this.DisplayParam.frameRate is the incoming frame rate this.frameRate = this.DisplayParam.frameRate && (1000 / this.DisplayParam.frameRate)
  this.imgUrls = this.DisplayParam.imgUrls
  this.DisplayParam.imageFileName != 0 ? this.readdir(this.DisplayParam.imageFileName) : this.showImages(true)
 },

 start () {
  if (this.flag) return
  this.showImages()
  this.flag = true
 },

 stop () {
  this.flag = false
  clearInterval(this.interval)
 },

 readImages(imageFileName, _A) {
  this.stop()
  let req = require.context("@/../static/images", true, /\.(?:bmp|jpg|gif|jpeg|png)$/).keys();
  let path = new RegExp(imageFileName[_A])
  req.forEach(item => {
  if (path.test(item)) {
   this.imgUrls.push({ img: "@/../static/images/" + imageFileName[_A] + item.substring(item.lastIndexOf('/')) })
  }
  })
  this.showImages()
 },
 readdir (imageFileName) {
  this.imgUrls = []
  for (let i = 0; i < imageFileName.length; i++) {
  this.readImages(imageFileName, i)
  }
 },

 showImages(_B) {
  if (_B) this.imgUrls = this.imgUrlsSort(this.imgUrls, 'sort')
  console.log(this.imgUrls)
  this.interval = setInterval(this.setIntervalFun, this.frameRate)
 },

 imgUrlsSort (ary, key) {
  return ary.sort((a, b) => {
  let x = a[key];
  let y = b[key];
  return ((x < y) ? -1 : (x > y) ? 1 : 0)
  })
 },

 setIntervalFun () {
  if (this.setIntervalNumber >= this.imgUrls.length) {
  this.setIntervalNumber = 0
  }
  this.imgsrc = this.imgUrls[this.setIntervalNumber++].img || ''
 }
 }
}
</script>

question

The above has achieved the function, but two problems have been found so far

1. The first parameter of the require.context() API cannot use a mutable value, such as a variable, and a warning will be issued.
2. The above code is implemented by constantly changing the src of the image, that is, each time the src is changed, an http request will be sent to obtain the image, resulting in the memory not being released and constantly increasing.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Use python-opencv to read the video and calculate the total number of video frames and FPS
  • Unity3D displays FPS in real time based on OnGUI
  • Unity OnGUI displays game FPS in real time
  • Python reads the video, processes it, and calculates the frame rate fps in real time
  • How to measure the number of frames per second (FPS) in Android
  • How to calculate the frame rate FPS of web animations

<<:  How to Dockerize a Python Django Application

>>:  What to do if you forget the initial password of MySQL on MAC

Recommend

Example code for configuring monitoring items and aggregated graphics in Zabbix

1. Install Zabbix Agent to monitor the local mach...

JavaScript Dom Object Operations

Table of contents 1. Core 1. Get the Dom node 2. ...

MySQL loop inserts tens of millions of data

1. Create a test table CREATE TABLE `mysql_genara...

Version numbers in css and js links in HTML (refresh cache)

background Search the keyword .htaccess cache in ...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

Four ways to create objects in JS

Table of contents 1. Create objects by literal va...

What is ssh port forwarding? What's the use?

Table of contents Preface 1. Local port forwardin...

JavaScript implements class lottery applet

This article shares the specific code of JavaScri...

List rendering instructions for efficient development of Vue front-end

v-for directive Speaking of lists, we have to men...

Solution to failure in connecting to mysql in docker

Scenario: After installing the latest version of ...

MySQL string splitting example (string extraction without separator)

String extraction without delimiters Question Req...

Solve the problem that the time zone cannot be set in Linux environment

When changing the time zone under Linux, it is al...