【问题标题】:Animate Scroll not happening动画滚动没有发生
【发布时间】:2014-06-28 15:18:58
【问题描述】:

已经有 100 个问题,但仍然无法找到我的 sn-p 失败原因的解决方案

这是我正在使用的 jQuery:

$("#readmore").click(function(event){
        event.preventDefault();
        var link = $(this);
        $("#readmore_div").slideToggle('slow',function(){
            if($(this).is(':visible')){ //if div is visible
                link.html('<b>Close</b>'),//Change text & scroll 
                $('html, body').animate({scrollTop : $("#readmore_div").offset().Top});
            }else{ //Revert back original text
                link.html('<b>Read More</b>');
            }
        });
    });

通过html() 的文本变化正常,但animate 没有将页面滚动到#readmore_div

谁能指出错误!

【问题讨论】:

  • 您也可以发布您的 html 吗?或者创建一个小提琴?

标签: jquery html scroll jquery-animate smooth-scrolling


【解决方案1】:

在您的代码中将 Top 替换为 top

试试这个:

$("#readmore").click(function(event){
        event.preventDefault();
        var link = $(this);
        $("#readmore_div").slideToggle('slow',function(){
            if($(this).is(':visible')){ //if div is visible
                link.html('<b>Close</b>'),//Change text & scroll 
                $('html, body').animate({scrollTop : $("#readmore_div").offset().top});
            }else{ //Revert back original text
                link.html('<b>Read More</b>');
            }
        });
    });

JSFiddle:

http://jsfiddle.net/QADbb/1

当前函数将按钮的文本更改为Read More,再次单击它会将其滚动到#readmore_div

【讨论】:

    【解决方案2】:

    试试这个:

    $('html,body').animate({
        scrollTop: $('#errors').offset().top},'slow');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多