Can I programatically set and/or get the position of the scroll bar in my client's browser?
I have a web site, and I would like to control the scroll bar.
I'd like to be able to say, use a method like scroll_bar_set($heig开发者_C百科ht)
(if such functions exist)...
I guess it should be javascript or jquery.
Can anybody tell me ?
Thx in advance
I might be too old school, but there is other way you can do it without Javascript.
You can use an tag somewhere in your HTML and link the user to say google.com/#second and the browser will take the user to either an A tag that has it's 'name' attribute with the value of 'second' or to an element whose ID is 'second', in either way it's going to find an unique element with the value of 'second'.
Keep in mind that this might be more accurate than setting a hard vertical position, since unless you have a very anal control over word breaking, line spacing, font easing and others, things are going to shift a few pixels from browser to browser and OS to OS.
If you don't want to add jQuery just for this basic function. Then try using
window.scrollTo(xpos,ypos);
you can put this on a js function like so
function scrollTop(){
window.scrollTo(0, 0);
}
You can also pass % in to the values window.scrollTo(0,100%);
see http://api.jquery.com/scrollTop/ and http://plugins.jquery.com/project/ScrollTo
scrollTop lets you get and set the scroll position.
精彩评论