update img-slider

This commit is contained in:
2025-07-01 06:01:51 +09:00
parent cf93721bad
commit bc99eb0814
11 changed files with 487 additions and 2 deletions

View File

@ -1185,3 +1185,158 @@ article.article-content {
}
}
/* Image Comparison Slider Styles */
.img-comparison-container {
position: relative;
width: 100%;
max-width: 800px;
margin: 20px auto;
overflow: hidden;
border-radius: 8px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}
.img-comparison-slider {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
cursor: pointer;
}
.img-before,
.img-after {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.img-before {
z-index: 2;
clip-path: inset(0 50% 0 0);
}
.img-after {
z-index: 1;
}
.img-before img,
.img-after img {
width: 100%;
height: 100%;
object-fit: cover;
user-select: none;
pointer-events: none;
position: absolute;
top: 0;
left: 0;
}
.slider {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: transparent;
outline: none;
cursor: pointer;
z-index: 4;
opacity: 0;
-webkit-appearance: none;
appearance: none;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 0;
height: 0;
}
.slider::-moz-range-thumb {
width: 0;
height: 0;
border: none;
background: transparent;
}
.slider-thumb {
position: absolute;
top: 0;
left: 50%;
width: 4px;
height: 100%;
background: #ffffff;
z-index: 3;
pointer-events: none;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);
transform: translateX(-50%);
}
.slider-thumb::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 40px;
height: 40px;
background: #ffffff;
border: 2px solid var(--theme-color);
border-radius: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.slider-thumb::after {
content: '↔';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: var(--theme-color);
font-size: 16px;
font-weight: bold;
z-index: 1;
}
/* Responsive design */
@media (max-width: 768px) {
.img-comparison-container {
margin: 15px auto;
border-radius: 6px;
}
.img-comparison-slider {
height: 250px;
}
.slider-thumb::before {
width: 32px;
height: 32px;
}
.slider-thumb::after {
font-size: 14px;
}
}
@media (max-width: 480px) {
.img-comparison-slider {
height: 200px;
}
.slider-thumb::before {
width: 28px;
height: 28px;
}
.slider-thumb::after {
font-size: 12px;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

@ -0,0 +1,123 @@
/**
* Image Comparison Slider
* UE5-style before/after image comparison component
*/
class ImageComparison {
constructor(container) {
this.container = container;
this.slider = container.querySelector('.slider');
this.beforeImg = container.querySelector('.img-before');
this.afterImg = container.querySelector('.img-after');
this.sliderThumb = container.querySelector('.slider-thumb');
this.isDragging = false;
this.containerRect = null;
this.init();
}
init() {
this.bindEvents();
this.updatePosition(50); // Start at 50%
}
bindEvents() {
// Mouse events
this.slider.addEventListener('input', (e) => {
this.updatePosition(e.target.value);
});
this.slider.addEventListener('mousedown', () => {
this.isDragging = true;
document.body.style.userSelect = 'none';
});
document.addEventListener('mouseup', () => {
if (this.isDragging) {
this.isDragging = false;
document.body.style.userSelect = '';
}
});
// Touch events for mobile
this.slider.addEventListener('touchstart', (e) => {
this.isDragging = true;
e.preventDefault();
});
this.slider.addEventListener('touchmove', (e) => {
if (this.isDragging) {
const touch = e.touches[0];
this.containerRect = this.container.getBoundingClientRect();
const x = touch.clientX - this.containerRect.left;
const percentage = Math.max(0, Math.min(100, (x / this.containerRect.width) * 100));
this.slider.value = percentage;
this.updatePosition(percentage);
e.preventDefault();
}
});
this.slider.addEventListener('touchend', () => {
this.isDragging = false;
});
// Direct click on container
this.container.addEventListener('click', (e) => {
if (e.target === this.container || e.target.classList.contains('img-comparison-slider')) {
this.containerRect = this.container.getBoundingClientRect();
const x = e.clientX - this.containerRect.left;
const percentage = Math.max(0, Math.min(100, (x / this.containerRect.width) * 100));
this.slider.value = percentage;
this.updatePosition(percentage);
}
});
// Keyboard support
this.slider.addEventListener('keydown', (e) => {
let value = parseFloat(this.slider.value);
switch (e.key) {
case 'ArrowLeft':
value = Math.max(0, value - 1);
break;
case 'ArrowRight':
value = Math.min(100, value + 1);
break;
case 'Home':
value = 0;
break;
case 'End':
value = 100;
break;
default:
return;
}
e.preventDefault();
this.slider.value = value;
this.updatePosition(value);
});
}
updatePosition(percentage) {
const position = parseFloat(percentage);
// Update clip-path for before image to show only the left portion
this.beforeImg.style.clipPath = `inset(0 ${100 - position}% 0 0)`;
// Update slider thumb position
this.sliderThumb.style.left = `${position}%`;
this.sliderThumb.style.transform = `translateX(-50%)`;
}
}
// Auto-initialize all image comparison components
document.addEventListener('DOMContentLoaded', function() {
const comparisons = document.querySelectorAll('.img-comparison-container');
comparisons.forEach(container => {
new ImageComparison(container);
});
});
// Export for manual initialization
window.ImageComparison = ImageComparison;