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 的 z-index 为 -10 时,没有任何效果。任何人都可以指出我实现这一目标的正确方向吗?

4

3 回答 3

1

First first problem you're having is, your hiddendiv can't be rolled over, its hidden with your -10 z index. Meaning as far as your selector is concerned its not there.

I would change your first selector to:

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

WIthout this you can not use your hiddenDiv as a selector

于 2012-01-11T17:06:25.663 回答
1
 .css('z-index',"10");

应该写成

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

你的第二个陈述是错误的,因为缺少一个点

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 回答
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 回答