|
1 | 1 | import { $ } from '@semantic-ui/query'; |
2 | 2 |
|
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