首页 > 代码库 > 学习笔记5

学习笔记5

一、例子:轮播

html:

    `<!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="http://www.mamicode.com/images/imgA_2.jpg">
      <img src="http://www.mamicode.com/images/imgB_2.jpg">
      <img src="http://www.mamicode.com/images/imgC_2.jpg">
      <img src="http://www.mamicode.com/images/imgD_2.jpg">
    </div>
    <ul id="nav">
      <li class="selected"></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
  <script src="http://www.mamicode.com/js/jquery-1.12.4.js"></script>
  <script src="http://www.mamicode.com/js/轮播.js"></script>
</body>
</html>
`

js:

    `$(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‘);
    });
  }
});
`

二、Handlebars

    `<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="http://www.mamicode.com/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>`

学习笔记5