seajs

http://cyj.me/why-seajs/zh/
http://seajs.org/docs/#quick-start

1.一個(gè)demo

Paste_Image.png

html文件中


<script src="../sea-modules/seajs/seajs/2.2.0/sea.js"></script>
<script>

  // Set configuration
  seajs.config({
    base: "../sea-modules/",
    alias: {
      "jquery": "jquery/jquery/1.10.1/jquery.js"
    }
  });

  // For development
  if (location.href.indexOf("?dev") > 0) {
    seajs.use("../static/hello/src/main");
  }
  // For production
  else {
    seajs.use("examples/hello/1.0.0/main");
  }
</script>

入口文件main

define(function(require) {

  var Spinning = require('./spinning');

  var s = new Spinning('#container');
  s.render();

});

模塊代碼

define(function(require, exports, module) {

  var $ = require('jquery');

  function Spinning(container) {
    this.container = $(container);
    this.icons = this.container.children();
    this.spinnings = [];
  }

  module.exports = Spinning;

  Spinning.prototype.render = function() {
    this._init();
    this.container.css('background', 'none');
    this.icons.show();
    this._spin();
  }

  Spinning.prototype._init = function() {
    var spinnings = this.spinnings;

    $(this.icons).each(function(n) {
      var startDeg = random(360);
      var node = $(this);
      var timer;

      node.css({
        top: random(40),
        left: n * 50 + random(10),
        zIndex: 1000
      }).hover(
          function() {
            node.fadeTo(250, 1)
                .css('zIndex', 1001)
                .css('transform', 'rotate(0deg)');

          },
          function() {
            node.fadeTo(250, .6).css('zIndex', 1000);
            timer && clearTimeout(timer);
            timer = setTimeout(spin, Math.ceil(random(10000)));
          }
      );

      function spin() {
        node.css('transform', 'rotate(' + startDeg + 'deg)');
      }

      spinnings[n] = spin;
    })

    return this;
  }

  Spinning.prototype._spin = function() {

    $(this.spinnings).each(function(i, fn) {
      setTimeout(fn, Math.ceil(random(3000)));
    });

    return this;
  }

  function random(x) { return Math.random() * x };

});

2.知識(shí)點(diǎn)

define define(id?, deps?, factory)

define
 也可以接受兩個(gè)以上參數(shù)。字符串 id
 表示模塊標(biāo)識(shí),數(shù)組 deps
 是模塊依賴。比如:
define('hello', ['jquery'], function(require, exports, module) { // 模塊代碼});
require.async require.async(id, callback?)

require.async方法用來在模塊內(nèi)部異步加載模塊,并在加載完成后執(zhí)行指定回調(diào)。callback
 參數(shù)可選。
    define(function(require, exports, module) { // 異步加載一個(gè)模塊,在加載完成時(shí),執(zhí)行回調(diào) 
    require.async('./b', function(b) { b.doSomething(); }); // 異步加載多個(gè)模塊,在加載完成時(shí),執(zhí)行回調(diào) 
    require.async(['./c', './d'], function(c, d) { 
          c.doSomething(); 
          d.doSomething();
    });
});
// // 并發(fā)加載模塊 a 和模塊 b,并在都加載完成時(shí),執(zhí)行指定回調(diào)
seajs.use(['./a', './b'], function(a, b) { a.init(); b.init();});
callback參數(shù)可選,省略時(shí),表示無需回調(diào)。

seajs.config({

  // 別名配置
  alias: {
    'es5-safe': 'gallery/es5-safe/0.9.3/es5-safe',
    'json': 'gallery/json/1.0.2/json',
    'jquery': 'jquery/jquery/1.10.1/jquery'
  },

  // 路徑配置
  paths: {
    'gallery': 'https://a.alipayobjects.com/gallery'
  },

  // 變量配置
  vars: {
    'locale': 'zh-cn'
  },

  // 映射配置
  map: [
    ['http://example.com/js/app/', 'http://localhost/js/app/']
  ],

  // 預(yù)加載項(xiàng)
  preload: [
    Function.prototype.bind ? '' : 'es5-safe',
    this.JSON ? '' : 'json'
  ],

  // 調(diào)試模式
  debug: true,

  // Sea.js 的基礎(chǔ)路徑
  base: 'http://example.com/path/to/base/',

  // 文件編碼
  charset: 'utf-8'
});
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前些日子從@張?chǎng)涡裎⒉┨幍靡环萃扑](Front-end-tutorial),號(hào)稱最全的資源教程-前端涉及的所有知識(shí)...
    谷子多閱讀 4,497評(píng)論 0 44
  • 我喊她“曾哥”。 曾哥不是會(huì)唱綿羊音的那個(gè)“曾哥”,曾哥是我們五人小分隊(duì)里的曾哥。其實(shí)參照她清純秀麗的面龐,更應(yīng)該...
    我是辣辣閱讀 336評(píng)論 0 0
  • 我篤定這兩三張稿件,我是晝思夜想也始終拼湊不出一個(gè)像樣的單字。 欣來我似牛虻般喜愛寄人籬下,汲取別人的血液,生啖與...
    業(yè)藏閱讀 316評(píng)論 0 2

友情鏈接更多精彩內(nèi)容