Is there a way to animate scrolling with CSS3?
Something like this?
@-webkit-keyframes scrolltoview
{
0% { scrollTop: 0; }
100% { scrollTop: 30%; }
}
How to put in the css where's the staring point of the animation?
Is there a way to animate scrolling with CSS3?
Something like this?
@-webkit-keyframes scrolltoview
{
0% { scrollTop: 0; }
100% { scrollTop: 30%; }
}
How to put in the css where's the staring point of the animation?
As explained here you can do it using margin-top
trick and updating scroll position dynamically. You can check the demo. Code is straight forward:
// 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");
});
});
});
@AndrewP provided a nice working example based on this idea
css3 animations work with css properties only. this isn't possible within the confines of css.
I discovered a better way to animate scrolling, which is to use JavaScript to translate the document body. The advantages of animating with translate over margin or position is pretty well documented. The bottom line is, when animating elements, the GPU will always do a better job.
Here's an example which calculates the user's current position from the top of the window and then smoothly translate back to the top of the window. Assume that a user click or touch event triggered the backToTop function.
See it in action: https://antibland.github.io/starting_point/
function whichTransitionEvent() {
var t,
el = document.createElement('fakeelement'),
transitions = {
'transition':'transitionend',
'OTransition':'oTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd'
};
for (t in transitions){
if (el.style[t] !== undefined){
return transitions[t];
}
}
}
function backToTop() {
var pos_from_top = window.scrollY,
transitionend = whichTransitionEvent(),
body = document.querySelector("body");
function scrollEndHandler() {
window.scrollTo(0, 0);
body.removeAttribute("style");
body.removeEventListener(transitionend, scrollEndHandler);
}
body.style.overflowY = "scroll";
window.scrollTop = 0;
body.style.webkitTransition = 'all .5s ease';
body.style.transition = 'all .5s ease';
body.style.webkitTransform = "translateY(" + pos_from_top + "px)";
body.style.transform = "translateY(" + pos_from_top + "px)";
transitionend && body.addEventListener(transitionend, scrollEndHandler);
}
I don't think this is possible with css3, but if I understand your question correctly, it is very easy with jquery.
Check out:
This is a very simple .js function i made to animate + scroll a webpage.
var Current_scroll_Y=0;
var Target_scroll_Y=0;
var scroll_Speed = 15;
function animate_Scroll(Y)
{
Target_scroll_Y = Y;
screen_Y = Math.floor(window.scrollY);
//Scroll Down
if(screen_Y<Y)
{
var myWindowScroll = setInterval(function(){ screen_Y = screen_Y+scroll_Speed; if(screen_Y>=Target_scroll_Y){ clearInterval(myWindowScroll); return;} window.scrollTo(0, screen_Y); }, 3);
}
//Scroll Up
if(screen_Y>Y)
{
var myWindowScroll = setInterval(function(){ screen_Y = screen_Y-scroll_Speed; if(screen_Y<=Target_scroll_Y){ clearInterval(myWindowScroll); return;} window.scrollTo(0, screen_Y); }, 3);
}
}//End animate_Scroll