CSS3 diamond puzzle realizes the function of rotating only div and not rotating the background image

CSS3 diamond puzzle realizes the function of rotating only div and not rotating the background image

Demand background

The project is made using Vue, and the business requirement has a puzzle effect. The default background image is dark, and it is divided into five areas. One area will be lit each time, and the whole image will be lit, and the puzzle is completed. Let’s take a look at the final rendering first.

CSS3 diamond puzzle to rotate only the div and not the background image

Applied knowledge points:

  • background-size
  • background-position
  • transform:rotate

Implementation ideas:

The outer large box is used to hold the dark background image. The five puzzle pieces inside are five divs. Each puzzle piece uses背景定位的方式to display a fixed area of ​​the background image. The puzzle piece in the middle is rotated. But after the rotation, another problem occurred: the background image also rotated, like this:

CSS3 diamond puzzle to rotate only the div and not the background image

The idea to solve the background rotation problem is: you can put a box on the outer layer of the 5th puzzle piece, rotate the outer box 45 degrees to the right, and then rotate the background elements 45 degrees to the left. After the operation, I found a new problem. The black border in the picture below is the outer box (for ease of viewing, the other puzzle pieces are set to invisible). If overflow:hidden is set for the outer box, the top, bottom, left, and right corners of the middle puzzle will be missing.

We need to make the width and height of the fifth puzzle piece larger, and then set its outer box overflow:hidden . If the width and height are set large, the corresponding background-size must also be changed.

CSS3 diamond puzzle to rotate only the div and not the background image

Finally, you can achieve the effect you just started with. The complete Vue code is as follows:

<template>
    <section class="box">
        <div class="bg" :style="{ backgroundImage: `url(${bgImg}) ` }"></div>
        <div v-if="item1Show" class="item item1" :style="{ backgroundImage: `url(${bgImg}) ` }"></div>
        <div v-if="item2Show" class="item item2" :style="{ backgroundImage: `url(${bgImg}) ` }"></div>
        <div v-if="item3Show" class="item item3" :style="{ backgroundImage: `url(${bgImg}) ` }"></div>
        <div v-if="item4Show" class="item item4" :style="{ backgroundImage: `url(${bgImg}) ` }"></div>
        <div v-if="item5Show" class="item item5wrap">
            <div class="item5" :style="{ backgroundImage: `url(${bgImg}) ` }"></div>
        </div>
    </section>
</template>
<script>
import bgImg from "@/assets/bg.jpeg";
export default {
    data() {
        return {
            bgImg,
            item1Show: false,
            item2Show: true,
            item3Show: true,
            item4Show: true,
            item5Show: true
        };
    }
};
</script>
<style lang="less">
@borderColor: #333;
.box {
    margin: 0px auto;
    width: 300px;
    height: 200px;
    background-color: #000;
    position: relative;
    .bg {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        background-position: center center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        opacity: 0.5;
    }
    .item {
        width: 50%;
        height: 50%;
        box-sizing: border-box;
        background-size: 200% 200%;
        border-right: 1px solid @borderColor;
        border-bottom: 1px solid @borderColor;
        position: absolute;
    }
    .item1 {
        background-position: 0 0;
        left: 0;
        top: 0;
    }
    .item2 {
        background-position: 100% 0;
        left: 50%;
        top: 0;
    }
    .item3 {
        background-position: 0 100%;
        left: 0;
        top: 50%;
    }
    .item4 {
        background-position: 100% 100%;
        left: 50%;
        top: 50%;
    }
    .item5wrap {
        width: 100px;
        height: 100px;
        border-left: 1px solid @borderColor;
        border-top: 1px solid @borderColor;
        transform: rotate(45deg);
        left: 50%;
        top: 50%;            
        transform: translate(-50%, -50%) rotate(45deg);
        overflow: hidden;
        .item5 {
            width: 150px;
            height: 150px;
            background-position: 50% 50%;
            background-size: 200% 133%;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%) rotate(-45deg);
        }
    }
}
</style>

This is the end of this article about how to use CSS3 diamond puzzle to rotate only the div background image without rotating it. For more relevant CSS3 rotating image content, please search 123WORDPRESS.COM's previous articles or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  How to use iframe to apply the data of other web pages while maintaining compatibility

>>:  How to implement communication between Docker containers

Recommend

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

Detailed explanation of Vuex environment

Table of contents Build Vuex environment Summariz...

MySQL 8.0.13 manual installation tutorial

This article shares the manual installation tutor...

How to implement batch deletion of large amounts of data in MySQL large tables

The question is referenced from: https://www.zhih...

The implementation process of ECharts multi-chart linkage function

When there is a lot of data to be displayed, the ...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

How to authorize all the contents of a folder to a certain user in Linux?

【Problem Analysis】 We can use the chown command. ...

MySQL executes commands for external sql script files

Table of contents 1. Create a sql script file con...

Explain TypeScript enumeration types in detail

Table of contents 1. Digital Enumeration 2. Strin...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

Summarize some general principles of web design and production

<br />Related articles: 9 practical suggesti...

CenOS6.7 mysql 8.0.22 installation and configuration method graphic tutorial

CenOS6.7 installs MySQL8.0.22 (recommended collec...

Calling Baidu Map to obtain longitude and latitude in Vue

In the project, it is necessary to obtain the lat...

Detailed steps for installing and configuring MySQL 8.0 on CentOS

Preface Here are the steps to install and configu...