首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用jquery在网站上弹出页脚,第一次点击?

用jquery在网站上弹出页脚,第一次点击?
EN

Stack Overflow用户
提问于 2013-08-23 02:48:33
回答 1查看 511关注 0票数 0

我有问题弹出页脚与jquery。我的代码是work,但唯一的问题是它不能在第一次点击按钮时工作,而是通过第二次点击按钮来工作?有什么想法吗?下面是我的代码:

代码语言:javascript
运行
复制
<script type="text/javascript">

    jQuery(function($) {

        var open = false;

        $('#footerSlideButton').click(function () {

            if(open === false) {

                $('#footerSlideContainer').animate({ height: '0' });

                $(this).css('backgroundPosition', 'top left');

                open = true;

            } else {

                $('#footerSlideContainer').animate({ height: '150px' });

                $(this).css('backgroundPosition', 'bottom left');

                open = false;

            }

        });        

    });

</script>
EN

回答 1

Stack Overflow用户

发布于 2013-08-23 02:57:39

我会将其概括为:

代码语言:javascript
运行
复制
(function($){
    window.opened=false;
    $('#footerSlideButton').on('click', function () {
        $('#footerSlideContainer').animate({ height : opened ? '0' : '150px'});
        $(this).css('backgroundPosition', opened ? 'top left' : 'bottom left');
        opened = !opened;
    });
})(jQuery);

您还可以将打开状态存储在元素数据中:

代码语言:javascript
运行
复制
(function($){
    $('#footerSlideButton').on('click', function () {
        $('#footerSlideContainer').animate({ height : $(this).data('open') ? '0' : '150px'});
        $(this).css('backgroundPosition', $(this).data('open') ? 'top left' : 'bottom left');
        $(this).data('open', !$(this).data('open'));
    });
})(jQuery);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18388560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档