【问题标题】:Scroll the page when the footer dynamically grows当页脚动态增长时滚动页面
【发布时间】:2011-10-06 13:19:04
【问题描述】:

我的页脚中有链接,单击它们时会显示隐藏的内容。

所以它们只是隐藏的 <ul> 元素,这些元素使用 jQuery slideToggle(); 显示

我遇到的问题是,当我这样做时,我必须在物理上滚动页面。如何强制窗口自行滚动?

我尝试使用 scrollTo 但这似乎不起作用。

是否可能是页脚所在的 div 导致了这种情况?

<div id="navigation">
            <ul>
                <li>
                    <p onclick="showdivs('news');">
                        + News</p>
                    <ul id="news" class="subitems" style="width: 94px;">
                    <li>Item #1</li>
                    <li>Item #2</li>
                   </ul>
                </li>
            </ul>
      </div>

Javascript - 我知道我可以通过 .click 事件做到这一点:

   function showdivs(id) {
        jQuery('.subitems').hide();
        jQuery('#' + id).slideToggle('slow');
    }

CSS:

#navigation
{
    width: 100%;
    height: 30px;
    background: #f2f2f2;
}
#navigation ul
{
    text-decoration: none;
    display: block;
    padding: 5px 8px;
    background-color: #e5e5e5;
    font-family: arial;
    font-size: 10px;
    font-weight: bold;
    color: #808080;
    width: auto;
    cursor: pointer;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    background: #f2f2f2;
}
#navigation ul li
{
    display: inline;
    height: 30px;
    float: left;
    list-style: none;
    margin-left: 15px;
    position: relative;
    background: #f2f2f2;
}
#navigation li p
{
    background-color: #f2f2f2;
    color: #009;
    text-decoration: none;
}
#navigation li p:hover
{
    text-decoration: underline;
}
#navigation li a
{
    text-decoration: none;
    background: #f2f2f2;
}
#navigation li a:hover
{
    text-decoration: underline;
}
#navigation li ul
{
    margin: 0px;
    padding: 0px;
    display: none;
    position: absolute;
    left: 0px;
    top: 25px;
    background-color: #f2f2f2;
}

#navigation li li
{
    list-style: none;
    display: list-item;
}
#navigation li li a
{
    text-align: left;
    font-size: 9px;
    max-width: 120px;
    background-color: #f2f2f2;
    background-repeat: no-repeat;
    background-position: 50% 100%;
    background-image: url(../images/line.png);
}
#navigation li li a:hover
{
    background-color: #f2f2f2;
    color: #000;
    -moz-border-radius: 5px;
}

【问题讨论】:

  • 你能把你的代码贴出来吗?
  • 已发布代码(javascript、html 和 CSS)。

标签: jquery css scroll footer


【解决方案1】:

您需要将 jQuery 的 .scrollTop(...) 函数与 $(document) 一起使用,将其传递到已切换元素的顶部:

jQuery('#' + id).slideToggle('slow', function () {
    $(document).scrollTop($(this).offset().top);
});

使用.slideToggle() 的callback 参数确保页面在动画完成后向下滚动(这可能是您最初的问题)。

这是一个例子:http://jsfiddle.net/JU3xj/


如果你想为滚动设置动画,你也可以这样做:

$("html, body").animate({
    'scrollTop': $(this).offset().top
}, 500); // Adjust as needed.

您将使用它来代替 .scrollTop() 调用:

function showdivs(id) {
    jQuery('.subitems').hide();
    jQuery('#' + id).slideToggle('slow', function () {
        $("html, body").animate({
            'scrollTop': $(this).offset().top
        }, 500);
    });
}

示例:http://jsfiddle.net/uktmR/

【讨论】:

  • 这似乎可行,但有点刺耳/生涩 - 有没有办法让它平滑?
  • @Jack:当然,您可以为scrollTop 属性设置动画。
  • 当我尝试你的动画功能时,我得到 jQuery(this).offset is null
  • @Jack:我需要更多信息——您在示例中是否遇到此错误?它发生在哪些浏览器中?
  • FireFox - 页面加载时的萤火虫 - 确切错误:jQuery(this).offset() 为空 - 我将此代码放在 showDiv 函数的正上方,还是应该放在其他地方? jQuery("html, body").animate({ 'scrollTop': jQuery(this).offset().top }, 500); // 根据需要调整。
【解决方案2】:

navigation.scrollTop = document.body.scrollHeight 是你需要的,对吧?

【讨论】:

【解决方案3】:

试试这个:http://jsfiddle.net/AlienWebguy/JU3xj/3/

首先我们创建一个叫scrollTo的插件:

jQuery.fn.extend({
    scrollTo : function(speed, easing) {
        return this.each(function() {
            var targetOffset = $(this).offset().top;
            $('html,body').animate({scrollTop: targetOffset}, speed, easing);
        });
    }
});

然后,我们只需将它附加到我们想要滚动到的任何元素:

$('.return').click(function(){
    $('#navigation').scrollTo(300,'linear');
});

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2017-07-31
    • 2013-01-24
    相关资源
    最近更新 更多