jQuery问题的鼠标悬停效果

发布于 2024-08-15 15:21:59 字数 471 浏览 6 评论 0原文

假设我有 4 个 imagediv(imgdiv1、imgdiv2、imgdiv3、imgdiv4)和 4 个 contentdiv(condiv1 condiv2 condiv3、condiv4)和 1 个主要内容 div(maincon) 所有内容div(主要内容div除外)都需要保持“隐藏”或不可见。当我将鼠标悬停在 imagediv 上时,每个 con 都会显示淡入效果。所有 contentdiv 都在同一个位置,而 imagediv 是一种菜单。

示例:如果我将鼠标移到 imgdiv1 上,condiv1 将出现并具有平滑的淡入淡出效果。当我的鼠标移出 imgdiv1 时,condiv1 应该淡出至不可见状态并具有淡出效果,imgdiv2、imgdiv3 和 imgdiv4 也是如此。 maincon div 将始终存在,并且当鼠标悬停在 imagediv 上触发时,condiv 应该刚好越过 maincon。

我怎样才能使用 jQuery 实现这一点? 最好的方法是什么?

Let's say I have 4 imagedivs (imgdiv1, imgdiv2, imgdiv3, imgdiv4) and 4 contentdivs (condiv1 condiv2 condiv3, condiv4)and 1 main content div (maincon)
all contentdivs (except the main content div) need to stay "hidden" or invisible. Each con will show up with a fadein effect when I do a mouseover on an imagediv. All contentdivs are in the same place while the imagedivs are a kind of menu.

Example: if I go with my mouse over imgdiv1, condiv1 will appear with a smooth fade in effect. When my mouse goes out of the imgdiv1, condiv1 should fade out to invisibility with a fadeout effect, same goes for imgdiv2, imgdiv3 and imgdiv4. the maincon div will always be there and the condivs should just go over the maincon when triggered with a mousover on imagedivs.

How can I achieve this using jQuery?
What is the best way?

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

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

发布评论

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

评论(3

∞觅青森が 2024-08-22 15:21:59

您可以使用悬停功能
$('#imgdiv1').hover(function(){ /*淡入代码*/},function(){/*淡出代码*/});

或者您可以查看 JqueryUI并使用选项卡小部件。

You could use the hover function
$('#imgdiv1').hover(function(){ /*fade in code*/},function(){/*fade out code*/});

alternatively you could look into JqueryUI and use the Tabs widget.

意中人 2024-08-22 15:21:59

您看过 hover 吗?它结合了鼠标输入/鼠标输出事件处理程序,最大限度地减少了您需要编写的代码。

Have you looked at hover ? it combines mouse in / mouse out event handers, minimizing the code you need to write.

烟花肆意 2024-08-22 15:21:59

您可以使用hover() 在两个函数之间切换,一个在mouseenter 上,另一个在鼠标离开imgdiv 上。为了实用性,给你的imgdiv一个额外的类:“imgdiv”。这允许您创建一次行为并通过each()将其附加到所有imgdiv元素;环形。

另外,为所有 contentdiv 元素指定一个“contentdiv”类,以便您可以通过一次调用隐藏它们。

$(document).ready(function(){
   // hide all that should be hidden
   $('div.contentdiv').hide();
    var divToShow= '';
    var strlength = 10;
    $('div.imgdiv').each(function(){
            $(this).hover(function(){
                //find which contentdiv to show
                var thisId= $(this).attr('id');
                strlength = thisId.length;
                divToShow = 'condiv'+thisId.charAt(strlength-1);
                $(divtoShow).stop().fadeIn('slow');
                }
               ,
                function(){
                 // when mouse leaves imgdiv...
                 $(divtoShow).stop().fadeOut('slow');
               }
            );
      });

});

You would use hover() to toggle between two functions, one on mouseenter, the other on mouse leaving the imgdiv. For the sake of practicability, give an additional class to your imgdiv: "imgdiv". This allows you to create the behaviour once and attach it to all imgdiv elements , via an each(); loop.

Also, give a class "contentdiv" to all contentdiv elements so you can hide them via one single call.

$(document).ready(function(){
   // hide all that should be hidden
   $('div.contentdiv').hide();
    var divToShow= '';
    var strlength = 10;
    $('div.imgdiv').each(function(){
            $(this).hover(function(){
                //find which contentdiv to show
                var thisId= $(this).attr('id');
                strlength = thisId.length;
                divToShow = 'condiv'+thisId.charAt(strlength-1);
                $(divtoShow).stop().fadeIn('slow');
                }
               ,
                function(){
                 // when mouse leaves imgdiv...
                 $(divtoShow).stop().fadeOut('slow');
               }
            );
      });

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