天天看點

使用Popup視窗建立無限級Web頁菜單(8)

  最後總結一下示例中的一些小問題,沒有什麼太重要的東西,主要都是為了UI上更好看些。

    顯示特效的支援,使用style的filter來實作的,代碼:

使用Popup視窗建立無限級Web頁菜單(8)

#region Menu.prototype.FadeinEffect = function(effect)

使用Popup視窗建立無限級Web頁菜單(8)

Menu.prototype.FadeinEffect = function(effect)

使用Popup視窗建立無限級Web頁菜單(8)

{

使用Popup視窗建立無限級Web頁菜單(8)

    if ( !this.m_Popup || !this.m_Popup.isOpen )

使用Popup視窗建立無限級Web頁菜單(8)

    {

使用Popup視窗建立無限級Web頁菜單(8)

        return;

使用Popup視窗建立無限級Web頁菜單(8)

    }

使用Popup視窗建立無限級Web頁菜單(8)

    var menuHtml = this.m_Popup.document.getElementById('menu');

使用Popup視窗建立無限級Web頁菜單(8)

    if ( !menuHtml )

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

    var filterString = 'progid:DXImageTransform.Microsoft.';

使用Popup視窗建立無限級Web頁菜單(8)

    switch(effect)

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        case 'GradientWipeLeft2Right' :

使用Popup視窗建立無限級Web頁菜單(8)

        {

使用Popup視窗建立無限級Web頁菜單(8)

            filterString += "GradientWipe(duration='0.5',gradientSize='0.75',motion='forward')";

使用Popup視窗建立無限級Web頁菜單(8)

            break;

使用Popup視窗建立無限級Web頁菜單(8)

        }

使用Popup視窗建立無限級Web頁菜單(8)

        case 'GradientWipeUp2Down' :

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

            filterString += "GradientWipe(duration='0.5',gradientSize='0.25',motion='forward',wipeStyle='1')";

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        case 'RevealTrans' :

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

            filterString += "RevealTrans(duration='0.5',transition='12')";

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        case 'Fade' :

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

            filterString += 'Fade()';

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        default :

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

            filterString = '';        // no effect

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

    if ( filterString )

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        menuHtml.style.visibility = 'hidden';

使用Popup視窗建立無限級Web頁菜單(8)

        menuHtml.style.filter = filterString;

使用Popup視窗建立無限級Web頁菜單(8)

        menuHtml.filters[0].apply();

使用Popup視窗建立無限級Web頁菜單(8)

        menuHtml.style.visibility = 'visible';

使用Popup視窗建立無限級Web頁菜單(8)

        menuHtml.filters[0].play(0.25);

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

};

 #endregion

    菜單條目上Text太長時使用...截斷顯示,代碼如下:

使用Popup視窗建立無限級Web頁菜單(8)

#region Menu.prototype.__isEllipsis = function(menuObj, menuHtml)

使用Popup視窗建立無限級Web頁菜單(8)

Menu.prototype.__isEllipsis = function(menuObj, menuHtml)

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

    if ( menuHtml.offsetWidth > Menu.Attributes.MaxMenuItemTextWidth )

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        for ( var i=0 ; i < menuHtml.rows.length ; ++i )

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

            var tr = menuHtml.rows(i);

使用Popup視窗建立無限級Web頁菜單(8)

            if ( !tr.type || tr.type != 'normal' )

使用Popup視窗建立無限級Web頁菜單(8)

            {

使用Popup視窗建立無限級Web頁菜單(8)

                continue;

使用Popup視窗建立無限級Web頁菜單(8)

            }

使用Popup視窗建立無限級Web頁菜單(8)

            var td = tr.cells(2);

使用Popup視窗建立無限級Web頁菜單(8)

            var span = td.childNodes[0];

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

            if ( span.scrollWidth > Menu.Attributes.MaxMenuItemTextWidth )

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

                td.style.fontWeight = 'bold';

使用Popup視窗建立無限級Web頁菜單(8)

                var doc = menuObj.m_Popup.document;

使用Popup視窗建立無限級Web頁菜單(8)

                var tbl = doc.createElement('TABLE');

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.border = 0;

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.cellpadding = 0;

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.cellspacing = 0;

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.style.textOverflow = 'ellipsis';

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.style.tableLayout = 'fixed';

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.style.color = Menu.Attributes.NormalMenuForeColor;

使用Popup視窗建立無限級Web頁菜單(8)

                var tbody = doc.createElement('TBODY');

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.appendChild(tbody);

使用Popup視窗建立無限級Web頁菜單(8)

                var tr = doc.createElement('TR');

使用Popup視窗建立無限級Web頁菜單(8)

                tbody.appendChild(tr);

使用Popup視窗建立無限級Web頁菜單(8)

                var td = doc.createElement('TD');

使用Popup視窗建立無限級Web頁菜單(8)

                tr.appendChild(td);

使用Popup視窗建立無限級Web頁菜單(8)

                td.innerHTML = span.innerHTML;

使用Popup視窗建立無限級Web頁菜單(8)

                menuHtml.rows(i).cells(2).innerHTML = '';

使用Popup視窗建立無限級Web頁菜單(8)

                menuHtml.rows(i).cells(2).appendChild(tbl);

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.style.lineHeight = '85%';

使用Popup視窗建立無限級Web頁菜單(8)

                td.style.width = '100%';

使用Popup視窗建立無限級Web頁菜單(8)

                td.style.overflow = 'hidden';

使用Popup視窗建立無限級Web頁菜單(8)

                td.style.whiteSpace = 'nowrap';

使用Popup視窗建立無限級Web頁菜單(8)

                td.style.textOverflow = 'ellipsis';

使用Popup視窗建立無限級Web頁菜單(8)

                tbl.title = td.innerText;

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

        menuHtml.style.width = Menu.Attributes.MaxMenuItemTextWidth;

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

    return menuHtml.offsetWidth;

使用Popup視窗建立無限級Web頁菜單(8)

  #endregion

    Menu.Attributes.MaxMenuItemTextWidth是預設的MenuItem上Text的最大寬度,如果超過這個寬度将使用一個table

element把Text包裝起來,然後使用CSS使其産生"..."效果。要用CSS的text-overflow:ellipsis産生"..."效果,這裡有幾個地方需要注意:必須用一個表格包起來,表格的CSS要設定table-layout為fixed;然後文字外的html

element(本例中是td)的樣式表必須設定為:overflow: hidden; white-space: nowrap;

text-overflow: ellipsis;,缺一不行,當然如果有繼承下來的css屬性也可以。 

    菜單中文字被截斷的效果

    比如要顯示如下效果:

MenuItem 1234567890 1234567890 1234567890

    HTML代碼為:

使用Popup視窗建立無限級Web頁菜單(8)

 <table style="table-layout: fixed;" width="200">

使用Popup視窗建立無限級Web頁菜單(8)

     <tr>

使用Popup視窗建立無限級Web頁菜單(8)

         <td style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis;

使用Popup視窗建立無限級Web頁菜單(8)

                 border: solid 1px red;">MenuItem 1234567890 1234567890 1234567890

使用Popup視窗建立無限級Web頁菜單(8)

         </td>

使用Popup視窗建立無限級Web頁菜單(8)

     </tr>

使用Popup視窗建立無限級Web頁菜單(8)

 </table>

    當我們的文字顯示為"..."overflow樣式後,我們還可以查詢到這個狀态,并給這樣的條目添加tooltip。我們隻用比較一下td元素的clientWidth和scrollWidth,如果不相等,也就是scrollWidth

> clientWidth,就說明文本沒有被顯示全(當然出滾動條的時候這個比較也成立)。

    子菜單彈出時如果其右邊的螢幕空間不夠菜單的寬度,菜單将從父級菜單的左邊彈出,使用下面的代碼實作:

使用Popup視窗建立無限級Web頁菜單(8)

 subpop.show(0, 0, 1, 1);

使用Popup視窗建立無限級Web頁菜單(8)

 var menuHtml = subpop.document.getElementById('menu');

使用Popup視窗建立無限級Web頁菜單(8)

 var x, y, w, h;

使用Popup視窗建立無限級Web頁菜單(8)

 x = miHtml.offsetWidth-2;

使用Popup視窗建立無限級Web頁菜單(8)

 y = 0;

使用Popup視窗建立無限級Web頁菜單(8)

 w = menuHtml.offsetWidth;

使用Popup視窗建立無限級Web頁菜單(8)

 w = this.__isEllipsis(subMenuObj, menuHtml);

使用Popup視窗建立無限級Web頁菜單(8)

 h = menuHtml.offsetHeight;

使用Popup視窗建立無限級Web頁菜單(8)
使用Popup視窗建立無限級Web頁菜單(8)

 var availableScreenWidth = window.screen.width;

使用Popup視窗建立無限級Web頁菜單(8)

 var factWidth = popup.document.parentWindow.screenLeft + menuObj.m_Bounds.width + w;

使用Popup視窗建立無限級Web頁菜單(8)

 if ( factWidth > availableScreenWidth )

使用Popup視窗建立無限級Web頁菜單(8)

 {

使用Popup視窗建立無限級Web頁菜單(8)

     x = - w + 2;

使用Popup視窗建立無限級Web頁菜單(8)

 }    

使用Popup視窗建立無限級Web頁菜單(8)

 subpop.show(x, y, w, h, miHtml);

使用Popup視窗建立無限級Web頁菜單(8)

 subMenuObj.FadeinEffect(Menu.Attributes.ShowMenuEffect);

    前面說到到的FadeinEffect和__isEllipsis兩個方法分别是在上面的位置被調用的。 

    如果對于popup制作無限級web頁菜單還有疑問,或者有什麼建議歡迎提出來讨論

使用Popup視窗建立無限級Web頁菜單(8)

本文轉自部落格園鳥食軒的部落格,原文連結:http://www.cnblogs.com/birdshome/,如需轉載請自行聯系原部落客。