使用CSS3动画滚动

Spi*_*iff 18 html javascript css html5 css3

有没有办法用CSS3动画滚动?

像这样的东西?

@-webkit-keyframes scrolltoview
{
    0%   { scrollTop: 0; }
    100% { scrollTop: 30%; }
}
Run Code Online (Sandbox Code Playgroud)

如何把css放在动画的起点?

men*_*nte 15

如此处所述,您可以使用margin-top技巧并动态更新滚动位置.你可以查看演示.代码很简单:

// Define the variables
var easing, e, pos;
$(function(){
  // Get the click event
  $("#go-top").on("click", function(){
    // Get the scroll pos
    pos= $(window).scrollTop();
    // Set the body top margin
    $("body").css({
      "margin-top": -pos+"px",
      "overflow-y": "scroll", // This property is posed for fix the blink of the window width change 
    });
    // Make the scroll handle on the position 0
    $(window).scrollTop(0);
    // Add the transition property to the body element
    $("body").css("transition", "all 1s ease");
    // Apply the scroll effects
    $("body").css("margin-top", "0");
    // Wait until the transition end
    $("body").on("webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd", function(){
      // Remove the transition property
      $("body").css("transition", "none");
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

@AndrewP 基于这个想法提供了一个很好的工作示例

  • @GerbenVanDijk我在这个问题上已经很晚了,但是我构建了我认为你要求的东西(一个函数来查找页面上锚点的每个链接并修改它们的点击以通过CSS3 + margin-top滚动,测试到IE 8):http://codepen.io/acusti/pen/bFBDr (2认同)

Dav*_*ick 9

css3动画仅适用于css属性.这在css的范围内是不可能的.