mouseenter z-index 10 动画到左边,然后 mouseleave z-index -10 动画回到右边?

发布于 2024-12-26 16:38:35 字数 1032 浏览 1 评论 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 时,没有任何效果。 有人能指出我实现这一目标的正确方向吗?

I am trying to have this hidden div behind the main div at all times, exept when:

mouse enters the hidden div, it should animate to the left, then back to the right and be on top of the main div

then when mouse leave the hidden div, it animated to the left, and back to the right to be behind the main div.

I'm not familiar with js and jQuery, so I tried something like:

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

But I see that when I give the hidden div a z-index of -10 at the begging, nothing works.
Could anyonde point me to the right direction to ahieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九厘米的零° 2025-01-02 16:38:35

您遇到的第一个问题是,您的hiddendiv 无法滚动,它隐藏在您的-10 z 索引中。这意味着就您的选择器而言,它不在那里。

我会将您的第一个选择器更改为:

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

如果没有这个,您将无法使用您的hiddenDiv作为选择器

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

梅窗月明清似水 2025-01-02 16:38:35
 .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"); 
        });
});
 .css('z-index',"10");

should be written as

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

and your second statement is wrong, since a dot is missing

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

so try write like so instead

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"); 
        });
});
寻梦旅人 2025-01-02 16:38:35

看看这个;

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();
        });
    });
});

Have a look at this one;

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();
        });
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文