Today to share the mobile-native JS Seamless carousel map;
The mobile side minimizes the use of DOM operations to frequently waste the performance of mobile devices, so this seamless carousel diagram uses more transition and transform, the idea that seamless thinking is the same as that of the PC-side seamless carousel that was shared yesterday, respectively inserted the last picture in front of the normal picture and inserted the first picture at the end of the normal picture, in the movement to the two pictures when the instant clear transition and pull the entire picture chain back to the normal picture stream position, resulting in a seamless
This mobile end Carousel also illustrates a way of thinking: in order to better consider the user experience, when the user sliding screen distance is less than a certain time, our picture will not be replaced with the next one, this effect is generated thanks to process control if the user presses the finger and lift the coordinates of the finger of the value of the difference
The same today's example also uses the concept of a switch, specific ideas that have been elaborated in yesterday's article
Today's example is based on the resolution of 640, and in the JS dynamic change onresize time value, so that our code to meet the size of different mobile phones
The best preparation for tomorrow are doing your best today.
Well, today is the best preparation for tomorrow.
HTML code:
<div id= "tabs" > <ul> <li style= "Background:pink;" >3</li> <li style= "background:red;" >0</li> <li style= "Background:green;" >1</li> <li style= "Background:yellow;" >2</li> <li style= "Background:pink;" >3</li> <li style= "background:red;" >0</li> </ul> </div>
CSS code:
<meta name= "viewport" content= "Width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" > <meta name= "apple-mobile-web-app-capable" content= "yes" > <meta name= " Apple-mobile-web-app-status-bar-style "content=" Black "> <meta name=" format-detection "content=" Telephone=no,email=no "> <style> *{ padding:0; margin:0; List-style:none; } html{ font-size:20px; } #tabs ul{ Width:96rem; height:100%; Overflow:hidden; Transform:translatex ( -16rem); } #tabs ul li{ Width:16rem; Height:7.5rem; Float:left; Font-size:3rem; Color: #fff; Text-align:center; Line-height:7.5rem; }
JS Code:
The size of the CSS model set at the resolution of 640 for the base resolution, the code in the closure changes the unit size (function (window,document) {function change () { var ohtml=document.getelementsbytagname (' html ') [0]; ohtml.style.fontsize=20*document.documentelement.clientwidth/320+ ' px '; } change (); Window.addeventlistener (' resize ', change,false); }) (window,document); Document.addeventlistener (' domcontentloaded ', function () {var otab=document.getelementbyid (' tabs '); var oul=otab.getelementsbytagname (' ul ') [0]; var Ali=oul.children; var inow=1; var X=-ali[0].offsetwidth*inow; Set the switch var bready=false; Oul.addeventlistener (' Touchstart ', function (EV) {if (bready) {return;} Bready=true; Oul.style.transition= ' None '; Record the pressed coordinates when you start to touch var Downx=ev.targettoUches[0].pagex; var disx=downx-x; function Fnmove (EV) {//re-calculates the displacement at move and assigns a value to UL X=ev.targettouches[0].pagex-dis X Oul.style.transform= ' TranslateX (' +x+ ' px) '; } function Fnend (EV) {document.removeeventlistener (' touchmove ', fnmove,false); Document.removeeventlistener (' Touchend ', fnend,false); At the time of lifting, record the position of lifting, notice the position raised is changedtouches[0] var Upx=ev.changedtouches[0].pagex; Open transition oul.style.transition= ' 0.4s all ease '; Here, for the user experience, a minimum moving distance is judged, when the moving distance is less than this is worth the time without movement if ((Math.Abs (UPX-DOWNX)) >50) { Determine the direction of movement, right or left if (downx>upx) {inow++; Set Move Range if (inow==ali.length) inow=ali.length-1; }else{//And the right side are opposite inow--; if (inow==-1) inow=0; }}//To assign a value x=-ali[0].offsetwidth*inow; Oul.style.transform= ' TranslateX (' +x+ ' px) '; Set UL change function TEnd () {//In order to not let the moment pull back to be found, first to clear transition Oul.style.transition= ' None '; Make a seamless judgment, and pull back to the second if (inow==ali.length-1) {inow=1) when the picture is to the second to the right. }else if (inow==0) {//When the picture moves to the first one, it pulls back to the bottom of the third one. Inow=ali.length-2; } X=-ali[0].offsetwidth*inow; Oul.style.transform= ' TranslateX (' +x+ ' px) '; Bready=false; } oul.addeventlistener (' Transitionend ', tend,false); } document.addeventlistener (' Touchmove ', fnmove,false); Document.addeventlistener (' Touchend ', fnend,false); Ev.preventdefault (); },false); },false); </script>
JS Native tab (includes mobile seamless tab) three