JS實現(xiàn)的適合做faq或menu滑動效果示例
本文實例講述了JS實現(xiàn)的適合做faq或menu滑動效果。分享給大家供大家參考,具體如下:
<!CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body,div,ul,li,p,h1,h2{ margin:0; padding:0; border:0; background:#FAFAFA; font-family:Arial, Helvetica, sans-serif,"宋體"}
body{ text-align:center; font-size:12px}
li{ list-style:none}
.rolinList{ width:402px; height:auto; margin:20px auto 0 auto; text-align:left}
.rolinList li{margin-bottom:1px;border:1px solid #DADADA}
.rolinList li h2{ width:380px; height:40px; background:#fff; font-size:14px; line-height:40px; padding-left:20px; color:#333; cursor:pointer}
.content{ height:150px;width:400px; background:#fff; background:#FAFAFA}
.content p{ margin:12px}
-->
</style>
<script type="text/javascript">
//<![CDATA[
window.onload = function() {
rolinTab("rolin")
}
function rolinTab(obj) {
var list = $(obj).getElementsByTagName("LI");
var state = {show:false,hidden:false,showObj:false};
for (var i=0; i<list.length; i++) {
var tmp = new rolinItem(list[i],state);
if (i == 0) tmp.pShow();
}
}
function rolinItem(obj,state) {
var speed = 0.0666;
var range = 1;
var interval;
var tarH;
var tar = this;
var head = getFirstChild(obj);
var content = getNextChild(head);
var isOpen = false;
this.pHidden = function() {
if (isOpen) hidden();
}
this.pShow = show;
var baseH = content.offsetHeight;
content.style.display = "none";
var isOpen = false;
head.onmouseover = function() {
this.style.background = "#EFEFEF";
}
head.onmouseout = mouseout;
head.onclick = function() {
this.style.background = "#EFEFEF";
if (!state.show && !state.hidden) {
if (!isOpen) {
head.onmouseout = null;
show();
} else {
hidden();
}
}
}
function mouseout() {
this.style.background = "#FFF"
}
function show() {
head.style.borderBottom = "1px solid #DADADA";
state.show = true;
if (state.openObj && state.openObj != tar ) {
state.openObj.pHidden();
}
content.style.height = "0px";
content.style.display = "block";
content.style.overflow = "hidden";
state.openObj = tar;
tarH = baseH;
interval = setInterval(move,10);
}
function showS() {
isOpen = true;
state.show = false;
}
function hidden() {
state.hidden = true;
tarH = 0;
interval = setInterval(move,10);
}
function hiddenS() {
head.style.borderBottom = "none";
head.onmouseout = mouseout;
head.onmouseout();
content.style.display = "none";
isOpen = false;
state.hidden = false;
}
function move() {
var dist = (tarH - content.style.height.pxToNum())*speed;
if (Math.abs(dist) < 1) dist = dist > 0 ? 1: -1;
content.style.height = (content.style.height.pxToNum() + dist) + "px";
if (Math.abs(content.style.height.pxToNum() - tarH) <= range ) {
clearInterval(interval);
content.style.height = tarH + "px";
if (tarH != 0) {
showS()
} else {
hiddenS();
}
}
}
}
var $ = function($) {return document.getElementById($)};
String.prototype.pxToNum = function() {return Number(this.replace("px",""))}
function getFirstChild(obj) {
var result = obj.firstChild;
while (!result.tagName) {
result = result.nextSibling;
}
return result;
}
function getNextChild(obj) {
var result = obj.nextSibling;
while (!result.tagName) {
result = result.nextSibling;
}
return result;
}
//]]>
</script>
<ul id="rolin">
<li>
<h2>腳本之家 1</h2>
<div <p>腳本之家--專業(yè)IT綜合性網(wǎng)站<br /><br /><a target="_blank" href="http://www.dhdzp.com/">http://www.dhdzp.com/</a></p>
</div>
</li>
<li>
<h2>腳本之家 2</h2>
<div <p>腳本之家--專業(yè)IT綜合性網(wǎng)站<br /><br /><a target="_blank" href="http://www.dhdzp.com/">http://www.dhdzp.com/</a></p>
</div>
</li>
<li>
<h2>腳本之家 3</h2>
<div <p>腳本之家--專業(yè)IT綜合性網(wǎng)站<br /><br /><a target="_blank" href="http://www.dhdzp.com/">http://www.dhdzp.com/</a></p>
</div>
</li>
<li>
<h2>腳本之家 4</h2>
<div <p>腳本之家--專業(yè)IT綜合性網(wǎng)站<br /><br /><a target="_blank" href="http://www.dhdzp.com/">http://www.dhdzp.com/</a></p>
</div>
</li>
<li>
<h2>腳本之家 5</h2>
<div <p>腳本之家--專業(yè)IT綜合性網(wǎng)站<br /><br /><a target="_blank" href="http://www.dhdzp.com/">http://www.dhdzp.com/</a></p>
</div>
</li>
<li>
<h2>腳本之家 6</h2>
<div <p>腳本之家--專業(yè)IT綜合性網(wǎng)站<br /><br /><a target="_blank" href="http://www.dhdzp.com/">http://www.dhdzp.com/</a></p>
</div>
</li>
</ul>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
微信小程序?qū)崿F(xiàn)密碼顯示與隱藏的睜眼閉眼功能
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)密碼顯示與隱藏的睜眼閉眼功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
JavaScript實現(xiàn)字符串轉(zhuǎn)數(shù)組的6種方法總結(jié)
數(shù)組是?JavaScript?中最強大的數(shù)據(jù)結(jié)構(gòu),我們常常通過將字符串轉(zhuǎn)換為數(shù)組來解決許多算法。本文為大家總結(jié)了6個JS字符串轉(zhuǎn)數(shù)組的方法,希望對你有所幫助2022-09-09
使用bootstrap typeahead插件實現(xiàn)輸入框自動補全之問題及解決辦法
這篇文章主要介紹了使用bootstrap typeahead插件實現(xiàn)輸入框自動補全之問題及解決辦法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
JS拖動技術(shù) 關(guān)于setCapture使用
JS拖動技術(shù) 關(guān)于setCapture使用,學(xué)習(xí)js拖動效果的朋友可以參考下。2010-12-12
javascript將浮點數(shù)轉(zhuǎn)換成整數(shù)的三個方法
將浮點數(shù)轉(zhuǎn)換成整數(shù)方法有很多,本例為大家介紹常用的三個方法,如果讀者想到其他好用方法,也可以交流一下2014-06-06

