📜  如何使用 HTML 和 CSS 创建 Image Stack Illusion?

📅  最后修改于: 2021-08-31 07:24:38             🧑  作者: Mango

在本文中,我们将在主图像下方创建图像错觉。它与旧版android中的图库图像集相同。这是一个简单的项目,我们只能通过使用 HTML 和 CSS 来实现我们的目标。

项目概况:

方法:

  • 创建一个主 div,我们将在其中存储其他 3 个 div。
  • 使用 CSS 中的伪类来创建背后的效果。
  • 使用@keyframes 为我们的页面提供一些过渡。

HTML代码:

  1. 首先,我们创建一个 HTML 文件(index.html)。
  2. 创建 HTML 文件后,我们将使用 标签为我们的网页提供标题。它应该放在 <head> 标签内。</li> <li>然后我们将提供所有动画效果的 CSS 文件链接到我们的 html。它也被放置在 <head> 标签内。</li> <li>现在,我们添加来自 Google Fonts 的链接,以便在我们的项目中使用不同类型的字体系列。</li> <li>来到我们 HTML 代码的正文部分。 <ol> <li>首先,我们正在向我们的页面提供标题。</li> <li>然后,我们必须创建一个 div,我们可以在其中存储所有其他 div</li> </ol> </li> </ol> <div class="noIdeBtnDiv"> <div> <h5 class="code">HTML</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace"><!DOCTYPE html> <html lang="en">    <head>     <link rel="stylesheet" href="style.css">     <link rel="preconnect" href="https://fonts.gstatic.com">     <link href= "https://fonts.googleapis.com/css2?family=Big+Shoulders+Display&display=swap"            rel="stylesheet"></head>    <body>     <h1>image stack illusion</h1>     <div class="container">         <div class="img0"></div>         <div class="img1"></div>         <div class="img2"></div>     </div> </body>    </html></code></pre> </div> <p></p> <hr/> <h5 class="code">CSS</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">* {     margin: 0;     padding: 0;     box-sizing: border-box; }    /* Giving all of the general styles    to the body */ body {     background: rgb(0, 0, 0);     display: flex;     justify-content: center;     top: 10px;     margin-top: 5em;     font-family: 'Big Shoulders Display', cursive; }    h1 {     color: rgb(240, 147, 8);     animation: todown 5s;     text-transform: uppercase; }    .container {     width: 50.5em;     height: 30em;     position: absolute;     top: 50%;     left: 50%;     transform: translate(-50%, -50%);     display: flex; }    .img0 {     position: relative;     width: 100%;     height: 100%;     margin-right: 3em;     background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20201230184929/htmlcssjslogo.png);     background-size: cover;     background-position: left;     animation: shrink 1s; }    .img0::after, .img0::before {     content: '';     position: absolute;     z-index: -1;     width: 100%;     height: 100%; }    .img0::after {     background: rgb(255, 255, 255);     transform: rotate(5deg); }    .img0::before {     background: rgb(50, 205, 50);     transform: rotate(-5deg); }    .img1 {     width: 100%;     height: 100%;     position: relative;     left: 3em;     margin-right: 3em;     background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20201230184929/htmlcssjslogo.png);     background-size: cover;     background-position: center; }    .img1::after, .img1::before {     content: '';     position: absolute;     z-index: -1;     width: 100%;     height: 100%; }    .img1::after {     background: rgb(255, 255, 255);     transform: rotate(3deg); }    .img1::before {     background: rgb(50, 205, 50);     transform: rotate(-3deg); }    .img2 {     width: 100%;     height: 100%;     position: relative;     left: 10em;     background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20201230184929/htmlcssjslogo.png);     background-size: cover;     background-position: right;     animation: shrink 1s; }    .img2::after, .img2::before {     content: '';     position: absolute;     z-index: -1;     width: 100%;     height: 100%; }    .img2::after {     background: rgba(255, 255, 255, 0.829);     transform: rotate(7deg); }    .img2::before {     background: rgba(50, 205, 50, 0.836);     transform: rotate(-7deg); }    /* Animations Effect */ @keyframes todown {     0% {         opacity: 0;         transform: translateY(-150px);     }     60% {         opacity: 0.6;         transform: translateY(1.2em);         transform: scale(0.5, 0.5);         color: rgb(0, 0, 255);     }     100% {         opacity: 1;         transform: translateY(0);     } }    @keyframes shrink {     0% {         transform: scale(0, 0);     }     50% {         transform: scale(0.5, 0.5);     }     100% {         transform: scale(1, 1);     } }</code></pre> </div> <p></p> <hr/> </div> </div> <p> <strong>CSS 代码:</strong> CSS 用于为我们的 HTML 页面提供不同类型的动画和效果,使其看起来对所有用户都是交互的。在 CSS 中,我们必须记住以下几点——</p> <ul> <li>恢复所有浏览器效果。</li> <li>使用类和 id 为 HTML 元素赋予效果。</li> <li>使用@keyframes 为浏览器提供动画/过渡效果。</li> <li>使用 ::before 和 ::after 等伪类。</li> <li> z-index 的使用。</li> </ul> <div class="noIdeBtnDiv"> <div class="responsive-tabs"> <h2 class="tabtitle"> CSS </h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace">* {     margin: 0;     padding: 0;     box-sizing: border-box; }    /* Giving all of the general styles    to the body */ body {     background: rgb(0, 0, 0);     display: flex;     justify-content: center;     top: 10px;     margin-top: 5em;     font-family: 'Big Shoulders Display', cursive; }    h1 {     color: rgb(240, 147, 8);     animation: todown 5s;     text-transform: uppercase; }    .container {     width: 50.5em;     height: 30em;     position: absolute;     top: 50%;     left: 50%;     transform: translate(-50%, -50%);     display: flex; }    .img0 {     position: relative;     width: 100%;     height: 100%;     margin-right: 3em;     background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20201230184929/htmlcssjslogo.png);     background-size: cover;     background-position: left;     animation: shrink 1s; }    .img0::after, .img0::before {     content: '';     position: absolute;     z-index: -1;     width: 100%;     height: 100%; }    .img0::after {     background: rgb(255, 255, 255);     transform: rotate(5deg); }    .img0::before {     background: rgb(50, 205, 50);     transform: rotate(-5deg); }    .img1 {     width: 100%;     height: 100%;     position: relative;     left: 3em;     margin-right: 3em;     background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20201230184929/htmlcssjslogo.png);     background-size: cover;     background-position: center; }    .img1::after, .img1::before {     content: '';     position: absolute;     z-index: -1;     width: 100%;     height: 100%; }    .img1::after {     background: rgb(255, 255, 255);     transform: rotate(3deg); }    .img1::before {     background: rgb(50, 205, 50);     transform: rotate(-3deg); }    .img2 {     width: 100%;     height: 100%;     position: relative;     left: 10em;     background-image: url( https://media.geeksforgeeks.org/wp-content/uploads/20201230184929/htmlcssjslogo.png);     background-size: cover;     background-position: right;     animation: shrink 1s; }    .img2::after, .img2::before {     content: '';     position: absolute;     z-index: -1;     width: 100%;     height: 100%; }    .img2::after {     background: rgba(255, 255, 255, 0.829);     transform: rotate(7deg); }    .img2::before {     background: rgba(50, 205, 50, 0.836);     transform: rotate(-7deg); }    /* Animations Effect */ @keyframes todown {     0% {         opacity: 0;         transform: translateY(-150px);     }     60% {         opacity: 0.6;         transform: translateY(1.2em);         transform: scale(0.5, 0.5);         color: rgb(0, 0, 255);     }     100% {         opacity: 1;         transform: translateY(0);     } }    @keyframes shrink {     0% {         transform: scale(0, 0);     }     50% {         transform: scale(0.5, 0.5);     }     100% {         transform: scale(1, 1);     } } </code></pre> </div> </div> </div> </div> <p>将以上两段(HTML 和 CSS 代码)代码组合起来,在浏览器上运行。它将显示图像堆栈错觉效果。</p> <p><strong>输出:</strong> <br/><img alt="" class="img-fluid" height="790" loading="lazy" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/How_to_create_Image_Stack_Illusion_using_HTML_and_CSS_?_1.jpg" width="1472"/></p> <div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7"></div> <p></p></div> </div> </div> </div> </div> </div> <footer> <div class="bg-white text-center pb-1"> <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有 <br> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a> <br> Made with ❤️ in Chengdu </p> </div> </footer> <!-- 引入Bootstrap JavaScript库 --> <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <!-- 引入Meilisearch搜索相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script> <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script> <!-- 引入prismjs代码高亮相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script> </body> </html>