2016-06-19 3 views
0

Aから始まり、目的の文字に達するまで、指定された単語の各文字をインクリメントするコードがあります。以下のコードスニペットでその例を見ることができます。このコードは、1つのdiv idをターゲットにしているときに機能しますが、「ブロック」クラスが割り当てられたテキストのすべてのブロックにこの増分テキストエフェクトを適用するようにしたいと思います。各クラスを使用してこのクラスにアクセス

$(document).ready(function() { 

    console.log("ready!"); 

    $('.block').each(function() { 

    function Letter(table, letter, duration) { 
     this.table = table; 
     this.letter = letter; 
     this.current = 0; 
     this.delay = duration/tbl.indexOf(letter); // ms 
     this.time = Date.now(); 
     this.done = false; 
    } 
    Letter.prototype.update = function() { 
     if (this.done) return; 
     var time = Date.now(); 
     if (time - this.time >= this.delay) { 
     this.time = time; 
     if (this.letter === this.table[this.current] || this.current === this.table.length) { 
      this.done = true; 
     } else { 
      this.current++; 
     } 
     } 
    }; 

    var word = $(this).html(); 
    console.log('Word: ' + word); 

    var tbl = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    var letters = []; 
    word.toUpperCase().split("").forEach(function(l) { 
     letters.push(new Letter(tbl, l, 2500)) 
     console.log(l); 
    }); 

    (function loop() { 
     var txt = "", 
     isDone = true; 
     letters.forEach(function(l) { 
     l.update(); 
     if (!l.done) isDone = false; 
     txt += l.table[l.current]; 
     }); 

     // output txt 
     //$("div#d").html(txt); 
     $(this).parent('.block').html(txt); 

     if (!isDone) requestAnimationFrame(loop); 
     else { /* done */ } 
    })(); 

    }); 

}); 

私はそれに割り当てられた「ブロック」クラスを使用して、各テキストビットに出力にインクリメント効果をしようとしている:

$(this).parent('.block').html(txt); 

私は上記で各「ブロック」クラスをターゲットにしようとしていますコード行は動作していません。これどうやってするの?インクリメントされている「言葉」は「ブロック」タグ内に収まるものは何でもあり、このラインと

お知らせ:あなたのコードで

var word = $(this).html(); 

$(document).ready(function() { 
 

 
    console.log("ready!"); 
 

 
    $('.block').each(function() { 
 

 
    function Letter(table, letter, duration) { 
 
     this.table = table; 
 
     this.letter = letter; 
 
     this.current = 0; 
 
     this.delay = duration/tbl.indexOf(letter); // ms 
 
     this.time = Date.now(); 
 
     this.done = false; 
 
    } 
 
    Letter.prototype.update = function() { 
 
     if (this.done) return; 
 
     var time = Date.now(); 
 
     if (time - this.time >= this.delay) { 
 
     this.time = time; 
 
     if (this.letter === this.table[this.current] || 
 
      this.current === this.table.length) { 
 
      this.done = true; 
 
     } else { 
 
      this.current++; 
 
     } 
 
     } 
 
    }; 
 

 
    var word = $(this).html(); 
 
    console.log('Word: ' + word); 
 

 
    var tbl = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
 
    var letters = []; 
 
    word.toUpperCase().split("").forEach(function(l) { 
 
     letters.push(new Letter(tbl, l, 2500)) 
 
     console.log(l); 
 
    }); 
 

 
    (function loop() { 
 
     var txt = "", 
 
     isDone = true; 
 
     letters.forEach(function(l) { 
 
     l.update(); 
 
     if (!l.done) isDone = false; 
 
     txt += l.table[l.current]; 
 
     }); 
 

 
     // output txt 
 
     //d.innerHTML = txt; 
 
     $("div#d").html(txt); 
 

 
     if (!isDone) requestAnimationFrame(loop); 
 
     else { /* done */ } 
 
    })(); 
 

 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id=d></div> 
 

 
<div id="other_spans"> 
 
    <span class="block">First</span> 
 
    <span class="block">Second</span> 
 
    <span class="block">Third</span>

+0

の内部での参照を使用できますか?同じ '#d'要素に? – guest271314

+2

'Let()'コンストラクタや 'update()'メソッドを '.each()'ループの前に宣言することはできませんでしたか?それらは実際にはループロジックの一部ではありません。また、あなたが気にしないことを願っていますが、各ブロックがどこで終了したのかがわかりにくかったため、コードのインデントを修正するだけで(実際のコード自体に変更はありません) – nnnnnn

+0

@ guest271314各結果は、対応する ".block"クラス要素に返されます。言い換えれば、上の例では、 "First" "Second"と "Third"は、それらの周りに "block"スパンを持つため、すべて増分エフェクトを表示します – cpcdev

答えて

1

作業フィドルへのリンクがあります。 $(this)現在の要素への参照を作成して、その結果必要があるが返されloop

$(document).ready(function() { 
 

 
    console.log("ready!"); 
 

 
    var tbl = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
 

 
    function Letter(table, letter, duration) { 
 
    this.table = table; 
 
    this.letter = letter; 
 
    this.current = 0; 
 
    this.delay = duration/tbl.indexOf(letter); // ms 
 
    this.time = Date.now(); 
 
    this.done = false; 
 
    } 
 
    
 
    Letter.prototype.update = function() { 
 
    if (this.done) return; 
 
    var time = Date.now(); 
 
    if (time - this.time >= this.delay) { 
 
     this.time = time; 
 
     if (this.letter === this.table[this.current] 
 
      || this.current === this.table.length) { 
 
     this.done = true; 
 
     } else { 
 
     this.current++; 
 
     } 
 
    } 
 
    }; 
 

 
    $(".block").each(function() { 
 
    // store reference to current `this` element 
 
    var elem = $(this); 
 
    var word = elem.html(); 
 
    console.log("Word: " + word); 
 
    var letters = []; 
 

 
    word.toUpperCase().split("") 
 
    .forEach(function(l) { 
 
     letters.push(new Letter(tbl, l, 2500)) 
 
     console.log(l); 
 
    }); 
 

 
    (function loop() { 
 
     var txt = "", 
 
     isDone = true; 
 
     letters.forEach(function(l) { 
 
     l.update(); 
 
     if (!l.done) isDone = false; 
 
     txt += l.table[l.current]; 
 
     }); 
 
     // `elem` : `this` element at `.each()` iteration 
 
     elem.html(txt); 
 

 
     if (!isDone) requestAnimationFrame(loop); 
 
     else { /* done */ } 
 
    })(); 
 

 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script> 
 
<div id="d"></div> 
 

 
<div id="other_spans"> 
 
    <span class="block">First</span> 
 
    <span class="block">Second</span> 
 
    <span class="block">Third</span>

1

この内部の生命維持は、ウィンドウですオブジェクト。ブロッククラスを持つ要素の参照を保持し、IIFE内部で使用します。以下のように -

$(document).ready(function() { 

    console.log("ready!"); 
    var tbl = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

    function Letter(table, letter, duration) { 
     this.table = table; 
     this.letter = letter; 
     this.current = 0; 
     this.delay = duration/tbl.indexOf(letter); // ms 
     this.time = Date.now(); 
     this.done = false; 
    } 
    Letter.prototype.update = function() { 
     if (this.done) return; 
     var time = Date.now(); 
     if (time - this.time >= this.delay) { 
      this.time = time; 
      if (this.letter === this.table[this.current] || this.current === this.table.length) { 
       this.done = true; 
      } else { 
       this.current++; 
      } 
     } 
    }; 


    $('.block').each(function() { 

     var $this = $(this); 

     var word = $(this).html(); 
     console.log('Word: ' + word); 


     var letters = []; 
     word.toUpperCase().split("").forEach(function(l) { 
      letters.push(new Letter(tbl, l, 2500)) 
      console.log(l); 
     }); 

     (function loop() { 
      var txt = "", 
       isDone = true; 
      letters.forEach(function(l) { 
       l.update(); 
       if (!l.done) isDone = false; 
       txt += l.table[l.current]; 
      }); 

      // output txt 
      //$("div#d").html(txt); 
      $this.html(txt); 

      if (!isDone) requestAnimationFrame(loop); 
      else { /* done */ } 
     })(); 

    }); 
}); 

ここでは、要素の各反復で関数、変数を再定義防ぐためにLetter機能と.each()tbl変数外に移動することができますhttps://jsfiddle.net/HectorBarbossa/tg9hpk4a/

+0

ありがとうございます、私はあなたの変更でコードを更新しましたが、それでも何も出力されません。 – cpcdev

+0

私は働いているフィドルへのリンクで私の答えを更新しました。 –

+1

@HectorBarbossa '.each()'の各繰り返しで 'Letter'関数と' tbl'変数をなぜ再定義するのですか? – guest271314

関連する問題