JS-發布訊息的一個運動小效果,js-發布訊息
模仿一些網站發布訊息時候,淡入,緩慢滑動的小效果,廢話不多說,看下最終測試效果!有圖有真相!
接下來一步步分析實現的過程,一下是為了示範過程中一些注意點,用的測試代碼,不是最終效果代碼,最終效果代碼,我貼在文章最後!!
html代碼
<textarea id="content" cols="60" rows="10"></textarea><br><input id="btn1" type="button" value="發布"><ul id="ulList"> <li>1233</li></ul>
樣式代碼
<style> *{padding: 0;margin: 0;} #ulList{ list-style: none; width: 300px;height: 300px; border: 1px solid #000; margin: 10px auto; } #ulList li{ border-bottom: 1px dashed #c00; padding: 2px; }</style>
<script> window.onload = function () { var oTxt = document.getElementById('content'); var oBtn = document.getElementById('btn1'); var oUl = document.getElementById('ulList'); oBtn.onclick = function () { var oLi = document.createElement('li');// 建立一個li元素 oLi.innerHTML = oTxt.value; oTxt.value = ''; oUl.appendChild(oLi); } }</script>
這裡每次新建立的li都是放在尾部,如果想讓它建立好放在頂部,需要判斷一下,如果當前ul裡沒有li,那麼直接appendChild,如果有已經有li元素,就用insertBefore
oBtn.onclick = function () { var oLi = document.createElement('li'); var aLi = oUl.getElementsByTagName('li');// 擷取的ul下的所有li oLi.innerHTML = oTxt.value; oTxt.value = ''; if(aLi.length){ // 判斷ul裡有沒有li元素 oUl.insertBefore(oLi); }else{ oUl.appendChild(oLi); } }
到這裡插入每一條內容,都會從頭部插入了,但是插入的動作太生硬了,這裡就用我們的運動架構,來使插入變得平滑;
首先保證先引入了運動架構:<script src="move.js"></script> 運動架構在《JS完美運動架構》中有代碼
<script> window.onload = function () { var oTxt = document.getElementById('content'); var oBtn = document.getElementById('btn1'); var oUl = document.getElementById('ulList'); oBtn.onclick = function () { var oLi = document.createElement('li'); var aLi = oUl.getElementsByTagName('li');// 擷取的ul下的所有li oLi.innerHTML = oTxt.value; oTxt.value = ''; if(aLi.length){ // 判斷ul裡有沒有li元素 oUl.insertBefore(oLi,aLi[0]);// 在下標為0的li前插入oLi }else{ oUl.appendChild(oLi); var iHeight = oLi.offsetHeight; oLi.style.height = 0;// 然後把li的高度置0 startMove(oLi,{height:iHeight}, function () { startMove(oLi,{opacity:100}); }) } }</script>
因為每個li的高度是不固定的,
var iHeight = oLi.offsetHeight;// 1.上來先把li的高度存一下
oLi.style.height = 0;// 2. 然後把li的高度置0
startMove(oLi,{height:iHeight})// 3. 通過運動架構重新把li的高度設回去
看一下運行效果發現,做運動的時候文字會超出li的邊框(圖中文字開始時超出了紅框的),需要在li的樣式設定中:使overflow:hidden,問題就解決了
#ulList li{ border-bottom: 1px dashed #c00; padding: 2px; overflow: hidden;}
到這裡問題就解決了,但是在IE7 下會有先卡一下在出來的的現象,所以這裡需要將原來的ul嵌套li的布局,改成div套div
<textarea id="content" cols="30" rows="10"></textarea><br><input id="btn1" type="button" value="發布"><br><div id="ulList"> <div>1233</div></div>
<script> window.onload = function () { var oTxt = document.getElementById('content'); var oBtn = document.getElementById('btn1'); var oUl = document.getElementById('ulList'); oBtn.onclick = function () { var oLi = document.createElement('div');//這裡將li改成div var aLi = oUl.getElementsByTagName('div');//這裡將li改成div oLi.innerHTML = oTxt.value; oTxt.value = ''; if(aLi.length){ oUl.insertBefore(oLi,aLi[0]); }else{ oUl.appendChild(oLi); var iHeight = oLi.offsetHeight; oLi.style.height = 0; startMove(oLi,{height:iHeight}, function () { startMove(oLi,{opacity:100});// 透明度改變 }) } }</script>
到這裡效果就實現了
為了美觀,修改一下樣式,最終效果的代碼如下
<div class="container"> <textarea id="content" cols="60" rows="10"></textarea><br> <input id="btn1" type="button" value="發布"><br> <div id="msgList"> <div></div> </div></div>
<style> *{padding: 0;margin: 0;} body{background: url("body_repeat.png");} .container{ width: 500px; margin: 10px auto; } #content{ padding: 8px; background: rgba(255,255,255,.6); border: none; box-shadow: 2px 2px 3px rgba(0,0,0,.6); border-radius: 5px; } #btn1{ border: none; width: 60px;height: 40px; background: #ffc09f; font-family:"微軟雅黑";font-size:16px;color: #fff; border-radius: 5px; } #msgList{ list-style: none; width: 420px;_height: 400px;min-height: 400px; background: rgba(255,255,255,.8); border-radius: 5px; border: 1px solid #eee; margin: 10px ; } #msgList div{ border-bottom: 1px dashed #aaa; padding: 2px; opacity: 0; filter:alpha(opacity:0); overflow: hidden; } </style>
<script src="move.js"></script><script> window.onload = function () { var oTxt = document.getElementById('content'); var oBtn = document.getElementById('btn1'); var oDiv = document.getElementById('msgList'); oBtn.onclick = function () { var newDiv = document.createElement('div');//每發布一條訊息需要建立一個新div var aDiv = oDiv.getElementsByTagName('div');// 擷取msgList的下的所有div元素 newDiv.innerHTML = oTxt.value;//新div的內容就等於textarea裡發布內容 oTxt.value = ''; // 然後把textarea清空 if(aDiv.length){ // 判斷msgList裡有沒有div元素 oDiv.insertBefore(newDiv,aDiv[0]);// 在下標為0的div前插入一個div }else{ oDiv.appendChild(newDiv); } // 接下來是添加運動部分,因為插入的每個div的高度不固定 var iHeight = newDiv.offsetHeight;// 先把div的高度存一下 newDiv.style.height = 0;// 然後把div的高度置0 startMove(newDiv,{height:iHeight}, function () { startMove(newDiv,{opacity:100}); })//通過運動架構重新把div的高度設回去 } }</script>