简体   繁体   English

如何自动调整此HTML菜单的大小以适合父级宽度?

[英]How can I auto size this HTML Menu to fit parent width?

I am converting a Drupal 6 theme to Drupal 7, and cant figure this part out. 我正在将Drupal 6主题转换为Drupal 7,但无法弄清楚这部分。 I have the following HTML: 我有以下HTML:

<ul id="nav" class=" scaling-active scaling-ready">
<li><a href="/demos/pro/taxonomy/term/12" title="">Design</a></li>
<li><a href="/demos/pro/taxonomy/term/13" title="">Inspiration</a></li>
<li><a href="/demos/pro/taxonomy/term/14" title="">Nature</a></li>
<li><a href="/demos/pro/taxonomy/term/15" title="">Photography</a></li>
<li><a href="/demos/pro/taxonomy/term/16" title="">Technology</a></li>
<li><a href="/demos/pro/taxonomy/term/20" title="">Travel</a></li>
<li><a href="/demos/pro/taxonomy/term/17" title="">Tutorials</a></li>
<li><a href="/demos/pro/taxonomy/term/18" title="">Urban</a></li>
<li><a href="/demos/pro/taxonomy/term/19" title="">Video Games</a></li>
</ul>

In Drupal 6 this theme was using jquery v1.3.2, but in Drupal 7 jquery 1.4.4 is built in, so the functions don't seem to be working. 在Drupal 6中,此主题使用的是jquery v1.3.2,但在Drupal 7中内置了jquery 1.4.4,因此这些功能似乎无法正常工作。 Here is the jquery function: 这是jquery函数:

$(function(){
    clearFormFields({
        clearInputs: true,
        clearTextareas: false,
        passwordFieldText: true,
        addClassFocus: "focus",
        filterClass: "form-text"
    });
    initAutoScalingNav({
        menuId: "nav",
        sideClasses: true
    });
    ieHover('#nav li');
    $('div.gallery-block').fadeGallery({
        slideElements:'ul.gallery > li',
        pagerLinks:'ul.switcher li'
    });
    $('div.pictures-box').fadeGallery({
        slideElements:'ul.fade-gallery > li',
        pagerLinks:'ul.pictures-list li',
        title: true
    });
});

function initAutoScalingNav(o) {
    if (!o.menuId) o.menuId = "nav";
    if (!o.tag) o.tag = "a";
    if (!o.spacing) o.spacing = 0;
    if (!o.constant) o.constant = 0;
    if (!o.minPaddings) o.minPaddings = 0;
    if (!o.liHovering) o.liHovering = false;
    if (!o.sideClasses) o.sideClasses = false;
    if (!o.equalLinks) o.equalLinks = false;
    if (!o.flexible) o.flexible = false;
    var nav = document.getElementById(o.menuId);
    if(nav) {
        nav.className += " scaling-active";
        var lis = nav.getElementsByTagName("li");
        var asFl = [];
        var lisFl = [];
        var width = 0;
        for (var i=0, j=0; i<lis.length; i++) {
            if(lis[i].parentNode == nav) {
                var t = lis[i].getElementsByTagName(o.tag).item(0);
                asFl.push(t);
                asFl[j++].width = t.offsetWidth;
                lisFl.push(lis[i]);
                if(width < t.offsetWidth) width = t.offsetWidth;
            }
            if(o.liHovering) {
                lis[i].onmouseover = function() {
                    this.className += " hover";
                }
                lis[i].onmouseout = function() {
                    this.className = this.className.replace("hover", "");
                }
            }
        }
        var menuWidth = nav.clientWidth - asFl.length*o.spacing - o.constant;
        if(o.equalLinks && width * asFl.length < menuWidth) {
            for (var i=0; i<asFl.length; i++) {
                asFl[i].width = width;
            }
        }
        width = getItemsWidth(asFl);
        if(width < menuWidth) {
            var version = navigator.userAgent.toLowerCase();
            for (var i=0; getItemsWidth(asFl) < menuWidth; i++) {
                asFl[i].width++;
                if(!o.flexible) {
                    asFl[i].style.width = asFl[i].width + "px";
                }
                if(i >= asFl.length-1) i=-1;
            }
            if(o.flexible) {
                for (var i=0; i<asFl.length; i++) {
                    width = (asFl[i].width - o.spacing - o.constant/asFl.length)/menuWidth*100;
                    if(i != asFl.length-1) {
                        lisFl[i].style.width = width + "%";
                    }
                    else {
                        if(navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
                            lisFl[i].style.width = width + "%";
                    }
                }
            }
        }
        else if(o.minPaddings > 0) {
            for (var i=0; i<asFl.length; i++) {
                asFl[i].style.paddingLeft = o.minPaddings + "px";
                asFl[i].style.paddingRight = o.minPaddings + "px";
            }
        }
        if(o.sideClasses) {
            lisFl[0].className += " first-child";
            lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
            lisFl[lisFl.length-1].className += " last-child";
            lisFl[lisFl.length-1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
        }
        nav.className += " scaling-ready";
    }
    function getItemsWidth(a) {
        var w = 0;
        for(var q=0; q<a.length; q++) {
            w += a[q].width;
        }
        return w;
    }
}

In the Drupal 6 version the above code automatically adds the style="width: xx" tags to the hyperlinks, which causes the menu to grow the menu buttons to fill the width of its container. 在Drupal 6版本中,以上代码自动将style =“ width:xx”标签添加到超链接,这会使菜单增加菜单按钮以填充其容器的宽度。

Thanks. 谢谢。

I would suggest throwing out that old javascript and replace with jQuery - seeing as someone else has already solved this problem : 我建议扔掉旧的JavaScript并替换为jQuery-看到别人已经解决了这个问题:

http://tympanus.net/codrops/2010/01/12/self-resizing-navigation-menu-with-jquery/ http://tympanus.net/codrops/2010/01/12/self-resizing-navigation-menu-with-jquery/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用html标签调整字体大小和重排文本以适合父div的宽度和高度 - How to adjust font-size and reflow text to fit the width and height of a parent div with html tags 如何自动适应/调整 html5 textarea 元素以适应初始内容? - How can I auto fit/resize a html5 textarea element to fit the initial content? 如何使用JavaScript自动缩小文本以适合div的宽度和高度? - How can I auto shrink text to fit div width and height using javascript? 如何动态调整表格列的大小以适应内容、左对齐并保持最大宽度? - How can I dynamically size table columns to fit content, left justify, and keep a max width? 如何使用内部宽度按比例缩放我的网页以适应任何显示尺寸? - How can I scale my webpage proportionally using inner width to fit any display size? “最适合”字体大小 - 如何测量句子宽度? - “Best fit” font size - how do I measure a sentence width? 如何根据 HTML CSS JavaScript 中的纵横比调整高度或宽度? - How can I fit either height or width based on aspect ratio in HTML CSS JavaScript? 如何设置图像的高度和宽度,使其适合我在 fabric.js 中的 canvas 的大小? - How do I set both height and width of image so that it can fit the size of my canvas in fabric.js? 如何调整列表元素的大小以适合父宽度 - How to resize list elements to fit parent width 如何使HTML表格具有自动宽度但大于屏幕宽度? - How can I make an HTML table auto width but larger than the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM