jQuery手风琴| 在页面加载和活动状态混乱中打开第一个元素

奥巴特

我正在使用下面的Javascript为手风琴制作动画(这是在此处解释的手风琴的略微修改形式:http : //tympanus.net/codrops/2010/04/26/elegant-accordion-with-jquery-and-css3/

现在,我想在页面加载时打开第一个元素,因此我想我只是通过Javascript为其提供了一些额外的类(并.active通过CSS定义了该状态)以将其打开。

这行得通,但是,如果我将鼠标悬停在除了first-element带有所说.active类的任何类之外,第一个元素将保持其状态,并保持打开状态,直到我至少将鼠标悬停在它之上一次。

所以,我想要的是:我的手风琴的第一个元素是打开的,如果用户将鼠标悬停在不是第一个元素上的任何元素上,它就会折叠我想我需要在hover函数中添加一行以使类脱离第一个元素或使新元素处于活动状态,但是我不知道该怎么做并继续破坏事情。

<script type="text/javascript">
        jQuery(function() {

            activeItem = jQuery("#accordion li:first");
            jQuery(activeItem).addClass('active');

            jQuery('#accordion > li, #accordion > li.heading').hover(
                function () {
                    var jQuerythis = jQuery(this);
                    jQuerythis.stop().animate({'height':'280px'},500);
                    jQuery('.heading',jQuerythis).stop(true,true).fadeOut();
                    jQuery('.bgDescription',jQuerythis).stop(true,true).slideDown(500);
                    jQuery('.description',jQuerythis).stop(true,true).fadeIn();
                },

                function () {
                    var jQuerythis = jQuery(this);
                    jQuerythis.stop().animate({'height':'40px'},1000);
                    jQuery('.heading',jQuerythis).stop(true,true).fadeIn();
                    jQuery('.description',jQuerythis).stop(true,true).fadeOut(500);
                    jQuery('.bgDescription',jQuerythis).stop(true,true).slideUp(700);
                }
            );
        });
    </script>
西蒙·阿考克

似乎正在发生这种情况,因为每个手风琴项目都有自己的悬停事件,可以处理自己的动画。您可以稍微重构代码,以使其更易于理解和重用:

        var activeItem = jQuery("#accordion li:first");

        jQuery('#accordion > li, #accordion > li.heading').hover(
            function () { hoverMe(jQuery(this)); },
            function () { unhoverMe(jQuery(this)); }
        );

        //This gets called when cursor hovers over any accordion item
        var hoverMe = function(jQuerythis) {

            //If the first item is still active
            if (activeItem) {
                contract(activeItem); //...Shrink it!
                activeItem = false;
            }

            //Expand the accordion item
            expand(jQuerythis);
        };

        //This gets called when cursor moves out of accordion item
        var unhoverMe = function(jQuerythis) {
                contract(jQuerythis);
        };

        //I have moved the hover animation out into a separate function, so we can call it on page load
        var expand = function(jQuerythis) {
                jQuerythis.stop().animate({'height':'280px'},500);
                jQuery('.heading',jQuerythis).stop(true,true).fadeOut();
                jQuery('.bgDescription',jQuerythis).stop(true,true).slideDown(500);
                jQuery('.description',jQuerythis).stop(true,true).fadeIn();
        };

        //I have moved the unhover animation out into a separate function, so we can contract the first active item from hoverMe()
        var contract = function() {
                jQuerythis.stop().animate({'height':'40px'},1000);
                jQuery('.heading',jQuerythis).stop(true,true).fadeIn();
                jQuery('.description',jQuerythis).stop(true,true).fadeOut(500);
                jQuery('.bgDescription',jQuerythis).stop(true,true).slideUp(700);
        };

        //Now expand the first item
        expand(activeItem);

我整理了一个简化的版本来说明逻辑。请让我知道你过得如何。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery手风琴添加活动

来自分类Dev

jQuery从链接打开手风琴

来自分类Dev

jQuery手风琴打开/关闭

来自分类Dev

JQuery 手风琴默认打开

来自分类Dev

jQuery手风琴,在手风琴完全加载后运行功能

来自分类Dev

排序一个手风琴表(jQuery)

来自分类Dev

jQuery手风琴菜单活动打开

来自分类Dev

jQuery手风琴菜单活动打开

来自分类Dev

jQuery手风琴-通过Ajax加载保持状态

来自分类Dev

jQuery手风琴打开和关闭

来自分类Dev

在警告中打开jQuery手风琴面板

来自分类Dev

jQuery手风琴菜单中某些元素滞后

来自分类Dev

单击手风琴中具有元素id的链接时,打开JQuery手风琴

来自分类Dev

单击手风琴中具有元素id的链接时,打开JQuery手风琴

来自分类Dev

jQuery UI手风琴滚动到活动部分

来自分类Dev

如何制作活动的 jQuery 手风琴菜单?

来自分类Dev

关闭打开子菜单-jQuery手风琴

来自分类Dev

通过URL哈希打开jQuery手风琴

来自分类Dev

根据ID打开JQuery手风琴

来自分类Dev

来自加载了ajax的div的jquery ui手风琴

来自分类Dev

在页面加载时按手风琴打开第一个项目

来自分类Dev

手风琴复选框jquery和html

来自分类Dev

面板中的jQuery手风琴对齐

来自分类Dev

jQuery手风琴中的动态内容(Textarea)

来自分类Dev

如何在jQuery中制作手风琴

来自分类Dev

JQuery手风琴面板中的2列

来自分类Dev

jQuery手风琴在不同的浏览器中

来自分类Dev

jQuery 中的手风琴引导程序

来自分类Dev

无法在 jQuery 中刷新手风琴