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

布局结构:

1

<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

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 Article Tags

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)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

What are the security implications of using iframes, and how can I mitigate them? What are the security implications of using iframes, and how can I mitigate them? Mar 18, 2025 pm 02:51 PM

What are the security implications of using iframes, and how can I mitigate them?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

How do I use the HTML5 <meter> element to display numerical data within a range? How do I use the HTML5 <meter> element to display numerical data within a range? Mar 12, 2025 pm 04:08 PM

How do I use the HTML5 <meter> element to display numerical data within a range?

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

How do I use the HTML5 <time> element to represent dates and times semantically?

See all articles