You should start using jQuery or some other js lib. It's way easier than js, and you can use it as a shorthand for most js instead of actually long, drawn out js.
Simply put <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
(or whatever the latest google cdn is https://developers.google.com/speed/libraries/devguide#jquery) in your <head>
.
Then, inside your event
code (much easier if you use jQuery: $.click()
for buttons, $.change()
for checkboxes, selects, radios...), put the code from your second link looking more like
$('#theIDofTheButtonThatTriggersThisAnimation').click(function(){
$('#theIDofTheElementYouWantToSmoothlyScroll').animate({
scrollTop: 0
}, 2000);
});
However, if you're trying to do animations, I recommend you look into some basic css properties like position:absolute
and position:relative
to keep from going crazy.
I'm still not quite sure what's going on in your code because it's very non-standard relative to how things are done now with css & jQuery. I'd break it down into something simple to learn the general concept.
For your example, you should build off of my animation example, how I learned: https://stackoverflow.com/a/12906254/1382306
I think you're trying to move your text up and down based upon a $.click()
. In the fiddle in my answer, it slides left and right. You can easily reformat up and down by using the css top
property instead of left
.
Once you figure out how to move the entire div
up and down, you can make a relative
container to hold all of the content absolute
div
s and manipulate all content div
s with a loop by setting their top
s. Here's a quick primer on absolute
in relative
: http://css-tricks.com/absolute-positioning-inside-relative-positioning/
All of my animations have relative
containers and absolute
content. It's how I made a custom gridview plugin that can instantly zip through an entire database.
Also, there really is no overuse of div
s when it comes to animating. Trying to make 1 div
do everything is a nightmare.
Try to see if you can reformat my fiddle into a vertical slide out. Once you've done that, research absolute
in relative
a little. If you have any more problems, just ask another question.
Change your thinking to these philosophies, and you'll start flying through this type of coding.