james438
07-12-2011, 03:13 PM
Sorry, I wasn't sure how to best title this thread. I am open to suggestions.
I have this script, which I have pruned down a great deal for simplicity sake. I am trying to get the line numbers on the left to scroll along with the text in the textarea. It works in ie8 and firefox 5, but is not perfect in Opera 11.5. It appears that I need to add an event listener. I am very much a novice with javascript though.
here is the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<head>
<TITLE>
Editor 1.23</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<style type="text/css">
#editContent{
margin-left:40px;
padding-left:3px;
border:1px solid #666;
}
.textAreaWithLines{
display:block;
margin:0;
border:1px solid #666;
border-right:none;
background:#aaa;
}
textarea{
border:0px;margin:0px;padding:0px;
line-height:16px;
background-color:#FFFFFF;color:black;}
body{
color:#FFFFFF;
background-color:navy;
line-height:16px;
}
</style>
<script type="text/javascript">
function createTextAreaWithLines(id)
{
var el = document.createElement('TEXTAREA');
var ta = document.getElementById(id);
var string = '';
for(var no=1;no<3000;no++){
if(string.length>0)string += '\n';
string += no;
}
el.className = 'textAreaWithLines';
el.style.height = (ta.offsetHeight-3) + "px";
el.style.width = "34px";
el.style.position = "absolute";
el.style.overflow = 'hidden';
el.style.textAlign = 'right';
el.style.paddingRight = '0.2em';
el.innerHTML = string; //Firefox renders \n linebreak
el.innerText = string; //IE6 renders \n line break
el.style.zIndex = 0;
ta.style.zIndex = 1;
ta.style.position = "relative";
ta.parentNode.insertBefore(el, ta.nextSibling);
setLine();
ta.focus();
ta.onmousedown = function() { setLine(); move=true; }
ta.onmouseup = function() { setLine(); move=false; }
ta.onmouseover = function() { setLine(); }
ta.onmousemove = function() { setLine(); }
ta.onmousewheel = function() { setLine(); }
ta.onblur = function() { setLine(); }
ta.onfocus = function() { setLine(); }
ta.onkeypress = function() { setLine(); }
ta.onkeydown = function() { setLine(); }
ta.onscroll = function() { setLine(); }
function setLine(){
el.scrollTop = ta.scrollTop;
el.style.top = (ta.offsetTop) + "px";
el.style.left = (ta.offsetLeft - 37) + "px";
}
}
</script></head>
<body>
<div style='width:100%;'><div style="width:72%;float:left;">
Contents of the web file:
<br><textarea name="data" style="width:98%;scrollbar-base-color:tan;height:722px;" wrap="off" id="editContent">
</textarea>
<script type="text/javascript">createTextAreaWithLines('editContent');</script>
</div>
<div style="float:left;width:23%;padding-left:40px;">
<b>contents of folder: <span style='color:orange;'></span></b><br>
<div style='height:725px;overflow:auto;scrollbar-base-color:tan;float:left;width:100%;
background-color:blue;line-height:19px;border:0px;margin:0px;padding:0px;'></div>
</div></div></body></html>
If you add multiple lines until it scrolls and then use the mouse wheel to scroll up and down you will see that it is not perfect. Make sure you do not move your mouse or else the line numbers correct itself.
It seems that I need to apply an event listener as demonstrated in this page: http://www.switchonthecode.com/tutorials/javascript-tutorial-the-scroll-wheel but I am not sure how to do that. Any help is certainly appreciated.
I have this script, which I have pruned down a great deal for simplicity sake. I am trying to get the line numbers on the left to scroll along with the text in the textarea. It works in ie8 and firefox 5, but is not perfect in Opera 11.5. It appears that I need to add an event listener. I am very much a novice with javascript though.
here is the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<head>
<TITLE>
Editor 1.23</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<style type="text/css">
#editContent{
margin-left:40px;
padding-left:3px;
border:1px solid #666;
}
.textAreaWithLines{
display:block;
margin:0;
border:1px solid #666;
border-right:none;
background:#aaa;
}
textarea{
border:0px;margin:0px;padding:0px;
line-height:16px;
background-color:#FFFFFF;color:black;}
body{
color:#FFFFFF;
background-color:navy;
line-height:16px;
}
</style>
<script type="text/javascript">
function createTextAreaWithLines(id)
{
var el = document.createElement('TEXTAREA');
var ta = document.getElementById(id);
var string = '';
for(var no=1;no<3000;no++){
if(string.length>0)string += '\n';
string += no;
}
el.className = 'textAreaWithLines';
el.style.height = (ta.offsetHeight-3) + "px";
el.style.width = "34px";
el.style.position = "absolute";
el.style.overflow = 'hidden';
el.style.textAlign = 'right';
el.style.paddingRight = '0.2em';
el.innerHTML = string; //Firefox renders \n linebreak
el.innerText = string; //IE6 renders \n line break
el.style.zIndex = 0;
ta.style.zIndex = 1;
ta.style.position = "relative";
ta.parentNode.insertBefore(el, ta.nextSibling);
setLine();
ta.focus();
ta.onmousedown = function() { setLine(); move=true; }
ta.onmouseup = function() { setLine(); move=false; }
ta.onmouseover = function() { setLine(); }
ta.onmousemove = function() { setLine(); }
ta.onmousewheel = function() { setLine(); }
ta.onblur = function() { setLine(); }
ta.onfocus = function() { setLine(); }
ta.onkeypress = function() { setLine(); }
ta.onkeydown = function() { setLine(); }
ta.onscroll = function() { setLine(); }
function setLine(){
el.scrollTop = ta.scrollTop;
el.style.top = (ta.offsetTop) + "px";
el.style.left = (ta.offsetLeft - 37) + "px";
}
}
</script></head>
<body>
<div style='width:100%;'><div style="width:72%;float:left;">
Contents of the web file:
<br><textarea name="data" style="width:98%;scrollbar-base-color:tan;height:722px;" wrap="off" id="editContent">
</textarea>
<script type="text/javascript">createTextAreaWithLines('editContent');</script>
</div>
<div style="float:left;width:23%;padding-left:40px;">
<b>contents of folder: <span style='color:orange;'></span></b><br>
<div style='height:725px;overflow:auto;scrollbar-base-color:tan;float:left;width:100%;
background-color:blue;line-height:19px;border:0px;margin:0px;padding:0px;'></div>
</div></div></body></html>
If you add multiple lines until it scrolls and then use the mouse wheel to scroll up and down you will see that it is not perfect. Make sure you do not move your mouse or else the line numbers correct itself.
It seems that I need to apply an event listener as demonstrated in this page: http://www.switchonthecode.com/tutorials/javascript-tutorial-the-scroll-wheel but I am not sure how to do that. Any help is certainly appreciated.