【问题标题】:jQuery show hidden content, then auto scroll to the middle of the contentjQuery 显示隐藏的内容,然后自动滚动到内容的中间
【发布时间】:2011-08-06 22:44:40
【问题描述】:

我设置了一个按钮,单击该按钮会展开页面。代码如下:

/*Source:http://rpardz.com/blog/show-hide-content-jquery-tutorial*/

jQuery('.open-content').hide().before('<div class="container_12"><a href="#" id="toggle-content" class="button"><div id="expand-button" ></div></a></div><div id="toggle-top" style="width:100%"></div>');
jQuery('a#toggle-content').click(function() {
    jQuery('.open-content').slideToggle(1000);
    return false;
});

如您所见,它运行良好:隐藏:http://cl.ly/101v0N0W1z2D2e0x3a0j 扩展:http://cl.ly/1Z2Q1d3Y2z2X3G1j1v2G

注意(参见图片侧面的滚动条)页面底部如何展开以显示更多内容;我想不通的是如何使页面在页面完成扩展后自动滚动到现在可见内容的底部..

我使用这个标准脚本来平滑滚动到页面上的位置..

/*Source: http://goo.gl/DaRfF */
jQuery(document).ready(function($) {

$(".scroll").click(function(event){     
    event.preventDefault();
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});

});

但我看不到如何集成它,以便它在完成扩展后自动滚动到内容的底部。非常感谢所有帮助,谢谢!

【问题讨论】:

    标签: jquery scroll show-hide


    【解决方案1】:

    这样的事情怎么样?

    jQuery('.open-content').slideToggle(1000, function(){
       var offset = jQuery('.open-content').offset();
       var y = offset.top + jQuery('.open-content').height();
       var wheight = $(window).height()
       var scroll = y - wheight;
       $(document).animate({scrollTop:scroll}, 500);
    });
    

    【讨论】:

    • 太棒了!这非常接近;我在上面遇到的唯一问题是,单击它会将我带到页面顶部,然后再向下滚动到新的扩展页面区域。如何防止它在向下滚动以查看扩展区域之前链接到顶部?可能是我实现不正确:gist.github.com/1129878
    • 我还注意到,如果我完全缩小页面的当前位置相当于页面的顶部,单击展开按钮会将整个页面向左移动大约十个像素。使效果看起来有点生涩。
    • 我将选择器更改为document(现在应该会更好)。另外,我建议您使用scrollTo plugin,代码将是这样的:$(document).scrollTo({top: scroll}, 'medium');
    • 很棒的提示。它链接到顶部的问题是流氓 href="#" x-D。很棒的帮助,CM;谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多