Home Web Front-end HTML Tutorial CSS3实现3D效果的图片墙_html/css_WEB-ITnose

CSS3实现3D效果的图片墙_html/css_WEB-ITnose

Jun 24, 2016 am 11:18 AM

先来看一下效果:http://1.huizit1.applinzi.com/CSS/transform_3D/img_3D.html

布局结构:

<div class="container">        <img  src="/static/imghw/default1.png"  data-src="../Img/1.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/2.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/3.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/4.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/5.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/6.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/7.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/8.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/9.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >        <img  src="/static/imghw/default1.png"  data-src="../Img/10.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >    </div>
Copy after login

CSS3中新增了translate-style和perspective属性,要让图片有3D的效果就要添加这两个属性,具体的解释这里不再赘述,可以看这篇文章来理解:http://www.zhangxinxu.com/wordpress/2012/09/css3-3d-transform-perspective-animate-transition/

给container添加translate-style为preserve-3d,添加perspective: 2000px;

这里一共10张图片,为了让10张图片围成一个圆,需要添加position属性为absolute,设置宽度相同,居中,这时所有图片都重合在了一起。每张图片绕Y轴旋转36*i(i:0->9)度(rotateY),然后每张图片在Z轴方向移动相同的距离(translateZ),这个距离能保证图片不重合在一起就行。这时图片就围成了一个环状,并且是有3D效果的。然后给container添加动画属性让其绕Y轴不停旋转(rotateY),这时动画就出现了。

注意:给图片添加的属性transform: rotateY(0deg) translateZ(350px);rotateY和translateZ的位置不能交换,因为先旋转后移动和先移动后旋转的效果是不一样的。

这里我给container添加了背景颜色来参考图片的相对位置和旋转轴。

如果要用鼠标点击来切换图片的话,只需要每次点击之后container的旋转角度加36度就可以。

贴上代码:

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>img_3D</title> 6 </head> 7 <style type="text/css"> 8     @keyframes an1{ 9         0%{10             transform: rotateY(0deg)  ;11         }12         50%{13             transform: rotateY(180deg)  ;14         }15         100%{16             transform: rotateY(360deg) ;17         }18     }19     .container{20         width: 900px;21         height: 400px;22         background: rgba(255,0,0,0.5);23         /*opacity: 0.3;*/24 25         margin: 200px auto;26         perspective: 2000px;27         transform-style: preserve-3d;28         animation: an1 10s linear 0s infinite;29     }30     .container img{31         width: 200px;32         height: auto;33         margin: auto;34         top: 0;35         bottom: 0;36         left: 0;37         right: 0;38         position: absolute;39     }40     .container img:nth-child(1){41         transform: rotateY(0deg) translateZ(350px);42     }43     .container img:nth-child(2){44         transform: rotateY(36deg) translateZ(350px);45     }46     .container img:nth-child(3){47         transform: rotateY(72deg) translateZ(350px);48     }49     .container img:nth-child(4){50         transform: rotateY(108deg) translateZ(350px);51     }52     .container img:nth-child(5){53         transform: rotateY(144deg) translateZ(350px);54     }55     .container img:nth-child(6){56         transform: rotateY(180deg) translateZ(350px);57     }58     .container img:nth-child(7){59         transform: rotateY(216deg) translateZ(350px);60     }61     .container img:nth-child(8){62         transform: rotateY(252deg) translateZ(350px);63     }64     .container img:nth-child(9){65         transform: rotateY(288deg) translateZ(350px);66     }67     .container img:nth-child(10){68         transform: rotateY(324deg) translateZ(350px);69     }70 </style>71 <body>72     <div class="container">73         <img  src="/static/imghw/default1.png"  data-src="../Img/1.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >74         <img  src="/static/imghw/default1.png"  data-src="../Img/2.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >75         <img  src="/static/imghw/default1.png"  data-src="../Img/3.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >76         <img  src="/static/imghw/default1.png"  data-src="../Img/4.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >77         <img  src="/static/imghw/default1.png"  data-src="../Img/5.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >78         <img  src="/static/imghw/default1.png"  data-src="../Img/6.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >79         <img  src="/static/imghw/default1.png"  data-src="../Img/7.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >80         <img  src="/static/imghw/default1.png"  data-src="../Img/8.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >81         <img  src="/static/imghw/default1.png"  data-src="../Img/9.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >82         <img  src="/static/imghw/default1.png"  data-src="../Img/10.jpg"  class="lazy" alt="CSS3实现3D效果的图片墙_html/css_WEB-ITnose" >83     </div>84 </body>85 </html>
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles