Skip to content

Commit 4bd8538

Browse files
committed
Docs: restore isInView example
1 parent ceac326 commit 4bd8538

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
<div class="container">
2+
<div class="draggable box">Drag Me</div>
3+
<div class="filler"></div>
4+
</div>
15

2-
<div id="container" style="width: 200px; height: 100px; overflow: auto; position: relative;">
3-
<div id="content" style="height: 300px;">
4-
<div id="test" style="position: absolute; top: 150px; left: 10px; width: 50px; height: 50px;"></div>
5-
</div>
6-
</div>
6+
<div class="output">
7+
<h3>Visibility Status</h3>
8+
<pre></pre>
9+
</div>
Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,83 @@
11
import { $ } from '@semantic-ui/query';
22

3-
const container = document.getElementById('container');
4-
container.scrollTop = 0;
5-
console.log($('#test').isInView());
6-
container.scrollTop = 100;
7-
console.log($('#test').isInView());
3+
// Drag functionality
4+
let isDragging = false;
5+
let $dragged = null;
6+
let offset = { top: 0, left: 0 };
7+
8+
// handle drag and drop start
9+
$('.box').on('pointerdown', function(event) {
10+
event.preventDefault();
11+
isDragging = true;
12+
$dragged = $(this);
13+
const bounds = $dragged.bounds();
14+
offset = {
15+
top: event.clientY - bounds.top,
16+
left: event.clientX - bounds.left,
17+
};
18+
moveElementToMouse(event);
19+
});
20+
21+
// handle drag & drop
22+
$(document)
23+
.on('pointermove', function(event) {
24+
if (isDragging && $dragged) {
25+
moveElementToMouse(event);
26+
}
27+
})
28+
.on('pointerup', function() {
29+
isDragging = false;
30+
$dragged = null;
31+
});
32+
33+
// handle scroll
34+
$('.container').on('scroll', checkVisibility);
35+
36+
function moveElementToMouse(event) {
37+
const container = $('.container').dimensions();
38+
$dragged.position({
39+
relativeTo: '.container',
40+
top: event.clientY - container.top + container.scrollTop - offset.top,
41+
left: event.clientX - container.left + container.scrollLeft - offset.left,
42+
});
43+
checkVisibility();
44+
}
45+
46+
// Initial check
47+
requestAnimationFrame(() => checkVisibility());
48+
49+
function checkVisibility() {
50+
const $box = $('.draggable');
51+
const $container = $('.container');
52+
53+
// Check visibility with different thresholds
54+
const basicCheck = $box.isInView({ viewport: $container });
55+
const halfVisible = $box.isInView({ viewport: $container, threshold: 0.5 });
56+
const fullyVisible = $box.isInView({ viewport: $container, fully: true });
57+
58+
const containerDims = $container.dimensions();
59+
60+
const info = `VISIBILITY STATUS:
61+
62+
In View: ${basicCheck ? '✓ YES' : '✗ NO'}
63+
50% Visible: ${halfVisible ? '✓ YES' : '✗ NO'}
64+
Fully Visible: ${fullyVisible ? '✓ YES' : '✗ NO'}
65+
66+
Container Scroll:
67+
Top: ${containerDims.scrollTop}px
68+
Left: ${containerDims.scrollLeft}px`;
69+
70+
$('.output pre').text(info);
71+
72+
// Update visual state
73+
$box.removeClass('in-view partially-in-view out-of-view');
74+
if (fullyVisible) {
75+
$box.addClass('in-view');
76+
}
77+
else if (basicCheck) {
78+
$box.addClass('partially-in-view');
79+
}
80+
else {
81+
$box.addClass('out-of-view');
82+
}
83+
}

0 commit comments

Comments
 (0)