6

我正在尝试使用 Jquery/Javascript 来模仿损坏的打字机字体(因为我找不到)。但我想让哪个字母被破坏是随机的。我能够拆分我想要的 id 字符串,并使用我发现的一些代码来获得一个介于 0 和字符串总长度之间的随机数。我现在遇到的问题是用那个特定的角色做一些事情。我想将它向下或向上推几个像素。我试图给它一个类,所以我可以添加一些边距或填充,但它不起作用。所以我被困在我现在的位置。

这是页面,我正在尝试对“关于”一词进行操作:
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 中不起作用)。我做了一个演示,我从帕特里克那里分叉出来。

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 回答