选择字符串中的特定字符并使用 Jquery 对其进行偏移(视觉上)
我正在尝试使用 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>
I'm trying to use Jquery/Javascript to mimic a broken typewriter font (since I could not find one). But I want to make it random which letter gets broken. I was able to split the string of the id that I wanted and use a bit of code I found to get a random number between 0 and the total length of the string. What I'm having problem with now is doing something with that specific character. I want to push it down or up a few pixels. I was trying to give it a class so I could add some margin or padding, but it doesn't work. So I'm stuck where I am now.
here's the page, I'm trying to do it to the word "ABOUT":
http://www.franciscog.com/bs/about.php
here's the script:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:更新以确保匹配的字符不是空格字符,并添加了@abelito建议的一点样式。
怎么样: http://jsfiddle.net/cgXa3/4/
更新: 如果您想将其应用于多个: http://jsfiddle.net/cgXa3/5/
EDIT: Updated to ensure that the matched character is not a space character, and added a little style suggested by @abelito.
How about this: http://jsfiddle.net/cgXa3/4/
Update: If you want to apply it to several: http://jsfiddle.net/cgXa3/5/
我喜欢帕特里克的回答,但作为替代方案,我会更改整个文本中的同一字母。也许还可以稍微旋转一下(尽管这在 IE 中不起作用)。我制作了一个演示,这是我从 Patrick 那里分叉出来的。
CSS
代码
更新:这是应用于多个 h1 标签所需的代码(更新的演示):
I like Patrick's answer, but as an alternative I would alter the same letter throughout the text. And maybe rotate it a tiny bit as well (although this won't work in IE). I made a demo which I forked off of Patrick's.
CSS
Code
Update: This is the code needed to apply to multiple h1 tags (updated demo):