6

Jquery / Javascriptを使用して、壊れたタイプライターフォントを模倣しようとしています(見つからなかったため)。しかし、どの文字が壊れるかをランダムにしたいと思います。必要なIDの文字列を分割し、見つけたコードを少し使用して、0から文字列の全長までの乱数を取得することができました。私が今問題を抱えているのは、その特定のキャラクターで何かをしていることです。数ピクセル上下に押したいです。マージンやパディングを追加できるようにクラスを設定しようとしましたが、機能しません。だから私は今いるところに行き詰まっています。

これがページです、私は「ABOUT」という言葉にそれをやろうとしています:http:
//www.franciscog.com/bs/about.php

スクリプトは次のとおりです。

<script type="text/javascript">

        function randomXToY(minVal,maxVal,floatVal)
            {
              var randVal = minVal+(Math.random()*(maxVal-minVal));
              return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
            }


        var str = $('#typehead').text();
                var strcnt = str.length;
        var exploded = str.split('');
        var rdmltr =randomXToY(0,strcnt); 
        var theLetter = exploded[rdmltr];
        theLetter.addClass("newClass");
        var toffset = $('.newClass').offset();
        alert(toffset.left + "," + toffset.top);

     </script>
4

2 に答える 2

4

編集:一致する文字がスペース文字ではないことを確認するために更新され、@abelitoによって提案された小さなスタイルが追加されました。

これはどうですか:http://jsfiddle.net/cgXa3/4/

function randomXToY(minVal,maxVal,floatVal){
    var randVal = minVal+(Math.random()*(maxVal-minVal));
    return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}


var exploded = $('#typehead').text().split('');
var rdmltr = randomXToY(0,exploded.length);

    // Make sure we don't get a space character
while(exploded[rdmltr] == ' ') {
    rdmltr = randomXToY(0,exploded.length);
}
    // Wrap the letter with a span that has the newClass
    //   and update it in the array
exploded[rdmltr] = '<span class="newClass">' + exploded[rdmltr] + '</span>';

    // Update the content
$('#typehead').html(exploded.join(''));
var toffset = $('.newClass').offset();
alert(toffset.left + "," + toffset.top);​

更新:複数に適用する場合:http://jsfiddle.net/cgXa3/5/

于 2010-07-19T01:24:45.330 に答える
0

私はパトリックの答えが好きですが、別の方法として、テキスト全体で同じ文字を変更します。また、少し回転させることもできます(ただし、これはIEでは機能しません)。Patrickからフォークしたデモを作成しました。

CSS

.newClass {
 left: 0px;
 top: -1px;
 color: red;
 position:relative;
 -webkit-transform: rotate(-5deg); 
 -moz-transform: rotate(-5deg); 
}

コード

function randomLetter(cased){
 // case = true for uppercase, false for lowercase
 var base = (cased) ? 65 : 97;
 // convert HTML escape code into a letter
 var rand = $('<span>&#' + parseInt(base+(Math.random()*25),10) + ';</span>');
 return rand.text();
};

$(document).ready(function(){
 var ltr = randomLetter(false);
 var reg = new RegExp( ltr, 'g');
 $('#typehead').html(function(i,html){
  return html.replace(reg, '<span class="newClass">' + ltr + '</span>');
 });
});

更新:これは、複数のh1タグに適用するために必要なコードです(更新されたデモ):

function randomXToY(minVal,maxVal,floatVal){
 var randVal = minVal+(Math.random()*(maxVal-minVal));
 return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

$('.typehead').each(function() {                  
 //access the text and characters within the tag with the id typehead 
 var exploded = $(this).text().split('');
 var rdmltr = randomXToY(0,exploded.length);

 // Make sure we don't get a space character or undefined
 while(exploded[rdmltr] == ' ' || exploded[rdmltr] == undefined) {
  rdmltr = randomXToY(0,exploded.length);
 }

 // Wrap the letter with a span that has the new class brokenType
 //   and update it in the array
 exploded[rdmltr] = '<span class="brokenType">' + exploded[rdmltr] + '</span>'; 

 // Update the content
 $(this).html(exploded.join(''));
});
于 2010-07-19T05:05:40.380 に答える