-2

我有一个 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),它具有自动边距左和边距右。

出于某种原因,我无法让我的 div 居中,并试图找出原因。

谢谢你的帮助

4

2 回答 2

2

好的,有你的问题。使用这个 CSS:

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

截屏:

于 2012-10-15T06:58:26.517 回答
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;}";

希望这会有所帮助

于 2012-10-15T06:57:25.977 回答