jQuery显示隐藏内容,然后自动滚动到内容的中间

标签 jquery scroll show-hide

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

/*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('.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);
});

关于jQuery显示隐藏内容,然后自动滚动到内容的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6969467/

相关文章:

php - $.ajax 在 json encode($array) 之后从 php 获取一个对象,但是如何在 jQuery 中获取键和值?

jquery - 更改 css 属性的滚动功能仅适用于第二次

javascript - 使用 jQuery 显示/隐藏复选框

javascript - 切换 .hta 中的表 <td>

html - 用 HTML 隐藏 <div>,用 CSS 显示

javascript - 使用 jQuery、JSON 和 PHP 从目录中获取文件大小和文件日期

javascript - javascript数组中的引用错误

javascript - 在 PhantomJS 中,为什么 jQuery 调用会导致脚本挂起?

android - QuickReturnListView 更快地计算滚动 Y 位置

python - 重新启动游戏(重置滚动屏幕)