Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css or javascript scroll transition?

I have two divs with:

width:100%; height:100%

so my whole document has an height of 200%; both div`s have an link to each other,

now when i click on the link, i want that the site smoothly slides to the other div,

I know how this would work in jquery , for example with .scrollto, but my client wants an app wihout frameworks. Only javascricpt and css!

I tried to achive it with translateY, but it didnt worked!

Here is an exemplary code: http://jsfiddle.net/hSU7R/

The HTML

<div class="full" id="one">
   <span style="width:100%; background-color:blue">
   <a href="#two" >Scroll to 2</a>
   </span>    
</div>


<div class="full" id="two">
    <span style="width:100%; background-color:blue">
    <a href="#one" >Scroll to 1</a></span>
</div>

The CSS

html,body { 
    width:100%;
    height:100%;}

.full {
    height:100%;
    width:100%;}

#one {background-color:green}
#two {background-color:red}
like image 614
Em Sta Avatar asked Feb 15 '13 20:02

Em Sta


People also ask

Is it better to animate with CSS or JavaScript?

TL;DR # Use CSS animations for simpler "one-shot" transitions, like toggling UI element states. Use JavaScript animations when you want to have advanced effects like bouncing, stop, pause, rewind, or slow down.

Are CSS animations faster than JavaScript?

The fact is that, in most cases, the performance of CSS-based animations is almost the same as JavaScripted animations — in Firefox at least. Some JavaScript-based animation libraries, like GSAP and Velocity. JS, even claim that they are able to achieve better performance than native CSS transitions/animations.


1 Answers

Is this what you're looking for? A fork of your jsFiddle.

There has to be a smarter way to do this, but that's why we have jQuery right? My basic idea was to grab each anchor and turn off the default click response. Then, replace it with one that starts a setInterval chain. Each time the interval transpires, the window will incrementally scroll based on a frame rate and an estimated total run time. The actual run-time seems to take longer than the input time, but it at least gives you a way to get started.

What is the main disadvantage to using jQuery? I would think you'd get better performance from their implementation, since the jQuery people work on this stuff all the time.

like image 133
Austin Mullins Avatar answered Oct 02 '22 00:10

Austin Mullins