asp.net javascript 滑動收縮多級菜單

來源:互聯網
上載者:User

  我們先來看看最終效果:http://www.orfeo-casa/index.aspx 左邊的菜單

主要的JAVASCRIPT:

<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 = "#000";
}

head.onmouseout = mouseout;

head.onclick = function() {
this.style.background = "#000";
if (!state.show && !state.hidden) {
if (!isOpen) {
head.onmouseout = null;
show();
} else {
hidden();
}

}
}

function mouseout() {
this.style.background = "#000"
}
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>

第二步:下面我詳細介紹下怎麼樣動態產生菜單:

HTML部分:

 <div id="Left">  
           <asp:Literal ID="ltaCategory" runat="server"></asp:Literal>
      </div>

CODE:

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.ltaCategory.Text = GetCategory();
        }
    }

/// <summary>
    /// 擷取產品分類
    /// </summary>
    /// <returns></returns>
    public string GetCategory()
    {
        Maticsoft.BLL.productclass bll = new Maticsoft.BLL.productclass();
        List<Maticsoft.Model.productclass> list = Maticsoft.BLL.productclass.get_List();
        StringBuilder strResult = new StringBuilder();
        strResult.AppendLine("<ul class=\"rolinList\" id=\"rolin\">");
        if (list.Count > 0)
        {   
            foreach (Maticsoft.Model.productclass model in list)
            {
                strResult.AppendLine("<li>");
                strResult.Append(string.Format("<h2>{0}</h2>",model.productname));
                strResult.AppendLine("<div class=\"content\">");
                strResult.AppendLine(GetChildCategory(model.id.ToString()));
                strResult.AppendLine("</div>");
            }
        }
        strResult.AppendLine("</ul>");
        return strResult.ToString();
    }
    string  GetChildCategory(string ParentID)
    {
        List<Maticsoft.Model.productclass> list = Maticsoft.BLL.productclass.get_List(ParentID);
        StringBuilder strResult = new StringBuilder();
        if (list.Count > 0)
        {
            foreach (Maticsoft.Model.productclass model in list)
            {
                strResult.AppendLine(string.Format("<a href=\"/List.aspx?cid={0}&action=bind\">{1}</a><br/>", model.id, model.productname));
            }
        }
        return strResult.ToString();
    } 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.