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.net/cxgkY/15/
Updated: The inner HTML might contains variable sized text
Here'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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With