Skip to content

Commit 5b89b41

Browse files
committed
Update isinview example
1 parent 7078cd3 commit 5b89b41

File tree

4 files changed

+132
-135
lines changed

4 files changed

+132
-135
lines changed

docs/src/examples/query/dimensions/query-intersects/page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function moveElementToMouse(event) {
4242
}
4343

4444
// Initial check
45-
checkIntersection();
45+
requestAnimationFrame(() => checkIntersection());
4646

4747
function checkIntersection() {
4848
const result = $('.source').intersects('.target', {
Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,59 @@
1-
.scroll-container {
2-
height: 300px;
3-
overflow-y: auto;
4-
padding: var(--padding);
5-
background: var(--standard-1);
1+
.container {
2+
position: relative;
3+
width: 500px;
4+
height: 400px;
65
border: var(--border);
6+
margin: 50px;
77
border-radius: var(--border-radius);
8-
margin: var(--margin) 0;
8+
overflow: auto;
9+
10+
/* force scroll */
11+
.filler {
12+
width: 800px;
13+
height: 1600px;
14+
}
915
}
1016

11-
.test-element {
12-
width: 200px;
13-
height: 100px;
14-
background: var(--blue-10);
15-
border: 2px solid var(--blue-30);
16-
color: var(--blue-70);
17-
display: flex;
18-
align-items: center;
19-
justify-content: center;
20-
margin: 0 auto 20px;
17+
.box {
18+
position: absolute;
19+
padding: 15px;
20+
text-align: center;
21+
align-content: center;
2122
border-radius: var(--border-radius);
23+
text-align: center;
2224
font-weight: bold;
23-
font-size: 16px;
24-
transition: all 0.3s ease;
25-
}
25+
cursor: pointer;
26+
user-select: none;
27+
transition: var(--transition);
28+
transition-property: border, background;
2629

27-
.test-element.in-viewport {
28-
background: var(--green-10);
29-
border-color: var(--green-30);
30-
color: var(--green-70);
31-
transform: scale(1.05);
32-
box-shadow: 0 8px 25px var(--green-20);
33-
}
30+
&.draggable {
31+
width: 200px;
32+
height: 100px;
33+
top: 150px;
34+
left: 150px;
35+
background: var(--blue-background-color);
36+
border: var(--blue-border);
37+
color: var(--blue-text-color);
38+
}
3439

35-
.test-element.partially-visible {
36-
background: var(--orange-10);
37-
border-color: var(--orange-30);
38-
color: var(--orange-70);
39-
}
40+
&.in-view {
41+
background: var(--green-background-color);
42+
border-color: var(--green-border);
43+
color: var(--green-text-color);
44+
}
4045

41-
.spacer {
42-
height: 200px;
43-
display: flex;
44-
align-items: center;
45-
justify-content: center;
46-
color: var(--standard-50);
47-
font-style: italic;
48-
}
46+
&.partially-in-view {
47+
background: var(--orange-background-color);
48+
border: var(--orange-border);
49+
color: var(--orange-text-color);
50+
}
4951

50-
.spacer.bottom {
51-
height: 100px;
52-
}
53-
54-
ui-button {
55-
margin: 5px;
52+
&.out-of-view {
53+
background: var(--red-background-color);
54+
border: var(--red-border);
55+
color: var(--red-text-color);
56+
}
5657
}
5758

5859
.output {
@@ -61,22 +62,18 @@ ui-button {
6162
border-radius: var(--border-radius);
6263
padding: var(--padding);
6364
margin: var(--margin) 0;
64-
}
65-
66-
.output h3 {
67-
margin-top: 0;
68-
color: var(--standard-70);
69-
}
65+
66+
h3 {
67+
margin-top: 0;
68+
color: var(--standard-70);
69+
}
7070

71-
#viewport-info {
72-
background: var(--standard-0);
73-
padding: var(--padding);
74-
border-radius: var(--border-radius);
75-
border: var(--border);
76-
white-space: pre-wrap;
77-
font-family: monospace;
78-
font-size: 13px;
79-
line-height: 1.4;
80-
max-height: 200px;
81-
overflow-y: auto;
71+
pre {
72+
white-space: pre-wrap;
73+
font-family: monospace;
74+
font-size: 13px;
75+
line-height: 1.4;
76+
min-height: 200px;
77+
overflow-y: auto;
78+
}
8279
}
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
<p>Check viewport visibility with threshold and fully options</p>
2-
3-
<div class="scroll-container" id="scroll-area">
4-
<div class="spacer">Scroll down to see elements...</div>
5-
6-
<div class="test-element" data-name="Element 1">Element 1</div>
7-
<div class="spacer">Scroll more...</div>
8-
9-
<div class="test-element" data-name="Element 2">Element 2</div>
10-
<div class="spacer">Keep scrolling...</div>
11-
12-
<div class="test-element" data-name="Element 3">Element 3</div>
13-
<div class="spacer bottom">End of content</div>
1+
<div class="container">
2+
<div class="draggable box">Drag Me</div>
3+
<div class="filler"></div>
144
</div>
155

16-
<ui-button id="check-viewport">Check Viewport</ui-button>
17-
<ui-button id="check-threshold">Check 50% Threshold</ui-button>
18-
<ui-button id="check-fully">Check Fully Visible</ui-button>
19-
<ui-button id="scroll-to-middle">Scroll to Middle</ui-button>
20-
216
<div class="output">
22-
<h3>Viewport Status</h3>
23-
<pre id="viewport-info"></pre>
7+
<h3>Visibility Status</h3>
8+
<pre></pre>
249
</div>
Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,83 @@
11
import { $ } from '@semantic-ui/query';
22

3-
function checkViewportStatus(options = {}) {
4-
let info = 'CLIPPING PARENT VISIBILITY CHECK';
3+
// Drag functionality
4+
let isDragging = false;
5+
let $dragged = null;
6+
let offset = { top: 0, left: 0 };
57

6-
if (options.threshold !== undefined) {
7-
info += ` (${Math.round(options.threshold * 100)}% threshold)`;
8-
}
9-
if (options.fully) {
10-
info += ' (fully visible only)';
11-
}
12-
13-
info += ':\n\n';
14-
15-
$('.test-element').each((element) => {
16-
const $element = $(element);
17-
const name = $element.attr('data-name');
18-
const isVisible = $element.isInView(options);
19-
20-
info += `${name}: ${isVisible ? '✓ VISIBLE IN CONTAINER' : '✗ NOT VISIBLE IN CONTAINER'}\n`;
8+
// handle drag and drop start
9+
$('.box').on('mousedown', 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+
});
2120

22-
// Visual feedback
23-
$element.removeClass('in-viewport partially-visible');
24-
if (isVisible) {
25-
$element.addClass('in-viewport');
26-
}
27-
else if ($element.isInView()) {
28-
$element.addClass('partially-visible');
21+
// handle drag & drop
22+
$(document)
23+
.on('pointermove', function(event) {
24+
if (isDragging && $dragged) {
25+
moveElementToMouse(event);
2926
}
27+
})
28+
.on('pointerup', function() {
29+
isDragging = false;
30+
$dragged = null;
3031
});
3132

32-
// Check all elements together
33-
const allVisible = $('.test-element').isInView(options);
34-
info += `\nAll elements visible in container: ${allVisible ? '✓ YES' : '✗ NO'}`;
33+
// handle scroll
34+
$('.container').on('scroll', checkVisibility);
3535

36-
$('#viewport-info').text(info);
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();
3744
}
3845

39-
// Event handlers using Query
40-
$('#check-viewport').on('click', () => {
41-
checkViewportStatus();
42-
});
46+
// Initial check
47+
requestAnimationFrame(() => checkVisibility());
4348

44-
$('#check-threshold').on('click', () => {
45-
checkViewportStatus({ threshold: 0.5 });
46-
});
49+
function checkVisibility() {
50+
const $box = $('.draggable');
51+
const $container = $('.container');
4752

48-
$('#check-fully').on('click', () => {
49-
checkViewportStatus({ fully: true });
50-
});
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 });
5157

52-
$('#scroll-to-middle').on('click', () => {
53-
const container = $('#scroll-area').el();
54-
const scrollHeight = container.scrollHeight;
55-
const clientHeight = container.clientHeight;
56-
container.scrollTop = (scrollHeight - clientHeight) / 2;
58+
const containerDims = $container.dimensions();
5759

58-
// Check after scroll
59-
setTimeout(() => checkViewportStatus(), 100);
60-
});
60+
const info = `VISIBILITY STATUS:
6161
62-
// Check on scroll
63-
$('#scroll-area').on('scroll', () => {
64-
checkViewportStatus();
65-
});
62+
In View: ${basicCheck ? '✓ YES' : '✗ NO'}
63+
50% Visible: ${halfVisible ? '✓ YES' : '✗ NO'}
64+
Fully Visible: ${fullyVisible ? '✓ YES' : '✗ NO'}
6665
67-
// Initial check
68-
checkViewportStatus();
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)