html:css
`<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>輪播</title> <style type="text/css"> * { margin: 0; padding: 0; } #container { position: relative; left: 0; top: 0; width: 430px; height: 430px; overflow: hidden; } #wrap { position: absolute; left: 0; top: 0; width: 1720px; height: 430px; overflow: hidden; } #wrap img { float: left; } #nav { width: 150px; height: 30px; list-style: none; position: absolute; bottom: 40px; left: 0; right: 0; margin: auto; } #nav>li { width: 30px; height: 30px; border-radius: 30px; background-color: rgba(255, 255, 255, 0.6); float: left; margin-left: 10px; cursor: pointer; } #nav>li:first-child { margin-left: 0; } #nav>li.selected { background-color: green; } </style> </head> <body> <div id="container"> <div id="wrap"> <img src="./images/imgA_2.jpg"> <img src="./images/imgB_2.jpg"> <img src="./images/imgC_2.jpg"> <img src="./images/imgD_2.jpg"> </div> <ul id="nav"> <li class="selected"></li> <li></li> <li></li> <li></li> </ul> </div> <script src="./js/jquery-1.12.4.js"></script> <script src="./js/輪播.js"></script> </body> </html> `
js:html
`$(function() { $('#wrap').data('index', 0); $('#nav').on('click', 'li', function() { // 清除自動播放的定時器 clearInterval(timer); var that = this; var index = $(this).index(); // 把當前要顯示的圖片的索引號,保存到附加數據中 $('#wrap').data('index', index).animate({ left: index * (-430) }, 600, function() { $('#nav li').removeClass('selected'); $(that).addClass('selected'); // 重啓自動播放的定時器 timer = setInterval(autoPlay, 2000); }); }); var timer = setInterval(autoPlay, 2000); function autoPlay() { // console.log(123); // 獲取當前的index var currentIndex = $('#wrap').data('index'); // 下一張的index var nextIndex; if(currentIndex === $('#wrap img').length - 1) { nextIndex = 0; } else { nextIndex = currentIndex + 1; } // console.log(nextIndex); $('#wrap').data('index', nextIndex).animate({ left: nextIndex * (-430) }, 600, function() { $('#nav li').removeClass('selected').eq(nextIndex).addClass('selected'); }); } }); `
`<script id="template" type="text/x-handlebars-template"> <h1>Hello</h1> <h2>Handlebars</h2> <table border="1"> <thead> <tr> <th>姓名</th> <th>年齡</th> <th>性別</th> </tr> </thead> <tbody> <tr> <td>{{name}}</td> <td>{{age}}</td> <td>{{gender}}</td> </tr> </tbody> </table> <p>hello, {{name}}</p> <p>{{hello}}</p> <table border="1"> <thead> <tr> <th>姓名</th> <th>年齡</th> <th>性別</th> </tr> </thead> <tbody> {{#each listOfStudents}} <tr> <td>{{name}}</td> <td>{{age}}</td> <td>{{gender}}</td> </tr> {{/each}} </tbody> </table> <p>hello, {{{person.a.name}}}</p> <p>hello, {{person/a/name}}</p> <div class="big"> <div class="small"> 2 </div> </div> <div class="big"> <div class="small"> 3 </div> </div> <div class="big"> <div class="small"> 4 </div> </div> {{circle name}} {{circle name}} {{circle name}} </script> <script src="./js/handlebars-v4.0.5.js"></script> <script> // 獲取模板的源代碼 var source = document.getElementById('template').innerHTML; // 把模板的源代碼,編譯成模板對象 var template = Handlebars.compile(source); // 建立helper Handlebars.registerHelper('circle', function(data) { // return '<div class="big"><div class="small">' + data + '</div></div>'; // 告訴系統,這個返回值要解析成真正的標籤 var obj = new Handlebars.SafeString('<div class="big"><div class="small">' + data + '</div></div>'); return obj; }); // 經過模板對象,獲取最終html代碼 var html = template({ person: { a: { name: 'Tom<input type="text">' }, b: 'hello' }, name: 'Bob', age: 20, gender: 'male', test: 'hello', listOfStudents: [ { name: 'bob', age: 20, gender: 'male' }, { name: 'tom', age: 22, gender: 'male' }, { name: 'Hellen', age: 20, gender: 'female' }, ] }); // console.log(html); // 把html代碼插入到網頁中去 document.getElementById('container').innerHTML = html; // helper </script>`