0

I have been using JavaScript scrollTop function to scroll the div to top by 5 px. but its not working. I am using:

scdiv.scrollTop = "5px";

The problem is it scrolls the div to top instead of moving to 5 px. Any suggestions?

6
  • 1
    Can you add more of your code to see if the problem is somewhere else? Commented Jul 28, 2012 at 9:33
  • Can you please clarify your question? Also, can you post an example on jsFiddle? Commented Jul 28, 2012 at 9:34
  • 1
    developer.mozilla.org/en/DOM/element.scrollTop Commented Jul 28, 2012 at 9:35
  • Do you want the page to scroll down by 5px? Commented Jul 28, 2012 at 9:35
  • no the div, my div id is scdiv Commented Jul 28, 2012 at 9:37

3 Answers 3

1

scrollTop is a property, its value is an int.

div.scrollTop += 5;

https://developer.mozilla.org/en/DOM/element.scrollTop

Sign up to request clarification or add additional context in comments.

Comments

0
$("#yourButtonId").on('click',function(){

var scDiv = $("#scdiv");

var top = scDiv.css('top');

top = parseInt(top);

scDiv.css('top',(top-5)+"px");

//To make it visually scrollable


  scDiv.animate({
    top: top
  },500);

});

Comments

0

scrollTop accepts integer numbers, it doesn't understand CSS values like px, so you would use something like this: element.scrollTop += 5;

See an example here: http://jsfiddle.net/pMafC/.

You can click a button to make the div scroll.

See scrollTop - MDN.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.