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

Pure CSS code to achieve flow and dynamic line effects

Ideas: An outer box sets the background; an inner...

Detailed explanation of linux crm deployment code

Linux basic configuration Compile and install pyt...

Implementation of MySQL's MVCC multi-version concurrency control

1 What is MVCC The full name of MVCC is: Multiver...

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...

js to achieve the effect of light switch

This article example shares the specific code of ...

Some tips on website design

In fact, we have been hearing a lot about web des...

How to understand Vue's simple state management store mode

Table of contents Overview 1. Define store.js 2. ...

MySQL calculates the number of days, months, and years between two dates

The MySQL built-in date function TIMESTAMPDIFF ca...

JavaScript data structure bidirectional linked list

A singly linked list can only be traversed from t...

Tips for Mixing OR and AND in SQL Statements

Today, there is such a requirement. If the logged...

How to use Dockerfile to build images in Docker

Build the image Earlier we used various images fo...

This article will show you the principle of MySQL master-slave synchronization

Table of contents Brief Analysis of MySQL Master-...