猿问

轮播图的制作原理?

如上图设计原型,网上找不到类似的轮播插件。
尝试自己制作,没有较好的js思路。
毕竟它跟大多数的轮播图有点差距:
1、首屏显示了三张图片。
2、左右图片仅显示一部份,如左图隐藏了左边部分,右图隐藏了右边部份。
3、每张图片之间均有一定的间距。
4、中间的大图比两边的图片显示尺寸变大。(所有图片都是16:9)

人到中年有点甜
浏览 717回答 1
1回答

万千封印

唉~ 写了半天~ 累啊<!DOCTYPE html><html><head>&nbsp; <meta http-equiv="Cache-Control" content="no-transform " />&nbsp;&nbsp; <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">&nbsp; <meta http-equiv="X-UA-Compatible" content="chrome=1" />&nbsp; <meta name="renderer" content="webkit" />&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0" />&nbsp; <title>轮播图 | 杰凯寇赞</title>&nbsp; <style>&nbsp; html,body {height: 100%}&nbsp; html {background: #222;color:#fff;} body {margin: 0;font-family: Roboto,Helvetica,Arial,Microsoft JhengHei,sans-serif}&nbsp; .viewport {width: 944px;height:283px;position: relative;overflow: hidden;}&nbsp; .father {width:3000%;height:100%;padding: 20px 0;box-sizing: border-box;transition: transform 0.6s ease-in-out;transform: translate3d(0,0,0);background: #ff5252;}&nbsp; .father.moving {transition-duration: 0s;}&nbsp; .father > div {width: 432px;height: 243px;margin: 0 20px;opacity:0.6;transition: all 0.6s ease-in-out;background: #2dbe60;float: left;}&nbsp; .father.moving > div {transition-duration: 0s;}&nbsp; .father > div.showing {opacity: 1;transform: scale3d(1.1, 1.1, 1);}&nbsp; .left, .right {width: 30px;height: 100%;line-height: 283px;user-select:none;text-align: center;cursor: pointer;background: rgba(0,0,0,0.15);position: absolute;top: 0;}&nbsp; .left {left: 0;} .right {right: 0;}&nbsp; </style></head><body>&nbsp; <div class="viewport">&nbsp; &nbsp; <div class="father" id="father">&nbsp; &nbsp; &nbsp; <div>A</div><!-- 1 -->&nbsp; &nbsp; &nbsp; <div>B</div>&nbsp; &nbsp; &nbsp; <div>C</div><!-- 3 -->&nbsp; &nbsp; &nbsp; <div>D</div>&nbsp; &nbsp; &nbsp; <div>E</div><!-- 5 -->&nbsp; &nbsp; &nbsp; <div>F</div>&nbsp; &nbsp; &nbsp; <div>G</div><!-- 7 --><!-- ^…^ -->&nbsp; &nbsp; &nbsp; <div>A</div><!-- 1 -->&nbsp; &nbsp; &nbsp; <div>B</div>&nbsp; &nbsp; &nbsp; <div>C</div><!-- 3 -->&nbsp; &nbsp; &nbsp; <div>D</div>&nbsp; &nbsp; &nbsp; <div>E</div><!-- 5 -->&nbsp; &nbsp; &nbsp; <div>F</div>&nbsp; &nbsp; &nbsp; <div>G</div><!-- 7 --><!-- Counting is too hard. -->&nbsp; &nbsp; &nbsp; <div>A</div><!-- 1 -->&nbsp; &nbsp; &nbsp; <div>B</div>&nbsp; &nbsp; &nbsp; <div>C</div><!-- 3 -->&nbsp; &nbsp; &nbsp; <div>D</div>&nbsp; &nbsp; &nbsp; <div>E</div><!-- 5 -->&nbsp; &nbsp; &nbsp; <div>F</div>&nbsp; &nbsp; &nbsp; <div>G</div><!-- 7 -->&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="left" id="left">:-o</div>&nbsp; &nbsp; <div class="right" id="right">:-)</div>&nbsp; </div><script type="text/javascript">var father=document.getElementById("father"),&nbsp; sons=father.children,&nbsp; sonsLength=sons.length/3,&nbsp; showWidth=432+20*2, //432: width; 20*2: margin*2&nbsp; showingId=parseInt(sonsLength/2)+sonsLength-1,&nbsp; transform=-showingId*showWidth+showWidth/2,&nbsp; checkTime=new Date()father.style.transform=`translate3d(${transform}px, 0, 0)`sons[showingId].className="showing"function go(nowShowingId, direction) {&nbsp; // Direction: "-1" stands for left, "1" stands for right.&nbsp; //+ Avoid continuous sliding&nbsp; if(new Date()-checkTime<700)return&nbsp; checkTime=new Date()&nbsp; //+ Standard show change&nbsp; sons[nowShowingId].className=""&nbsp; //- Change here&nbsp; nowShowingId=nowShowingId+direction&nbsp; showingId=nowShowingId;&nbsp; transform=nowShowingId*showWidth-showWidth/2&nbsp; father.style.transform=`translate3d(-${transform}px, 0, 0)`&nbsp; sons[nowShowingId].className="showing"&nbsp; //+ Special show change&nbsp; if(nowShowingId==1){&nbsp; &nbsp; showingId=sonsLength*2+1 // How does it works?&nbsp; }&nbsp; else if(nowShowingId==sonsLength*2+2){&nbsp; &nbsp; showingId=1+1 // Imagine the answer. (Use DevTools!&nbsp; }&nbsp; else {return}&nbsp; //- change here&nbsp; setTimeout(function(){&nbsp; &nbsp; father.classList.add("moving")&nbsp; &nbsp; sons[showingId].className="showing"&nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; father.style.transform=`translate3d(-${showingId*showWidth-showWidth/2}px, 0, 0)`&nbsp; &nbsp; &nbsp; sons[nowShowingId].className=""&nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; father.classList.remove("moving")&nbsp; &nbsp; &nbsp; },50) // =1.&nbsp; &nbsp; },530) // =2.&nbsp; },100) // =3. /= 1,2,3: Specified like that because of setTimeout's time error}document.getElementById("left").onclick=function (){go(showingId, -1)}document.getElementById("right").onclick=function (){go(showingId, 1)}</script></body></html>原理可以参考随后这篇文章新货!如何制作一个高效轮播图?
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答