下面将分享一个用css和javascript模拟手写明信片的动画效果。在写信的过程中,当笔变干的时候 , 会自动的去浸入墨水;当写错了字,怎么办?没关系,他会划掉重新写。嘿嘿。强大把,现在我们一起来看看他的实现方法以及效果。
第一步:准备HTML代码
首先,我们先创建基本的html代码文件和信的内容:
index.html
<div id="letter"></div> <img id="inkwell1" src="inkwell1.gif" alt="inkwell1" /> <img id="inkwell2" src="inkwell2.gif" alt="inkwell2" /> <div id="letter_src"> A man named Grant once foi|und a box of old Papers in his dwelling||||||||house. Grant didn't like old things. So he burned most of the papers. But one of these papers was a letter. He read it. A well-known writer had written it.<br><br> 'About a million|||||||hundred years ago nobody know about him|||this writer,' thought Grant. 'Nobody read his books. But now everybody reads him. Some people like to buy old letters. I can get a lot of money for this letter.'<br><br> But the letter looked dirty. So Grant decided to wash |||||clean it. He worked hard and soon the letter looked new. Grant was not|||||||was very happy.<br><br> He took the letter to a shop in London where they bought and sold old papers. 'I want to sell this letter,' Grant said to the man in shop. 'It is a well-known writer's letter. How much will you give me for it?'<br><br> The man looked at the letter for a long time. 'I'll give you two pounds for it,' he said at last.<br><br> 'Only two pounds!' said Grant. 'But people pay ten pounds for old letters. And I have even cleaned it.'<br><br> 'I can see that,' said the man. 'That's the mistake. People who buy old papers like dirty papers more than clean papers.'<br><br> </div>
第二步:定义CSS样式文件
body { background: url('bg.jpg') no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } #inkwell1 { bottom: 100px; left: 140px; position: fixed; } #inkwell2 { bottom: 100px; left: 140px; position: fixed; visibility: hidden; } #letter { font-family: Comic Sans MS; font-size: 18px; font-weight: bold; margin: 50px auto; position: relative; width: 75%; } #letter_src { display: none; }
第三步:创建javascript文件
window.onload = function(){ // public variables var vLetter = document.getElementById('letter'); var iSpeedInk = 5; // other variables var sText = document.getElementById('letter_src').innerHTML; var iCurChar = 0; var sChars = '<span>'; var iCurInk = 0; var sCurCaret = ''; var sCaret = " <img src='pen.gif' style='position:absolute' />"; var doStep = function () { // current char var sChar = sText.charAt(iCurChar); // default char delay var iDelay = 32; if (sChar == '') { sCurCaret = ''; } else if (sChar == '|') { // we use | symbol to emulate 'error' symbol sChar = ''; sChars = sChars.substring(0, sChars.length-1); iDelay = 64; } else if (sChar == '<') { // pass tags var iPos = sText.indexOf('>', iCurChar); sChar = sText.substring(iCurChar, iPos + 1); iCurChar = iPos; } else if (sChar == '&') { // pass html entities var iPos = sText.indexOf(';', iCurChar); sChar = sText.substring(iCurChar, iPos + 1); iCurChar = iPos; } else if (sChar == '.') { // custom delay in case of . symbol iDelay = 300; } else if (sChar == ',') { // custom delay in case of , symbol iDelay = 100; } else if (sChar == ' ') { // custom delay in case of space symbol iDelay = 32; } else if (iCurChar > 5) { sCurCaret = sCaret; } // expenditure of ink if (sChar == ' ') { iCurInk += iSpeedInk; sChar = '</span><span style="color:RGB(' + (iCurInk) + ',' + (iCurInk) + ',' + (iCurInk) + ')">' + sChar; } if (document.getElementById('inkwell2').style.visibility == 'visible') { sCurCaret = sCaret; document.getElementById('inkwell2').style.visibility = 'hidden'; sChar = '</span><span style="color:RGB(0,0,0)">' + sChar; } // refresh Ink if (iCurInk > 160) { iCurInk = 0; document.getElementById('inkwell2').style.visibility = 'visible'; iDelay = 1000; sCurCaret = ''; } // add current char to chars sChars += sChar; // hide the caret at the end of the letter if (iCurChar == sText.length - 1) sCurCaret = ''; // update letter with new chars vLetter.innerHTML = sChars + sCurCaret; // goto next char iCurChar++; // next step if (iCurChar < sText.length) { setTimeout(doStep, 20 + iDelay); } } doStep(); };
ok,一起来看看demo吧,也一起提供附件下载。
原文地址:script-tutorials.com
知道了,使用空格区分,英文一个单词一个空格,中文没空格
发现一个bug,英文时会自己去沾墨水,中文时不可以
支持中文的!只要修改下index.html文件,然后另存为utf-8格式!
不支持中文,这个是硬伤
这个很牛X,逢年过节、过个生日啥的,做好后发个链接过去,那气派啊!收了源码
预览了下,有点意思,墨水还能变淡,但有一个就是:既然是书写的写错了是不是直接划掉效果会更好点,擦掉有点像删除的
嗯,这个建议不错,既然来源于生活,那就还原生活
很好啊,非常喜欢
帅锅。网站很漂亮啊~
帅惨了
多谢支持
不错不错啊
喜欢就好
不错
汗颜……
谢谢你的大爱