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

Introduction to installing and configuring JDK under CentOS system

Table of contents Preface Check and uninstall Ope...

How to use DQL commands to query data in MySQL

In this article, the blogger will take you to lea...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

How to use webSocket to update real-time weather in Vue

Table of contents Preface About webSocket operati...

How to connect to MySQL remotely through Navicat

Using Navicat directly to connect via IP will rep...

Detailed explanation of how to enter and exit the Docker container

1 Start the Docker service First you need to know...

Detailed explanation of nginx-naxsi whitelist rules

Whitelist rule syntax: BasicRule wl:ID [negative]...

Vue integrates a rich text editor that supports image zooming and dragging

need: According to business requirements, it is n...

Linux system dual network card binding configuration implementation

System version [root@ ~]# cat /etc/redhat-release...

Two ways to install the Linux subsystem in Windows 10 (with pictures and text)

Windows 10 now supports Linux subsystem, saying g...

How to use nginx as a proxy cache

The purpose of using cache is to reduce the press...

How to make JavaScript sleep or wait

Table of contents Overview Checking setTimeout() ...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...