English 中文(简体)
• 如何利用J Query 美元的功能开发窗口
原标题:
  • 时间:2009-05-07 04:13:57
  •  标签:

每当用户接近文件顶点时,我就试图将100px压缩。

当用户接近文件顶点时,我有执行职能,但信息学。 工作滞后。

我在检查之前和之后都发出警告,看看看它是否实际是线上而没有线,而且只有第一个警示才会停止。

alert("starting");
$.scrollTo({ top:  +=100px , left:  +=0px  }, 800);
alert("finished");

我知道,我有适当的接连页,因为Im在整个过程中使用许多其他杂交功能,而且他们都从事罚款工作。 我也试图从上面删除x子,似乎并没有改变。

最佳回答

If it s not working why don t you try using jQuery s scrollTop method?

$("#id").scrollTop($("#id").scrollTop() + 100);

If you re looking to scroll smoothly you could use basic javascript setTimeout/setInterval function to make it scroll in increments of 1px over a set length of time.

问题回答
$( html, body ).animate({scrollTop: $("#page").offset().top}, 2000);

jQuery now supports scrollTop as an animation variable.

$("#id").animate({"scrollTop": $("#id").scrollTop() + 100});

You no longer need to setTimeout/setInterval to scroll smoothly.

To get around the html vs body issue, I fixed this by not animating the css directly but rather calling window.scrollTo(); on each step:

$({myScrollTop:window.pageYOffset}).animate({myScrollTop:300}, {
  duration: 600,
  easing:  swing ,
  step: function(val) {
    window.scrollTo(0, val);
  }
});

This works nicely without any refresh gotchas as it s using cross-browser JavaScript.

Have a look at http://james.padolsey.com/javascript/fun-with-jquerys-animate/ for more information on what you can do with jQuery s animate function.

Looks like you ve got the syntax slightly wrong... I m assuming based on your code that you re trying to scroll down 100px in 800ms, if so then this works (using scrollTo 1.4.1):

$.scrollTo( +=100px , 800, { axis: y  });

Actually something like

function scrollTo(prop){
    $( html,body ).animate({scrollTop: $("#"+prop).offset().top +
 parseInt($("#"+prop).css( padding-top ),10) }, slow );
}

will work nicely and support padding. You can also support margins easily - for completion see below

function scrollTo(prop){
    $( html,body ).animate({scrollTop: $("#"+prop).offset().top 
+ parseInt($("#"+prop).css( padding-top ),10) 
+ parseInt($("#"+prop).css( margin-top ),10) +}, slow );
}




相关问题
热门标签