Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to set my horizontal bar scroll position to middle in div please help me,here i pasted my code below.


<pre lang="HTML">
<div id="divrpt"  style="overflow:auto;width:750px;height:350px; removed:absolute;" >

contents

<div>
Posted

1 solution

Hello,

You can do this via JavaScript. use code something like shown below.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Scroll Test</title>
<style type="text/css">
body { font: normal normal normal 16px/12px "Segoe UI", Calibri, Verdana, Tahoma, Arial; }
</style>
<script type="text/javascript">
function adjustScroll() {
	var elm = document.getElementById('codeDiv');
	elm.scrollLeft = (elm.scrollWidth - elm.offsetWidth)/2;
}
window.onload = adjustScroll;
</script>
</head>
<body>
    <div id="codeDiv" style="width:200px; height:300px;overflow:auto;padding:0;margin:0;border:1px solid #8c8c8c;">
        <pre style="margin:0;padding:0;border-width:0" id="codePart">
<span style='color:#800000; font-weight:bold; '>import</span><span style='color:#004a43; '> java</span><span style='color:#808030; '>.</span><span style='color:#004a43; '>io</span><span style='color:#808030; '>.</span><span style='color:#800000; font-weight:bold; '>*</span><span style='color:#800080; '>;</span>
<span style='color:#800000; font-weight:bold; '>class</span> MyFirstProgram <span style='color:#800080; '>{</span>
  <span style='color:#3f5fbf; '>/** Print a hello message */</span>
  <span style='color:#800000; font-weight:bold; '>public</span> <span style='color:#800000; font-weight:bold; '>static</span> <span style='color:#bb7977; '>void</span> main<span style='color:#808030; '>(</span><span style='color:#bb7977; font-weight:bold; '>String</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> args<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
    <span style='color:#bb7977; font-weight:bold; '>BufferedReader</span> in <span style='color:#808030; '>=</span>
        <span style='color:#800000; font-weight:bold; '>new</span> <span style='color:#bb7977; font-weight:bold; '>BufferedReader</span><span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>new</span> <span style='color:#bb7977; font-weight:bold; '>InputStreamReader</span><span style='color:#808030; '>(</span><span style='color:#bb7977; font-weight:bold; '>System</span><span style='color:#808030; '>.</span>in<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#bb7977; font-weight:bold; '>String</span> name <span style='color:#808030; '>=</span> <span style='color:#0000e6; '>"Instructor"</span><span style='color:#800080; '>;</span>
    <span style='color:#bb7977; font-weight:bold; '>System</span><span style='color:#808030; '>.</span>out<span style='color:#808030; '>.</span>print<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Give your name: "</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>try</span> <span style='color:#800080; '>{</span>name <span style='color:#808030; '>=</span> in<span style='color:#808030; '>.</span>readLine<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span><span style='color:#800080; '>}</span>
        <span style='color:#800000; font-weight:bold; '>catch</span><span style='color:#808030; '>(</span><span style='color:#bb7977; font-weight:bold; '>Exception</span> e<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
           <span style='color:#bb7977; font-weight:bold; '>System</span><span style='color:#808030; '>.</span>out<span style='color:#808030; '>.</span>println<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Caught an exception!"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800080; '>}</span>
    <span style='color:#bb7977; font-weight:bold; '>System</span><span style='color:#808030; '>.</span>out<span style='color:#808030; '>.</span>println<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Hello "</span> <span style='color:#808030; '>+</span> name <span style='color:#808030; '>+</span> <span style='color:#0000e6; '>"!"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
  <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
        </pre>
    </div>
</body>
</html>

Regards,
 
Share this answer
 
Comments
ManojMurali 29-May-13 7:43am    
Thank a lot its working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900