在jquery小部件中以margin auto为中心

时间:2012-10-15 06:16:44

标签: javascript jquery css

我有一个jquery小部件我正在制作我想要居中的一些div。这是我试图做的代码,我似乎无法让它工作。

jQuery.fn.nav = function(home, eat, sleep, drink) {
//store the jQuery object(s) for later use
var elements = this;

return this.each(function() {
    var containerElement = this;
    var container = jQuery(this);
    container.css('width', '100%');
    container.css('text-align', 'center');

    var centElement = document.createElement('div');
    var cent = jQuery(centElement);
    cent.css('overflow', 'auto');
    cent.css('width', '275px');
    cent.css('margin-left', 'auto');
    cent.css('margin-right', 'auto');

    if(home == true) {
        var navElement = document.createElement('div');
        var nav = jQuery(navElement);
        nav.addClass('inactive');
        nav.attr('id', 'home');
        cent.append(nav);
    }
    if(eat == true) {
        var navElement = document.createElement('div');
        var nav = jQuery(navElement);
        nav.addClass('inactive');
        nav.attr('id', 'eat');
        cent.append(nav);
    }
    if(drink == true) {
        var navElement = document.createElement('div');
        var nav = jQuery(navElement);
        nav.addClass('inactive');
        nav.attr('id', 'drink');
        cent.append(nav);
    }
    if(sleep == true) {
        var navElement = document.createElement('div');
        var nav = jQuery(navElement);
        nav.addClass('inactive');
        nav.attr('id', 'sleep');
        cent.append(nav);
    }
    container.append(cent);
    //put together the styling used
    var styleText = "#" + containerElement.id + " div {width: 60px; height: 60px; float: left;";
    styleText += " margin-left: 5px; margin-right: 3px;}";
    styleText += "#" + containerElement.id + " div a {display: block; width: 100%; height: 100%;}";
    styleText += "#" + containerElement.id + " #home.inactive {background-image:url(img/inactive_home.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #eat.inactive {background-image:url(img/inactive_eat.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #sleep.inactive {background-image:url(img/inactive_sleep.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #drink.inactive {background-image:url(img/inactive_drink.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #home.active {background-image:url(img/active_home.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #eat.active {background-image:url(img/active_eat.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #sleep.active {background-image:url(img/active_sleep.png); background-size: cover;}";
    styleText += "#" + containerElement.id + " #drink.active {background-image:url(img/active_drink.png); background-size: cover;}";
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = styleText;
    document.getElementsByTagName('head')[0].appendChild(style);
});
}

让我解释一下,因为人们似乎不喜欢我的问题。我一直使用'margin:0 auto'来居中div,事实上,这就是我将下面链接页面的主体(easyuniv.com/staging)集中在一起。我有这个javascript函数,它在调用它的div里面创建我的导航。变量容器是调用它的div,我将其宽度设置为100%,然后在其中附加一个div(称为cent),它具有margin-left和margin-right auto。

出于某种原因,我不能让这个以我的div为中心,并试图找出原因。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

好的,得到了​​你的问题。使用此CSS:

#hesd div {margin: 25px auto 10px; float: none;}

截图:

答案 1 :(得分:1)

使用以下格式更改var styleText

    var styleText = "#" + containerElement.id + " div {width: 60px; height: 60px;";
    styleText += " margin-left: 5px; margin-right: 3px;}";
    styleText += "#" + containerElement.id + " div a {display: block; width: 100%; height: 100%;}";
    styleText += "#" + containerElement.id + " #home.inactive {background-image:url(img/inactive_home.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #eat.inactive {background-image:url(img/inactive_eat.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #sleep.inactive {background-image:url(img/inactive_sleep.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #drink.inactive {background-image:url(img/inactive_drink.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #home.active {background-image:url(img/active_home.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #eat.active {background-image:url(img/active_eat.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #sleep.active {background-image:url(img/active_sleep.png); background-size: cover;float:left;}";
    styleText += "#" + containerElement.id + " #drink.active {background-image:url(img/active_drink.png); background-size: cover;float:left;}";

希望这会有所帮助