jQuery 在所有浏览器中的 SlideUp() 上闪烁...附有示例页面

2024-04-20

我知道这个问题已经讨论过好几次了,但我有一个有点不同的问题。当调用 .slideUp() 并且浏览器风已经向下滚动以查看所有底部内容时,当 div 滑动关闭时它会闪烁多次。

示例页面如下,只需单击它所说的位置,然后滑动到底部并关闭上面的 div 之一。

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>This is the title</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" type="text/javascript"></script>
        <script  type="text/javascript">
        $(document).ready(function() {

                    $('div.Accordion > div.Content').click(function() {
                        $(this).prev('div.collapsePanelHeader').slideDown(1000);
                        $(this).slideUp(1000);
                    });

                    $('div.Accordion > div.collapsePanelHeader').click(function() {
                        $(this).slideToggle(1000);
                        $(this).next('div.Content').slideToggle(1000);
                    });

                    $('div.Accordion > div.collapsePanelHeader2').click(function() {
                        $(this).toggleClass('accordionHeaderSelected','accordionHeader');
                        $(this).next('div.Content2').slideToggle(1000);
                    });

                });
        </script>
        <style type="text/css">
            .Accordion
            {
                font-size: .9em;
                background-color: #ebebeb;
                border: solid 2px #ccc;
                padding: 5px 10px;
                width: 500px;
            }
            p
            {
                font-size: 1em;
            }
            .collapsePanelHeader
            {
            }
            .HeaderContent
            {
                background-color: #ebebeb;
            }
            .Content
            {
                background-color: #fff;
                border: solid 1px #ccc;
                padding: 10px;
            }
            .accordionHeaderSelected
            {
                border: solid 1px #ccc;
                background-color: #EBEBEB;
                margin-bottom: 10px;
            }
            .accordionHeader
            {
                border: none;
                background-color: #EBEBEB;
                margin-bottom: 10px;
                text-decoration: none;
            }
            .collapsePanelHeader2
            {
            }
            .HeaderContent2
            {
                background-color: #ebebeb;
            }
            .Content2
            {
                background-color: #ebebeb;
                padding-left: 30px;
            }
            .gvCSections
            {
                padding-top: -10px;
            }
            .gvCSections tr td
            {
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <div class="Accordion">
            <h2>Title Goes Here</h2>
            <hr style="border-top: none; border-bottom: 1px solid #999999;" />
            <div class="collapsePanelHeader">
            Click here Click here Click here Click here Click here Click here Click here Click here Click here Click here Click here Click here
                ... <span style="font-size: x-small; font-weight: bold; cursor: pointer; text-decoration: underline;">click for more</span>
            <br /><br /><br />
            </div>
            <div class="Content" style="display: none;">
            <div class="collapsePanelHeader3 accordionHeaderSelected">
                CourseDescription
            </div>
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    <br />
                    <strong>For more information, contact</strong>:
                    <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />

                    <strong>To enroll</strong>:
                    <br />
                    Click on a link below and complete the registration form.
            </div>
            <div class="collapsePanelHeader2 accordionHeader">
                <b>Click on this line</b>
            </div>
            <div class="Content2 accordionContent" style="display: none;">
                    <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
            </div>

            <h2>Title Goes Here</h2>
            <hr style="border-top: none; border-bottom: 1px solid #999999;" />
            <div class="collapsePanelHeader">
                Click here Click here Click here Click here Click here Click here Click here Click here Click here Click here Click here
                ... <span style="font-size: x-small; font-weight: bold; cursor: pointer; text-decoration: underline;">click for more</span>
                <br /><br /><br />
            </div>
            <div class="Content" style="display: none;">
            <div class="collapsePanelHeader3 accordionHeaderSelected">
                CourseDescription
            </div>
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    <br />
                    <strong>For more information, contact</strong>:
                    <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />

                    <strong>To enroll</strong>:
                    <br />
                    Click on a link below and complete the registration form.
            </div>
            <div class="collapsePanelHeader2 accordionHeader">
                <b>Click on this line</b>
            </div>
            <div class="Content2 accordionContent" style="display: none;">
                    <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
            </div>

            <h2>Title Goes Here</h2>
            <hr style="border-top: none; border-bottom: 1px solid #999999;" />
            <div class="collapsePanelHeader">
            Click here Click here Click here Click here Click here Click here Click here Click here Click here Click here
                ... <span style="font-size: x-small; font-weight: bold; cursor: pointer; text-decoration: underline;">click for more</span>
                <br /><br /><br />
            </div>
            <div class="Content" style="display: none;">
            <div class="collapsePanelHeader3 accordionHeaderSelected">
                CourseDescription
            </div>
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    <br />
                    <strong>For more information, contact</strong>:
                    <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />

                    <strong>To enroll</strong>:
                    <br />
                    Click on a link below and complete the registration form.
            </div>
            <div class="collapsePanelHeader2 accordionHeader">
                <b>Click on this line</b>
            </div>
            <div class="Content2 accordionContent" style="display: none;">
                    <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
                    Content Content                <br />
            </div>
        </div>
    </body>
</html>

一个简单的解决方法是在关闭可折叠元素之前检查 body 元素的计算高度,然后将 body 的计算高度设置为其 CSS height 属性。

$('div.Accordion > div.collapsePanelHeader').click(function() {

    var body = $('body');
    body.css('height', 'auto');
    body.css('height', body.height());

    $(this).slideToggle(1000);
    $(this).next('div.Content').slideToggle(1000);
 });

这会强制 body 元素保持其高度,即使关闭通常会将 body 重置/重绘到其先前默认高度的高面板也是如此。

另请注意,在检查计算的高度之前,CSS 高度会重置为auto,否则 jQuery 会绕过计算样式并使用上一次传递期间设置的值。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 在所有浏览器中的 SlideUp() 上闪烁...附有示例页面 的相关文章

  • 仅在 Chrome 上我收到此错误:Uncaught TypeError: Illegal constructor [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 当我在 Chrome 上加载 jQuery 时 我会收到此错误 Uncaught TypeError Illegal constr
  • fadeOut() 和slideUp() 同时进行?

    我已经发现jQuery 淡出然后滑动 https stackoverflow com questions 734554 jquery fadeout then slideup这很好 但不是那个 我怎么能够fadeOut and slideU
  • jQuery 中什么函数相当于 .SelectMany()?

    让我解释一下 我们知道 jQuery 中的映射函数充当 Select 如 LINQ 中 tr map function return this children first returns 20 tds 现在的问题是我们如何在 jQuery
  • 选择 asp.net CheckBoxList 中的所有项目

    ASP NET 和 C 我想要一个带有 全选 项目的复选框列表 当这个特定项目是 已选择 所有其他都将被选择 也 当选择被删除时 这个项目 也将来自所有人 其他物品 选中 取消选中 任何其他项目只会有一个 对特定项目的影响 无论选择状态如何
  • Angularjs : $locationProvider.hashPrefix("!") ;

    我想将网址显示为 www test com 因为我正在使用 locationProvider hashPrefix 但它显示网址为 www test com 我想 哈希之前而不是哈希之后 Thanks var app angular mod
  • 使用jquery调用AS3外部接口

    我正在使用ExternalInterface 调用嵌入在html 页面中的Flash 应用程序 以下代码工作正常 我正在使用按钮进行测试 document ready function button click function var a
  • 选择单选按钮时隐藏/显示 3 个文本框

    我有 2 个单选按钮 选择一个文本框时 我想显示 3 个文本框 并在选择其他文本框时隐藏它 这是代码 这是我的 2 个单选按钮
  • 意外的令牌:尝试解析 JSON 字符串时

    我正在尝试解析这个 JSON 字符串 RESULTS name Thessaloniki GR type Sailing l sailing weather beach Porto 20Carras 20Marina 45904 name
  • javascript setInterval 不适用于对象

    所以 我正在尝试创建一个 javascript 对象 并使用 setInterval 方法 这似乎不起作用 如果我删除引号 则该方法将运行一次 有什么想法吗 另外 我正在使用 Jquery Yacoby 和
  • 仅动态包含 javascript 文件一次

    我正在编写一个 javascript 函数 该函数用于包含外部 JS 文件 但仅一次 我需要这样一个函数的原因是 当通过 AJAX 加载某些内容时会调用它 并且我需要对该内容运行特定于页面的代码 不 只需使用 live不会覆盖它 这是我的尝
  • 如何防止 iframe 中的链接在新选项卡中打开

    我为我制作的基于网络的操作系统制作了一个基于网络的小型网络浏览器 我注意到在某些网站中 它们有喜欢在新选项卡中打开的链接 有没有办法可以防止这种情况并在 iframe 中打开链接 这是我的整个浏览器的代码 以防万一
  • IE6刷新时内存泄漏?

    我每隔几秒钟重新加载一个页面以从服务器获取更新 该页面可以打开并且这种情况可能永远发生 我猜测 特别是因为该浏览器位于未关闭的远程服务器上 我这样做 setTimeout function location href location hr
  • 将删除和箭头键添加到正则表达式中

    我正在执行日期验证 现在我正在执行该用户只能输入numbers and backspace所以现在我想在正则表达式中添加 2 个键 我想添加delete and arrow keys那么我应该在正则表达式中做什么改变 这是我的代码
  • 仅单击 div 内部

    我正在为一个小网站制作教程 我只想让教程气泡可点击 因此 当我们尝试单击气泡之外的某些内容时 什么也不会发生 换句话说 我希望我的 html 不可点击 而 tutorial bubble 可点击 尝试这个 jQuery function h
  • 确定元素是在页面折叠上方还是下方

    我有一些页面有多个输入框 用户可以在其中输入文本 在单击 下一步 按钮之前 需要填写其中一些内容 我弹出验证错误供用户查看 但是如果问题不在页面上 我希望页面滚动到它 而不是他们必须搜索丢失 错误的字段 我有一个滚动到位 但我无法确定要滚动
  • 如何更改自动完成中的结果过滤器?

    我不想进行字面匹配 而是想通过正则表达式选择结果 我可以覆盖自动完成的默认行为来完成此任务还是需要替代结构 有一个内置的方法可以做到这一点 只需提供一个函数source http jqueryui com demos autocomplet
  • Jquery 在 DIV 中进行多重加载

    这是我的代码 right load textes html nicolas right load textes html antoine 问题是内容divantoine覆盖了右边div nicolas加载的内容div div right l
  • 禁用特定 div 上的 Tab 键

    我有以下结构 div div Some content div div Some content div div 我想 禁用 div2 上的 tab 键 我的意思是按下 tab 键时 div2 的元素不会获得焦点 有没有简单的方法可以使用
  • 如何限制下拉列表更改而不禁用它

    如何在不禁用下拉列表的情况下限制下拉列表选项更改 意味着我无法更改选项 并且下拉列表不应该是只读的 我的问题是我的服务器没有读取禁用的元素 这是我的出价 http jsfiddle net m3v7y jQuery var lastSel
  • 在 PHP 中模拟 jQuery.ajax 请求

    我必须在 PHP 中模拟 AJAX 请求 就像在 jQuery 中一样 我当前的代码在这里 原始 AJAX 调用 不得修改 ajax type POST url someFile php data data success function

随机推荐