0

次の場合を除き、常にメインの div の背後にこの非表示の div を配置しようとしています。

マウスが非表示の div に入ると、左にアニメーション化され、次に右に戻り、メインの div の上に表示されます

次に、マウスが非表示のdivを離れると、左にアニメーション化し、右に戻ってメインのdivの後ろに移動します。

私はjsとjQueryに慣れていないので、次のようなことを試しました:

<div class="mainDiv">
    content of main div

    <div class="hiddenDiv">
    content of hidden div
    </div>

    rest of content of main div
</div>

<script>
jQuery(document).ready(function() {
    jQuery(".hiddenDiv")css('z-index',"-10");
    //tell hiddenDiv to be hidden, this seem to block everything for some reason

    jQuery(".hiddenDiv").mouseenter(function () {
        jQuery(".hiddenDiv").animate({"left": "-=50px"}, "fast").css('z-index',"10"); 
        //when mouse enters, hiddenDiv shows up
    }),
    jQuery(".hiddenDiv").mouseleave(function () {
        jQuery(".hiddenDiv").animate({"left": "+=50px"}, "slow").css('z-index',"-10"); 
        //when mouse leaves, it's hidden again.
    });
});
</script>

しかし、最初から非表示の div に -10 の z-index を指定すると、何も機能しないことがわかります。これを達成するために正しい方向に私を向けることができますか?

4

3 に答える 3

1
 .css('z-index',"10");

次のように書く必要があります

 .css('zIndex',"10");

ドットがないため、2番目のステートメントが間違っています

jQuery(".hiddenDiv").css('zIndex',"-10");

代わりにそのように書いてみてください

jQuery(document).ready(function() {
    var hdiv = jQuery(".hiddenDiv");  /* cache a reference for a matter of performance */

    hdiv.css('zIndex', "-10")
        .mouseenter(function () {
            hdiv.animate({"left": "-=50px"}, "fast")
                .css('zIndex', "10"); 
        })
        .mouseleave(function () {
            hdiv.animate({"left": "+=50px"}, "slow")
                .css('zIndex', "-10"); 
        });
});
于 2012-01-11T17:05:39.003 に答える
1

あなたが抱えている最初の最初の問題は、hiddendivをロールオーバーできず、-10zインデックスで隠されていることです。セレクターに関する限り、そこにはありません。

最初のセレクターを次のように変更します。

jQuery(".mainDiv").mouseenter(function () {
    //etc etc

これがないと、hiddenDivをセレクターとして使用できません。

于 2012-01-11T17:06:25.663 に答える
0

これを見てください。

jQuery(document).ready(function() {

  // Hide all .hiddenDiv
  //jQuery(".hiddenDiv").css('z-index',"-10");
  jQuery(".hiddenDiv").hide(); // Maybe this would be enough to hide the elements?

  // Bind events to all .mainDiv
  jQuery('.mainDiv')
    .mouseenter(function () {
      jQuery(this).find('.hiddenDiv') // Find all .hiddenDiv in the current .mainDiv
        //.css('zIndex', "10")
        .show()
        .animate({"left": "-=50px"}, "fast");
    })
    .mouseleave(function () {
      jQuery(this).find('.hiddenDiv')
        .animate({"left": "+=50px"}, "slow", function() {
          // This is a callback function that executes when the animation has finished.
          //jQuery(this).css('zIndex', "-10");
          jQuery(this).hide();
        });
    });
});
于 2012-01-11T17:07:51.500 に答える