jQuery scrollTop只工作一次



我使用这段代码打开一个div并向下滚动到它。它运行良好,但只有第一次使用它,即使在页面刷新时也不会再次运行。有人知道为什么会发生这种事吗?提前感谢!

这是网址(www.patrickor.ca)

$(document).ready(function() {
$("div.ftropen")
    .click(function(){
        $("div#connect").animate({height: "500px" }, "fast");                                              
        $("div#ftrconnect").fadeOut("fast");  //hide connectbtn
        $("div#ftrhide").fadeIn("fast");  //show hidebtn
        $("#connect").scrollTop($("#connect").scrollTop() + 500);
    return false;
});
$("div.ftrclose")
    .click(function(){
        $("div#connect").animate({height: "0px" }, "fast");                                            
        $("div#ftrhide").fadeOut("fast");  //hide hidebtn
        $("div#ftrconnect").fadeIn("fast");  //show connectbtn
    return false;
});
});

在Chrome 14中为我提供Uncaught TypeError: Cannot read property 'top' of null jquery.anchor.js:32jquery.anchor.js库似乎有问题。我已经把它注释掉了,并用替换了打开功能

$("div.ftropen").click(function(){
    $("div#connect").animate({height: "500px" }, "fast", function() {
        $("body").scrollTop( $("#connect").position().top);
    });                                    
    $("div#ftrconnect").fadeOut("fast");  //hide connectbtn
    $("div#ftrhide").fadeIn("fast");  //show hidebtn
    return false;
});

将主体滚动到<div id="connect"> 的顶部

我会看看我是否可以让锚动画插件工作…

实际上,Anchor Slider插件似乎完全干扰了您的<div>点击事件。<a>点击首先发生并消耗事件。我认为您需要决定使用Anchor Slider插件来为代码中的"滚动到"或jQuery animate()设置动画。

编辑:如果删除jquery.anchor.js脚本,请使用以下目标

<div id="connect" style="height:0px; display:block;"><a style="display:block;margin-top:500px;height:100px;" id="target" name="target">f</a></div>

和这个JavaScript:

$(document).ready(function() {
    $('div.ftropen').click(function(event){
        $('div#connect').animate({height: '500px' }, 'fast', function() {
            $('body').animate({scrollTop : $('#target').position().top + 500}, 700);
        });
        $('div#ftrconnect').fadeOut('fast');  //hide connectbtn
        $('div#ftrhide').fadeIn('fast');  //show hidebtn
        event.stopPropagation();
        return false;
    });
    $('div.ftrclose')
        .click(function(){
            $('div#connect').animate({height: '0px' }, 'fast');
            $('div#ftrhide').fadeOut('fast');  //hide hidebtn
            $('div#ftrconnect').fadeIn('fast');  //show connectbtn
        return false;
    });
});

其在CCD_ 8高度动画化完成时将滚动设置为CCD_。

第2版:添加了一个演示

而不是

    $("body").animate({

使用以下

    $("html:not(:animated),body:not(:animated)").animate({

Mozilla和Webkit浏览器对BODY和HTML的处理方式不同。因此,把两者都掩盖起来会有所帮助。同时引入:not(:animatized)声明。JS让动画滚动到某个位置,最终会变成。。。

    $('#DIV a[href^=#]').click(function(e) {
       e.preventDefault();
       var h = $(this).attr('href');
       $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $(h).offset().top }, 1200);
    });

最新更新