<div class="comparison-container">
<div class="comparison-demo">
<div class="comparison-header">
<h2>Comparación Antes y Después</h2>
<p>Arrastra el control deslizante para ver la transformación</p>
</div>
<div class="image-comparison" id="imageComparison">
<div class="image-container">
<img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=600&h=400&fit=crop" alt="Después" class="after-image">
<img src="https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=600&h=400&fit=crop" alt="Antes" class="before-image">
<div class="slider-handle" id="sliderHandle">
<div class="handle-line"></div>
<div class="handle-label">Arrástrame</div>
</div>
</div>
<div class="comparison-labels">
<div class="label before-label">Antes</div>
<div class="label after-label">Después</div>
</div>
</div>
</div>
</div>
.comparison-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 40px 20px;
border-radius: 20px;
max-width: 700px;
margin: 0 auto;
}
.comparison-demo {
background: white;
border-radius: 15px;
padding: 30px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}
.comparison-header {
text-align: center;
margin-bottom: 30px;
}
.comparison-header h2 {
margin: 0 0 10px 0;
color: #333;
font-size: 2rem;
font-weight: 700;
}
.comparison-header p {
color: #666;
font-size: 1.1rem;
margin: 0;
}
.image-comparison {
position: relative;
width: 100%;
max-width: 600px;
margin: 0 auto;
}
.image-container {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
border-radius: 12px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}
.before-image,
.after-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.before-image {
clip-path: inset(0 50% 0 0);
z-index: 2;
}
.after-image {
z-index: 1;
}
.slider-handle {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
width: 4px;
height: 100%;
background: white;
cursor: ew-resize;
z-index: 3;
display: flex;
align-items: center;
justify-content: center;
}
.handle-line {
position: absolute;
width: 100%;
height: 100%;
background: white;
}
.handle-label {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(90deg);
background: rgba(102, 126, 234, 0.9);
color: white;
padding: 8px 12px;
border-radius: 20px;
font-size: 0.8rem;
font-weight: 600;
white-space: nowrap;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.slider-handle::before,
.slider-handle::after {
content: '';
position: absolute;
width: 40px;
height: 40px;
background: white;
border-radius: 50%;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
z-index: -1;
}
.slider-handle::before {
top: 20px;
left: 50%;
transform: translateX(-50%);
}
.slider-handle::after {
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
.comparison-labels {
display: flex;
justify-content: space-between;
margin-top: 20px;
padding: 0 20px;
}
.label {
font-weight: 600;
color: #333;
font-size: 1.2rem;
padding: 8px 16px;
border-radius: 25px;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.before-label {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
/* Animaciones */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.image-container {
animation: fadeIn 0.6s ease-out;
}
.comparison-labels {
animation: fadeIn 0.6s ease-out 0.3s both;
}
/* Responsivo */
@media (max-width: 768px) {
.comparison-container {
padding: 30px 15px;
}
.comparison-demo {
padding: 20px;
}
.comparison-header h2 {
font-size: 1.7rem;
}
.image-container {
height: 300px;
}
.handle-label {
font-size: 0.7rem;
padding: 6px 10px;
}
.label {
font-size: 1rem;
padding: 6px 12px;
}
}
@media (max-width: 480px) {
.image-container {
height: 250px;
}
.comparison-labels {
padding: 0 10px;
}
.label {
font-size: 0.9rem;
}
}
document.addEventListener('DOMContentLoaded', function() {
// Obtener elementos del DOM
const imageComparison = document.getElementById('imageComparison');
const sliderHandle = document.getElementById('sliderHandle');
const beforeImage = document.querySelector('.before-image');
const imageContainer = document.querySelector('.image-container');
let isDragging = false;
// Inicializar posición del control deslizante
function initSlider() {
const containerWidth = imageContainer.offsetWidth;
const initialPosition = containerWidth / 2;
updateSliderPosition(initialPosition);
}
// Actualizar posición del control deslizante
function updateSliderPosition(position) {
const containerWidth = imageContainer.offsetWidth;
const percentage = (position / containerWidth) * 100;
// Limitar posición entre 0 y el ancho del contenedor
const clampedPosition = Math.max(0, Math.min(position, containerWidth));
const clampedPercentage = Math.max(0, Math.min(percentage, 100));
// Actualizar posición del control deslizante
sliderHandle.style.left = `${clampedPercentage}%`;
// Actualizar recorte de imagen anterior
beforeImage.style.clipPath = `inset(0 ${100 - clampedPercentage}% 0 0)`;
}
// Eventos del mouse
sliderHandle.addEventListener('mousedown', function(e) {
isDragging = true;
e.preventDefault();
});
document.addEventListener('mousemove', function(e) {
if (!isDragging) return;
const containerRect = imageContainer.getBoundingClientRect();
const position = e.clientX - containerRect.left;
updateSliderPosition(position);
});
document.addEventListener('mouseup', function() {
isDragging = false;
});
// Eventos táctiles para móviles
sliderHandle.addEventListener('touchstart', function(e) {
isDragging = true;
e.preventDefault();
});
document.addEventListener('touchmove', function(e) {
if (!isDragging) return;
const containerRect = imageContainer.getBoundingClientRect();
const position = e.touches[0].clientX - containerRect.left;
updateSliderPosition(position);
});
document.addEventListener('touchend', function() {
isDragging = false;
});
// Manejar cambio de tamaño de ventana
window.addEventListener('resize', initSlider);
// Inicializar control deslizante
initSlider();
});