你們好,這裏是「 從零開始學 Web 系列教程 」,並在下列地址同步更新......javascript
- github:https://github.com/Daotin/Web
- 微信公衆號:Web前端之巔
- 博客園:http://www.cnblogs.com/lvonve/
- CSDN:https://blog.csdn.net/lvonve/
在這裏我會從 Web 前端零基礎開始,一步步學習 Web 相關的知識點,期間也會分享一些好玩的項目。如今就讓咱們一塊兒進入 Web 前端學習的探索之旅吧!css
需求:頁面無橫向滾動條,頁面隨着寬度的改變自動伸縮。html
代碼:前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> * { margin: 0; padding: 0; } header { width: 100%; display: flex; } header > a { flex: 1; } header > a > img { width: 100%; } main { width: 100%; padding: 0 10px; box-sizing: border-box; } main > .item { width: 100%; height: 100px; background-color: pink; border-radius: 10px; margin-top: 10px; display: flex; } main > .item:nth-of-type(1) { background-color: rgb(78, 50, 182); } main > .item:nth-of-type(2) { background-color: rgb(31, 153, 209); } main > .item:nth-of-type(3) { background-color: rgb(240, 147, 7); } main > .item:nth-of-type(4) { background-color: rgb(187, 19, 131); } .item > .left { flex: 1; } .item > .right { flex: 2; flex-wrap: wrap; display: flex; } .item > .right > a { display: block; width: 50%; color: #fff; text-decoration: none; line-height: 50px; text-align: center; border-left: 1px solid #ccc; border-bottom: 1px solid #ccc; box-sizing: border-box; } .item > .right > a:nth-last-of-type(-n+2) { border-bottom: none; } .extra { width: 100%; display: flex; } .extra > a { flex: 1; } .extra > a > img { width: 100%; } footer { width: 100%; } footer > .nav { width: 100%; height: 30px; border-top: 2px solid #ccc; border-bottom: 2px solid #ccc; display: flex; } footer > .nav > a { flex: 1; text-align: center; line-height: 30px; text-decoration: none; color: #666; } footer > .link { text-align: center; margin-top: 5px; } footer > .copyright { text-align: center; margin-top: 5px; } </style> </head> <body> <div class="container"> <header> <a href="javascript:;"><img src="./images/banner.jpg"></a> </header> <main> <section class="item"> <div class="left"></div> <div class="right"> <a href="javascript:;">海外酒店</a> <a href="javascript:;">團購</a> <a href="javascript:;">特價機票</a> <a href="javascript:;">民宿客棧</a> </div> </section> <section class="item"> <div class="left"></div> <div class="right"> <a href="javascript:;">海外酒店</a> <a href="javascript:;">團購</a> <a href="javascript:;">特價機票</a> <a href="javascript:;">民宿客棧</a> </div> </section> <section class="item"> <div class="left"></div> <div class="right"> <a href="javascript:;">海外酒店</a> <a href="javascript:;">團購</a> <a href="javascript:;">特價機票</a> <a href="javascript:;">民宿客棧</a> </div> </section> <section class="item"> <div class="left"></div> <div class="right"> <a href="javascript:;">海外酒店</a> <a href="javascript:;">團購</a> <a href="javascript:;">特價機票</a> <a href="javascript:;">民宿客棧</a> </div> </section> <section class="extra"> <a href="javascript:;"><img src="./images/extra_1.png"></a> <a href="javascript:;"><img src="./images/extra_2.png"></a> </section> </main> <footer> <div class="nav"> <a href="javascript:;">電話預約</a> <a href="javascript:;">下載客戶端</a> <a href="javascript:;">個人</a> </div> <p class="link"> <a href="javascript:;">網站地圖</a> <a href="javascript:;">English</a> <a href="javascript:;">電腦版</a> </p> <p class="copyright">© 攜程旅行</p> </footer> </div> </body> </html>
案例截圖:java
示例代碼:jquery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> * { margin: 0; padding: 0; } .container { width: 560px; height: 300px; margin: 100px auto; position: relative; } .container > ul { width: 100%; height: 100%; list-style: none; /* transform: rotate3d(1,1,0,-45deg); */ transform-style: preserve-3d; } ul > li { width: 20%; height: 100%; float: left; position: relative; transform-style: preserve-3d; transition: transform 0.5s; } ul > li > span { position: absolute; width: 100%; height: 100%; } ul > li:nth-of-type(2) > span { background-position: -100%; } ul > li:nth-of-type(3) > span { background-position: -200%; } ul > li:nth-of-type(4) > span { background-position: -300%; } ul > li:nth-of-type(5) > span { background-position: -400%; } ul > li > span:nth-of-type(1) { background-image: url("./images/q1.jpg"); transform: translateZ(150px); } ul > li > span:nth-of-type(2) { background-image: url("./images/q2.jpg"); transform: translateY(-150px) rotateX(90deg); } ul > li > span:nth-of-type(3) { background-image: url("./images/q3.jpg"); transform: translateZ(-150px) rotateX(180deg); } ul > li > span:nth-of-type(4) { background-image: url("./images/q4.jpg"); transform: translateY(150px) rotateX(-90deg); } .left { width: 60px; height: 60px; background-color: rgba(0,0,0,0.5); line-height: 60px; color: #fff; text-align: center; font-family: "Consolas"; font-size: 40px; position: absolute; left: 0; top: 50%; transform: translateY(-50%); cursor: pointer; z-index: 10; } .right { width: 60px; height: 60px; background-color: rgba(0,0,0,0.5); line-height: 60px; color: #fff; text-align: center; font-family: "Consolas"; font-size: 40px; position: absolute; right: 0; top: 50%; transform: translateY(-50%); cursor: pointer; z-index: 10; } </style> </head> <body> <div class="container"> <ul> <li> <span></span> <span></span> <span></span> <span></span> </li> <li> <span></span> <span></span> <span></span> <span></span> </li> <li> <span></span> <span></span> <span></span> <span></span> </li> <li> <span></span> <span></span> <span></span> <span></span> </li> <li> <span></span> <span></span> <span></span> <span></span> </li> </ul> <span class="left"><</span> <span class="right">></span> </div> </body> <script> var index = 0; // 添加節流閥 var flag = true; document.querySelector(".left").onclick = function () { if(flag) { flag = false; index++; var liObjs = document.querySelectorAll("li"); for(var i=0; i<liObjs.length; i++) { liObjs[i].style.transform = "rotateX("+90*index+"deg)"; liObjs[i].style.transitionDelay = i*0.1 + "s"; } setTimeout(function () { flag = true; }, 1000); } } document.querySelector(".right").onclick = function () { if(flag) { flag = false; index--; var liObjs = document.querySelectorAll("li"); for(var i=0; i<liObjs.length; i++) { liObjs[i].style.transform = "rotateX("+90*index+"deg)"; liObjs[i].style.transitionDelay = i*0.1 + "s"; } setTimeout(function () { flag = true; }, 1000); } } </script> </html>
示例效果:css3
案例代碼:git
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="./css/360Page.css"> <script src="./js/jquery.min.js"></script> <script src="./js/jquery.fullPage.min.js"></script> </head> <body> <div id="360Page"> <div class="section first"> <div class="logo"></div> <div class="text"> <img src="./images/text_1.png" alt=""> <img src="./images/text_2.png" alt=""> <img src="./images/text_3.png" alt=""> <img src="./images/text_4.png" alt=""> <img src="./images/text_5.png" alt=""> <img src="./images/text_6.png" alt=""> <img src="./images/text_7.png" alt=""> <img src="./images/text_8.png" alt=""> </div> <div class="info1"></div> </div> <div class="section second"> <div class="shield"> <img src="./images/shield_1.png" alt=""> <img src="./images/shield_2.png" alt=""> <img src="./images/shield_3.png" alt=""> <img src="./images/shield_4.png" alt=""> <img src="./images/shield_5.png" alt=""> <img src="./images/shield_6.png" alt=""> <img src="./images/shield_7.png" alt=""> <img src="./images/shield_8.png" alt=""> <img src="./images/shield_9.png" alt=""> </div> <div class="info2"></div> </div> <div class="section third"> <div class="info3"></div> <div class="circle"> <div class="rocket"></div> </div> </div> <div class="section fourth"> <div class="search"> <div class="search-bar"></div> <div class="search-text"></div> <div class="search-content"></div> </div> <div class="info4"></div> </div> <div class="section fifth"> <h3>第五屏</h3> </div> </div> </body> <script> $(function(){ $('#360Page').fullpage({ sectionsColor: ['#0da5d6', '#2AB561', '#DE8910', '#16BA9D', '#0DA5D6'], afterLoad: function (anchorLink, index) { $(".section").removeClass("current"); $(".section:eq("+(index-1)+")").addClass("current"); } }); }); </script> </html>
全屏插件的使用:github
sectionsColor:設置每一屏的顏色;瀏覽器
afterLoad:回調函數,當每一屏加載完成後須要執行的動做。
CSS 代碼:
* { margin: 0; padding: 0; } .section { overflow: hidden; } /* 第一屏 */ .first { padding-top: 100px; } .first .logo { width: 251px; height: 186px; background-image: url("../images/logo.png"); margin: 0 auto; } .first .text { text-align: center; margin: 100px 0 20px 0; } .first .text > img { margin: 0 20px; opacity: 0.1; transition: margin 0.8s,opacity 0.8s; } .first .info1 { width: 772px; height: 49px; background-image: url("../images/info_1.png"); margin: 0 auto; } /* 第一屏動畫 */ .first.current .text > img { margin: 0 5px; opacity: 1; } /* 第二屏 */ .second > div{ display: flex; justify-content: space-around; align-items: center; } .second .shield { width: 440px; font-size: 0; /* 消除盾牌之間的間隙*/ } .second .shield > img { opacity: 0.1; transition: transform 0.8s, opacity 0.8s; } .second .shield > img:nth-of-type(1){ transform: translate(-100px, 200px) rotate(30deg); } .second .shield > img:nth-of-type(2){ transform: translate(300px, 20px) rotate(60deg); } .second .shield > img:nth-of-type(3){ transform: translate(300px, 10px) rotate(-50deg); } .second .shield > img:nth-of-type(4){ transform: translate(600px, 20px) rotate(-90deg); } .second .shield > img:nth-of-type(5){ transform: translate(30px, 200px) rotate(90deg); } .second .shield > img:nth-of-type(6){ transform: translate(-30px, -30px) rotate(-30deg); } .second .shield > img:nth-of-type(7){ transform: translate(0px, 100px) rotate(-30deg); } .second .shield > img:nth-of-type(8){ transform: translate(320px, 150px) rotate(30deg); } .second .shield > img:nth-of-type(9){ transform: translate(-190px, -280px) rotate(-30deg); } .second .info2 { width: 635px; height: 309px; background-image: url("../images/info_2.png"); } /* 第二屏動畫 */ .second.current .shield > img { transform: none; opacity: 1; } /* 第三屏 */ .third { position: relative; } .third .info3 { width: 631px; height: 278px; background-image: url("../images/info_3.png"); position: absolute; left:50%; top: 50%; transform: translate(-100%,-50%); } .third .circle { width: 453px; height: 449px; background-image: url("../images/circle.png"); position: absolute; left: 57%; top: 50%; transform: translateY(-50%); } .third .circle .rocket { width: 203px; height: 204px; background-image: url("../images/rocket.png"); position: absolute; left: -50%; top: 150%; transition: left 0.8s, top 0.8s; } /* 第三屏動畫 */ .third.current .circle .rocket { left: 115px; top: 115px; } /* 第四屏 */ .fourth { position: relative; } .fourth .search { width: 529px; height: 438px; position: absolute; left: 50%; top: 50%; transform: translate(-115%, -50%); overflow: hidden; } .fourth .search .search-bar{ width: 529px; height: 66px; background-image: url("../images/search.png"); transform: translateX(-100%); } .fourth .search .search-text{ width: 0; height: 22px; background-image: url("../images/key.png"); position: absolute; left: 18px; top: 23px; } .fourth .search .search-content{ width: 529px; height: 0; background-image: url("../images/result.png"); margin-top: -12px; } .fourth .info4 { width: 612px; height: 299px; background-image: url("../images/info_4.png"); position: absolute; left: 50%; top: 50%; transform: translateY(-50%); } /* 第四屏動畫 */ .fourth.current .search .search-bar{ transform: none; transition: transform 0.8s; } .fourth.current .search .search-text{ width: 99px; transition: width 0.8s 0.8s steps(5); } .fourth.current .search .search-content{ height: 372px; transition: height 0.8s 1.6s; }
使用 current 交集選擇器實現,當加載完某一全屏後須要執行的動畫。