使用jQuery,我用
选择div中的所有文本var c = $('#content').text();
但我想只选择浏览器窗口中正在查看的文本。所以,只是澄清我的想法的一个粗略的例子,看看下面的图像:
应该只返回:
自行车车架是自行车的主要部件,其上有车轮和其他车辆
然而,如果我要打开浏览器,并重新运行代码
自行车车架是自行车的主要部件,车轮和其他部件安装在自行车车架上。直立式自行车的现代和最常见的框架设计基于安全自行车,由两个三角形组成,一个主三角形和一个成对的后三角形。这被称为钻石框架。1框架需要坚固,坚硬和轻盈,它们通过组合不同的材料和形状来实现。
应该退回。
是否有某种方法可以检测浏览器窗口中任何给定时刻哪些文本可见?
答案 0 :(得分:3)
是的,您需要测量视口以计算元素顶部的距离。请参阅此fiddle。
$.fn.is_on_screen = function(){
var win = $(window);
var viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
var bounds = this.offset();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();
return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};
if( $('.target').length > 0 ) { // if target element exists in DOM
if( $('.target').is_on_screen() ) { // if target element is visible on screen after DOM loaded
$('.log').html('<div class="alert alert-success">target element is visible on screen</div>'); // log info
} else {
$('.log').html('<div class="alert">target element is not visible on screen</div>'); // log info
}
}
$(window).scroll(function(){ // bind window scroll event
if( $('.target').length > 0 ) { // if target element exists in DOM
if( $('.target').is_on_screen() ) { // if target element is visible on screen after DOM loaded
$('.log').html('<div class="alert alert-success">target element is visible on screen</div>'); // log info
} else {
$('.log').html('<div class="alert">target element is not visible on screen</div>'); // log info
}
}
});
<强> HTML 强>
<div class="log">log</div>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
<div class="target label label-info">target element</div>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
<强> CSS 强>
.log {
position: fixed;
right: 20px;
top: 20px;
}
答案 1 :(得分:0)
我认为:visible伪选择器可以帮助我们。
$('<div/>').html( $('.selector') ).contents().filter(':visible').text();