简体   繁体   English

HTML textarea; 垂直滚动到文本

[英]HTML textarea; scroll vertically to text

I have an HTML textarea: 我有一个HTML textarea:

<textarea>
Some text
Another text in another line

BOOM

Hello there.
</textarea>

I want to be able to vertically scroll to the word BOOM so that it is visible (it doesn't matter on which line it appears). 我希望能够垂直滚动到单词BOOM,以便它可见(它出现在哪一行无关紧要)。

Is this possible? 这可能吗?

There's actually a way to do this using window.find() and some variation for IE browsers. 实际上有一种方法可以使用window.find()和IE浏览器的一些变体。

Here's what I came up with so far: 这是我到目前为止所提出的:

var gotoText = function(text) {
    function iefind(string) {
        var txt = document.body.createTextRange(); 
        if (txt.findText(string)) { 
            txt.scrollIntoView();
            txt.collapse(false);
        }
    }

    if(!window.find) { // ie
        iefind(text); 
        return;
    }

    // a double window.find() for backwards and forward search
    if(!window.find(text, false, true)){
       window.find(text, false, false);
    }
};
$('textarea').animate({ 'scrollTop': 30 });

of course 30 is working for my own example with my cols and rows. 当然30是我的cols和行的自己的例子。 so find the the right value for yourself. 所以找到适合自己的价值。

Note to self: 注意自我:

There is no way of calculating the scroll height which a particular word is at however if you set a fixed value as your css line-height then you can see the story from the point of view that of boom is on for example the 4th line then its scroll height value is 4x (x:line-height) but i can't really right now be bothered to check how good that will work when someone zooms in the browser. 没有办法计算特定单词所在的滚动高度,但是如果你设置一个固定值作为你的css行高,那么你可以从繁荣的例子看第4行那样的故事。它的滚动高度值是4x(x:line-height)但是我现在真的不能去检查当有人放大浏览器时它会有多好用。 but worth a try for you as you have the case. 但是,如果你有这种情况,值得一试。

But how do I know that BOOM is 30 from the top? 但我怎么知道BOOM从顶部开始是30?

Create a <div> , add it to the page and style it with exactly the same font, white-space , dimensions, border, padding and overflow as the <textarea> object. 创建一个<div> ,将其添加到页面并使用与<textarea>对象完全相同的字体, white-space ,尺寸,边框,填充和溢出来设置样式。 Wrap the 'BOOM' in a <span> , and measure the position of the span relative to the position of the div. 将“BOOM”包裹在<span> ,并测量跨度相对于div位置的位置。

(This isn't a lot of fun.) (这不是很有趣。)

It's possible with some javascript! 这可能与一些JavaScript! Even if I'm late I hope it can be usefull to someone else! 即使我迟到了,我希望它对其他人有用!

I published an answer here: 我在这里发表了一个答案:

http://blog.blupixelit.eu/scroll-textarea-to-selected-word-using-javascript-jquery/ http://blog.blupixelit.eu/scroll-textarea-to-selected-word-using-javascript-jquery/

It works perfectly with jsut one needed rule: Set a line-height n the css of the textarea! 它与jsut一个必需的规则完美配合:设置textarea的css的行高!

It calculate the position of the word to scroll to just by doing some simple mathematic calculation and it worked perfectly in all my experiments! 它通过做一些简单的数学计算来计算滚动到的单词的位置,它在我的所有实验中都能很好地工作!

Feel free to ask me anything you need about the code! 随意问我关于代码的任何信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM