12

DIV包含需要滚动条的段落的碎片

例如

<div id="text" style='overflow:scroll;width:200px;height:200px'>

<div style='font-size:64px;'>BIG TEXT</div> 
Lorem Ipsum is simply dummy text of the printing and typesetting 
industry. Lorem Ipsum    has been the industry's standard dummy text ever  
since the 1500s, when an unknown printer took a galley of type and scrambled it 
to make a type specimen book. It has survived not only five centuries, but also 
the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets 
containing Lorem Ipsum passages, and more recently with desktop publishing software 
like Aldus PageMaker 
including versions of Lorem Ipsum.

</div>

当滚动条移动时,文本发生变化(由于overflow:scroll),是否可以只选择当前视口中显示的文本?

示例:http: //jsfiddle.net/cxgkY/15/

更新:内部 HTML 可能包含可变大小的文本

4

4 回答 4

4

这是一个小演示,应该可以满足您的期望:小链接(也适用于可变大小)。这个想法是为每个单词自动创建一个单独span的,然后,每次div滚动时,检查哪些spans 是可见的(通过检查它们的top偏移量),从而更新文档选择范围。如果有什么不清楚的地方,我很乐意解释。

于 2012-08-25T10:10:08.127 回答
3

不知道只选择当前视口中的文本是什么意思,但可能是这样的:

var elm = $("#text"),
    t = $(elm).text().split(' '),
    html = [],
    selected_text = '';

$.each(t, function(i,e) {
    html.push('<span>'+e+'</span>');
});

elm.html(html.join(' '));

$('span', elm).each(function(i,e) {
    if ($(e).offset().top < elm.height()) {
        $(e).css('background', 'red');
        selected_text += $(e).text()+' ';
    }
});

小提琴

如果本练习的目的只是在另一个容器元素中显示文本,您可以按常规方式进行并伪造它:

$("#text").on('scroll', function() {
    $('#visible').html($(this).html()).scrollTop($(this).scrollTop());
});

小提琴 ​</p>

于 2012-08-25T09:43:33.857 回答
0

可能是这样的(参见演示:http: //jsfiddle.net/cxgkY/14/):

HTML:

<div id="text">
    <div id="wrapper">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

​ ​ JavaScript:

$('#text').scroll(function () {
  var text = $(this).text();

  var begin = $(this).scrollTop() / 
      $(this).children('#wrapper').height();

  var end = begin + $(this).height() /
    $(this).children('#wrapper').height();

  var text = text.slice(text.length * begin, text.length * end);
  $('#visible').text(text);
});  
于 2012-08-25T09:41:33.487 回答
0
<div id="text">
    <p>Line 1</p>
    <p>Line 2</p>
    <p>Line 3</p>

</div>

你可以做..

$(function(){
    var meanLineHeight = $('#text > p').first().outerHeight();

    var result = $('#result');

    $('#text').scroll(function(){
        var linesScrolled = Math.ceil(this.scrollTop/ meanLineHeight);
        console.log(linesScrolled);

        var linesToSelect = $('#text > p').slice(linesScrolled);
        linesToSelect.css('background-color', 'yellow');
    });​​​​​​​
});

​演示

于 2012-08-25T09:58:06.817 回答