Detailed explanation of using backgroundImage to solve the image carousel switching

Detailed explanation of using backgroundImage to solve the image carousel switching

Implementing carousel with a single DOM node

You can use backgroundImage to add multiple images and offset them to achieve a carousel effect.

  • Create a div and attach an image to it using backgroundImage
  • Use backgroundPosition to adjust the position
  • Using css3 transition to adjust transition
  • It can replace simple image switching
  /**
        * Play picture */
    function playImage(src) {
        if (animaitionFinshed) return;
        if (!_imageEl) {
            _imageEl = document.createElement('div')
            _imageEl.className = `swiper_container`;
            _imageEl.style.backgroundImage = `url(${src.url})`;
            _imageEl.setAttribute("data-img", src.url);
            elContainer.appendChild(_imageEl);
        } else {
            animaitionFinshed = true;
            let width = elContainer.clientWidth, height = elContainer.clientHeight;
            let preImage = _imageEl.getAttribute("data-img");
            _imageEl.style.backgroundImage = `url(${preImage}),url(${src.url})`;
            _imageEl.style.backgroundPositionX = `center,${width + 20}px`;
            setTimeout(() => {
                _imageEl.style.transition = "all 0.8s ease";
                _imageEl.style.backgroundPositionX = `-${width + 20}px,center`;
            }, 0);

            setTimeout(() => {
                _imageEl.style.transition = "none";
                _imageEl.style.backgroundImage = `url(${src.url}) `;
                _imageEl.style.backgroundPositionX = `center`;
                _imageEl.setAttribute("data-img", src.url)
                animaitionFinshed = false;
            }, 800)
        }
    }

source code

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.

<<:  Summary of the differences between Html, sHtml and XHtml

>>:  Example of deploying MySQL on Docker

Recommend

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

Detailed explanation of Angular data binding and its implementation

Table of contents Preface What is data binding? T...

IIS 7.5 uses URL Rewrite module to achieve web page redirection

We all know that Apache can easily set rewrites f...

Analysis of MySQL query sorting and query aggregation function usage

This article uses examples to illustrate the use ...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Ajax responseText parses json data case study

Solve the problem that the responseText returned ...

In-depth explanation of JavaScript this keyword

Table of contents 1. Introduction 2. Understand t...

How much do you know about JavaScript inheritance?

Table of contents Preface The relationship betwee...

Pay attention to the use of HTML tags in web page creation

HTML has attempted to move away from presentation...

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define ...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

About if contains comma expression in JavaScript

Sometimes you will see English commas ",&quo...

A brief discussion on MySQL large table optimization solution

background The amount of new data in the business...

How to set process.env.NODE_ENV production environment mode

Before I start, let me emphasize that process.env...

The difference between delete, truncate, and drop and how to choose

Preface Last week, a colleague asked me: "Br...