4

add newボタンをクリックすると、jQueryはにnewdragrable DIVsを挿入しますCONTAINER。これをクリックすると、値DIVsを変更したいz-index。ただし、jQueryはz-index値を数値として取得できません。それはちょうど'auto'...真のz-index値を表示するための解決策はありますincrease it by 1か?

jsFiddleの例-http : //jsfiddle.net/ynternet/84nVQ/10/

HTML

<div id="add" style="background:yellow; width:100px;"> add new </div>
<div id="container"> </div>

jQuery

function handler() {
    if ($(this).find("#menu").length) {
        return;
    }
    var currentIndex = $(this).css('z-index');
    alert(currentIndex);
    var newIndex = currentIndex + 1;
    alert(newIndex);
    $(this).css('z-index', newIndex);
}
$("#add").on({
    click: function(e) {
        var timestamp = Date.now();
        var posx = Math.floor(Math.random() * 400);
        var posy = Math.floor(Math.random() * 400);
        $('#container').append(function() {
            return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posy + 'px; ">Click me, drag a change z-index</div>').click(handler).draggable({
                containment: "#container",
                scroll: false,
                cursor: 'lock'
        });
    });
    }
});

CSS

#container {
    width:500px;
    height:500px;
    background: palegoldenrod;
    position: relative;
    top:20px;
    left: 100px;
    padding: 0px;
}
.add_to_this {
    padding:5px;
    background:yellowgreen;
    position: absolute;
    display:inline-block;
    width:200px;
    height:50px;
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select: none;
}
4

1 に答える 1

5

コードの2つの問題:

  • z-indexあなたはそれを設定しなかったのであなたは最初に本当のものを持っていないので、あなたはそれを持っています'auto'
  • z-index解析しないため、文字列として操作します

するべきこと :

  • cssに追加しますz-indexz-index:100;
  • を解析しz-indexます:var currentIndex = parseInt($(this).css('z-index'), 10);

デモンストレーション(迷惑だったのでアラートなし)

于 2012-10-09T07:11:29.760 に答える