js 展开&收缩 二种
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #div1{ width:200px; min-height:20px; border:1px #000 solid;} </style> <script type="text/javascript"> window.onload = function(){ var aInput = document.getElementsByTagName("input"); var oDiv = document.getElementById("div1"); var str = ‘曾经是打工妹,如今她是上市公司的CEO;她曾经被逼婚,如今蜕变为网络第一红娘;她在成就更多人幸福的同时,也成就了自己的事业。她就是"小龙女",世纪佳缘CEO龚海燕‘; var timer = null; var arr = str.split(""); aInput[0].onclick = function(){ clearInterval(timer); timer = setInterval(function (){ var val = arr.shift(); oDiv.innerHTML += val; if(arr.length == 0 ){ clearInterval(timer); } },50); } aInput[1].onclick = function(){ clearInterval(timer); var arr2 = oDiv.innerHTML.split(""); timer = setInterval(function(){ arr.unshift(arr2.pop()); oDiv.innerHTML = arr2.join(""); if(arr2.length == 0 ){ clearInterval(timer); } },50) } aInput[1].onclick = function(){ var arr2 = oDiv.innerHTML.split(""); clearInterval(timer); timer = setInterval(function(){ arr.unshift(arr2.pop()); oDiv.innerHTML = arr2.join(""); if(arr2.length == 0){ clearInterval(timer); } },50) } } </script> </head> <body> <input type="button" value="展开" ><input type="button" value="收缩"> <div id="div1"></div> </body> </html>
<script type="text/javascript"> window.onload = function(){ var aInput = document.getElementsByTagName("input"); var oDiv = document.getElementById("div1"); var str = ‘曾经是打工妹,如今她是上市公司的CEO;她曾经被逼婚,如今蜕变为网络第一红娘;她在成就更多人幸福的同时,也成就了自己的事业。她就是"小龙女",世纪佳缘CEO龚海燕‘; var timer = null; var iNow = 0; aInput[0].onclick = function(){ clearInterval(timer); timer = setInterval(function (){ var val = str.substring(0,++iNow); oDiv.innerHTML = val; if(val == str){ clearInterval(timer); } },50); } aInput[1].onclick = function(){ clearInterval(timer); timer = setInterval(function(){ var val = str.substring( 0 , iNow-- ) oDiv.innerHTML = val; if(val == "" ){ clearInterval(timer); } },50) } } </script>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。