今天给大家带来自动轮播的multi-flip动画,o(^▽^)o,不知道什么是multi-flip没关系,来张效果图就懂了->
multi-flip2.gif
1.总体思路有正面7个div,背面7个div,重叠在一起,正面一张图片,背面另外一张图片(如一张纸的正反面画着不同的图画),正面旋转180度后显示背面的图片,并将原本正面div的图片替换成原本背面的div的图片,背面旋转180度,显示正面(此时回到最初的位置状态),一直循环。简单来说:正面div旋转,替换,转回,背面div替换成下张图片,以此一直循环下去。(单纯看文字有点难以理解,我们直接来看看“庐山真面目”)2.HTML:先看html,有个整体了解。
<body> <div class="outer_box"> <div id="d0" class="inner_div"> <div class="bcwd"></div><!--背面的div--> <div class="fwd"></div><!--正面的div--> </div> <div id="d1" class="inner_div"> <div class="bcwd"></div> <div class="fwd"></div> </div> <!--此处省略div id="d2"-"d5"的书写(代码相同)--> <div id="d6" class="inner_div"> <div class="bcwd"></div> <div class="fwd"></div> </div> </div> </body>
3.CSS:切分图片:将一张完整的图切成7块,各自进行不同的动画。在没有切图的情况下,如何让图片分成7块:设置7个div,每个div宽度为图片的1/7,position:absolute,且每个div的backgroundPosition的x值由0逐渐递减1/7.
64LL4E2OL83]ED{]CQ6I0`J.png
(图中蓝色部分为背景图,每个div都插入同一背景图,通过设置backgroundPosition,让图片沿X轴左移,即可7个div都显示对应的部分,构成完整的一张图)
关键样式:
.bcwd{ background: url(../img/sample2.jpg) no-repeat; background-size: 600px 400px; /*背面的图片默认状态,背对正面,且上下颠倒(使选装之后上下不颠倒)*/ -webkit-transform: rotateY(180deg) rotateZ(180deg); }.fwd{ position: absolute; top: 0; background: url(../img/sample1.jpg) no-repeat; background-size: 600px 400px;}
解释一下第五行中的这行代码:-webkit-transform: rotateY(180deg) rotateZ(180deg);如果只是在fwd和bcwd div绘制图片,会在图中的面1、3分别显示图片1、2,但正确的应是面1为正面(图1),面2、3紧贴一起,面4为背面(图2),这样才可以做到正面div向前旋转180度后背面成为正面,且背面图片是正的。为实现上述效果,bcwd div就要沿Y轴旋转180度,变成面4显示图片,再沿Z轴旋转180度,是背面图片旋转为正面时,图片不是倒过来的。
2.png
js设置样式:
var bcwd = document.getElementsByClassName('bcwd'); var fwd = document.getElementsByClassName('fwd'); for (var i = 0; i < inner.length; i++) { //设置bcwd和back的位置(left background-position) bcwd[i].style.left = 85.72 * i + 'px'; bcwd[i].style.backgroundPosition = '-' + 85.72 * i + 'px 0'; fwd[i].style.left = 85.72 * i + 'px'; fwd[i].style.backgroundPosition = '-' + 85.72 * i + 'px 0'; }
4.JS:实现动画间隔一段时间后正面转向背面,再间隔一段时间后,背面转为正面。
.inner_div.r{ -webkit-transform: rotateX(180deg); /*正面转为背面,背面转为正面*/ -webkit-transition: -webkit-transform 1s ease-in-out;}
setInterval(function(){ go(); },2000); var index = 2,nextIndex = 3; //用于计算第几张图片 inner[0].addEventListener("webkitTransitionEnd", function () { //换背景图(此时,fwd在背面。bcwd在正面) if(index == 4) nextIndex = 1; //最后一张图片与第一张图片的切换 else nextIndex = index + 1; for (var j = 0; j < fwd.length; j++) { //fwd的图片替换成当前图片 fwd[j].style.backgroundImage = 'url(img/sample' + index + '.jpg)'; } reset(); //fwd旋转为正面,bcwd旋转为背面 for (var j = 0; j < fwd.length; j++) { //bcwd的图片替换成下一张图片 bcwd[j].style.backgroundImage = 'url(img/sample' + nextIndex + '.jpg)'; } index = nextIndex; });
//实现背面转向正面 function go () { setTimeout(function() { inner[3].classList.add("r"); setTimeout(function() { inner[2].classList.add("r"); inner[4].classList.add("r"); setTimeout(function() { inner[1].classList.add("r"); inner[5].classList.add("r"); setTimeout(function() { inner[0].classList.add("r"); inner[6].classList.add("r"); }, 200); //时间可以改变,但是4个时间相加总和+动画旋转时间(css 1s)要小于上边setTimout的总时间 }, 200); }, 200); }, 200) }//实现正面转为背面 function reset () { for (var m = 0;m<inner.length;m++) { inner[m].classList.remove("r"); } }
目前效果:
multi-flip.gif
(是否感觉有一片一片的生硬感呢?)5.升华:添加以下2句CSS即可解决:
.outer_box{ -webkit-transform-style: preserve-3d; -webkit-perspective: 2000; /*增强3D效果*/}
6.补充:1.backface-visibility: visible|hidden;定义当元素不面向屏幕时是否可见。visible:背面是可见的。hidden:背面不可见的。2.以下两句很有用处,增强3D效果,大大提升动画效果啊!-webkit-transform-style: preserve-3d; -webkit-perspective: 2000;3.完整代码晚点揭晓。

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
