forked from edg2s/rangefix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
38 lines (33 loc) · 1.21 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$( function () {
var selection = document.getSelection();
$( document ).on( 'mousemove', function () {
if ( selection.rangeCount === 0 ) {
return;
}
var i, l, rect, rects, offset,
range = selection.getRangeAt( 0 ),
$highlightsNative = $( '<div>' ),
$highlightsFixed = $( '<div>' );
// Native
rects = range.getClientRects();
for ( i = 0, l = rects.length; i < l; i++ ) {
$highlightsNative.append( $( '<div>' ).addClass( 'highlight' ).css( rects[i] ) );
}
$( '.highlights-native' ).empty().append( $highlightsNative );
rect = range.getBoundingClientRect();
$highlightsNative.append( $( '<div>' ).addClass( 'bounding' ).css( rect ) );
// Fixed
rects = rangeGetClientRects( range );
for ( i = 0, l = rects.length; i < l; i++ ) {
$highlightsFixed.append( $( '<div>' ).addClass( 'highlight' ).css( rects[i] ) );
}
$( '.highlights-fixed' ).empty().append( $highlightsFixed );
rect = rangeGetBoundingClientRect( range );
if ( rect ) {
$highlightsFixed.append( $( '<div>' ).addClass( 'bounding' ).css( rect ) );
}
// Adjust for container position
offset = $( '.col-text' )[0].getBoundingClientRect();
$( '.highlights' ).css( { top: -offset.top, left: -offset.left } );
} );
} );