【问题标题】:jQuery: scroll textarea to given positionjQuery:将文本区域滚动到给定位置
【发布时间】:2011-03-28 11:58:43
【问题描述】:

我有一个包含大量文本的文本区域:

<textarea cols="50" rows="10" id="txt">lots and lots of text goes here</textarea>

我想向下滚动文本区域,以便用户可以看到第 2000 个字符。如何使用 javasctipt/jQuery 做到这一点?

$('#txt').scrollToCharNo(2000); // something like this would be great

编辑(我的解决方案)

嗯,我自己设法让它工作。我发现的唯一方法是创建一个与 textarea 具有相同字体和宽度的 DIV,在所需字符附近放置一个 SPAN 并找到该跨度的位置。

我敢肯定,有人可能会发现我的解决方案很有用,所以我将其粘贴在这里:

jQuery.fn.scrollToText = function(search) {
    // getting given textarea contents
    var text = $(this).text();
    // number of charecter we want to show
    var charNo = text.indexOf(search);
    // this SPAN will allow up to determine given charecter position
    var anch = '<span id="anch"></span>';
    // inserting it after the character into the text
    text = text.substring(0, charNo) + anch + text.substring(charNo);

    // creating a DIV that is an exact copy of textarea
    var copyDiv = $('<div></div>')
                    .append(text.replace(/\n/g, '<br />')) // making newlines look the same
                    .css('width', $(this).attr('clientWidth')) // width without scrollbar
                    .css('font-size', $(this).css('font-size'))
                    .css('font-family', $(this).css('font-family'))
                    .css('padding', $(this).css('padding'));

    // inserting new div after textarea - this is needed beacuse .position() wont work on invisible elements
    copyDiv.insertAfter($(this));
    // what is the position on SPAN relative to parent DIV?
    var pos = copyDiv.find('SPAN#anch').offset().top - copyDiv.find('SPAN#anch').closest('DIV').offset().top;
    // the text we are interested in should be at the middle of textearea when scrolling is done
    pos = pos - Math.round($(this).attr('clientHeight') / 2);
    // now, we know where to scroll!
    $(this).scrollTop(pos);
    // no need for DIV anymore
    copyDiv.remove();
};


$(function (){
    $('#scroll_button').click(
        function() {
            // scrolling to "FIND ME" using function written above
            $('#txt').scrollToText('FIND ME');
            return false;
        }
    );
});

这是一个演示(它有效!):http://jsfiddle.net/KrVJP/

由于没有一个答案真正解决了问题,我会接受experimentX的答案:感谢您努力帮助我,我很感激!

【问题讨论】:

  • 请查看以下question。我认为这会对你有所帮助。
  • 恐怕我的问题没有答案。我怎么知道第 2000 个字符的位置?
  • 您为什么不发布自己的解决方案作为答案并接受它?
  • @Bill the Lizard,我知道这是合乎逻辑的,但我想奖励帮助过我的人,即使他们的答案不准确。

标签: javascript jquery textarea scroll


【解决方案1】:

我不确定它是否会起作用。也请检查它here。它似乎适用于第 2000 位、第 1500 位和第 1000 位。

编辑混淆字体大小或行高???

 $(function (){
    var height = 2000/$('#txt').attr('cols');

    $('#txt').scrollTop(height*13);

    $('#txt').selectRange(2000,2000); //this is just to check
});

$.fn.selectRange = function(start, end) { //this is just to check 
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

更新这个怎么样

    var height = 1200/$('#txt').attr('cols');
    var line_ht = $('#txt').css('line-height');
    line_ht = line_ht.replace('px','');

    height = height*line_ht; 

【讨论】:

  • 这行不通,因为它没有考虑行高。 selectRange 函数在 Chrome 中也不适用于我。
  • @AndyE 看来 selectRange 正在工作,但 srcollTop jsfiddle.net/ZY8az/2
  • 另外,这段代码没有考虑到不是每行长度(字符数)都等于cols属性,因为文本换行。
  • @AndyE 检查更新——但我仍然期待很多错误。
  • @SilverLight 有什么建议,我想不出其他... :(
【解决方案2】:

你可以使用thisjquery插件

【讨论】:

  • 有问题:你不能在那里设置字符,只能设置 x/y 位置。我如何知道第 2000 个字符的坐标?
  • 只是想知道您是否可以使用 DOM 元素- $(...).scrollTo( $('ul').get(2).childNodes[20], 800 );上面链接中给出的“指定目标的方式”中的选项。但恐怕 textarea 不支持子节点属性
  • 这个插件似乎没有提供将教科书滚动到给定行或字符的功能。
猜你喜欢
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2014-06-28
  • 1970-01-01
  • 2013-04-01
  • 2014-12-20
  • 1970-01-01
相关资源
最近更新 更多