HTML: How to set scroll bar thumb to a specific location

My application involves appointments. That means the <div> that contains these appointment entries will necessarily have scrollbars, both vertical and horizontal. My goal is to be able to center the last appointment added or edited within the viewport of the browser.

Here's how I'm envisioning the thought process:

When an appointment is added/edited, I will get the appointment <div>'s x-y position, and its width/height. I need to get the browser viewport's width and height. Based on this, I need to calculate and set the vertical and horizontal scrollbars thumb to a certain position so that this appointment that was added/created will somehow appear dead center within the browser viewport, both horizontally and vertically.

Can anyone tell me how I can accomplish this? Code snippets would be appreciated, as that would demystify to me the appropriate JavaScript screen properties and functions I need to work with. Help on this would really be immensely appreciated.

Thanks in advance.
elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lenamtlCommented:
Hi,

I suggest you to use a responsive framework like Bootstrap, Foundation, Getskeleton this will save you a lot of calculation.

http://getbootstrap.com/
http://foundation.zurb.com/
http://getskeleton.com/
elepilAuthor Commented:
lenamtl, thanks for responding.

You do realize learning a framework can be a daunting task. Instead of giving me general links as you've done, can you provide specific links within those framework sites that shows it can do what I am trying to do? Last thing I want is to spend hours and hours, only to find out it can't assist in my objectives.

Thank you.
Robert SchuttSoftware EngineerCommented:
see this fiddle. the code is:
function doScroll() {
    var cal = document.getElementById('calendar');
    var calstl = window.getComputedStyle(cal);
    var itm = document.getElementById('item');
    var itmstl = window.getComputedStyle(itm);
    cal.scrollLeft = getNumProp(itmstl, 'left') - (getNumProp(calstl, 'width') - getNumProp(itmstl, 'width')) / 2;
    cal.scrollTop = getNumProp(itmstl, 'top') - (getNumProp(calstl, 'height') - getNumProp(itmstl, 'height')) / 2;
}

function getNumProp(stl, prp) {
    return Number(stl[prp].replace('px',''));
}

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

elepilAuthor Commented:
To Robert Schutt, thank you for responding.

I greatly appreciate your code snippet. Having said that, and this is my fault for not being more emphatically specific in my description, the target objects I intend to center are actually generated dynamically. In my original post, I talked about "appointment <div>s". Appointments are made on the fly, and it's not something I can make a CSS entry for prior. Your code snippet relies on CSS entries already made prior, specifically the left and top CSS properties.

When I try to get the CSS left and top on an object with no predefined CSS definition, I get 'auto' for both left and top, making it impossible to calculate coordinates. Is it possible for you to slightly modify your code snippet so as not to rely on predefined CSS styles on the target object?

Thanks.
Robert SchuttSoftware EngineerCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
Robert, that was awesome! Thank you very much for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.