admin管理员组文章数量:1316019
A chipped DIV
containing paragraphs that need a scrollbar
e.g.
<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>
When the scrollbar move, the text change (due to overflow:scroll
), is it possible to select only the text displayed in the current view port?
Example: /
Updated: The inner HTML might contains variable sized text
A chipped DIV
containing paragraphs that need a scrollbar
e.g.
<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>
When the scrollbar move, the text change (due to overflow:scroll
), is it possible to select only the text displayed in the current view port?
Example: http://jsfiddle/cxgkY/15/
Updated: The inner HTML might contains variable sized text
Share Improve this question edited Aug 25, 2012 at 10:00 Howard asked Aug 25, 2012 at 9:28 HowardHoward 19.8k36 gold badges115 silver badges187 bronze badges4 Answers
Reset to default 4Here's a little demo that should do what you're expecting: little link (works with variable sizes as well). The idea is to automatically create a separate span
for each word, and then, each time the div
is scrolled, check which span
s are visible (by checking their top
offset), thus updating document selection range. If anything isn't clear I'd be glad to explain it.
Not sure what you mean by selecting just the text in the current viewport, but maybe something like this:
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()+' ';
}
});
FIDDLE
If the point of this exercise is to just display the text in another container element you can just do it the regular way and fake it:
$("#text").on('scroll', function() {
$('#visible').html($(this).html()).scrollTop($(this).scrollTop());
});
FIDDLE
May be something like this (See DEMO: http://jsfiddle/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);
});
<div id="text">
<p>Line 1</p>
<p>Line 2</p>
<p>Line 3</p>
</div>
You could do..
$(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');
});
});
DEMO
本文标签: javascriptHow to get the current active text within a scrolled DIVStack Overflow
版权声明:本文标题:javascript - How to get the current active text within a scrolled DIV - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741994363a2409741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论