برای بهبود ظاهر مقایسهگر تصاویر و حذف سایههای اضافی، میتوانید تغییرات زیر را در کد CSS اعمال کنید:
html Copy code
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>مقایسهگر تصاویر</title>
<style>
body {
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
font-family: system-ui, -apple-system, sans-serif;
}
.comparison-slider {
position: relative;
width: 100%;
max-width: 800px;
height: 600px;
overflow: hidden;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.comparison-slider img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
transition: clip-path 0.3s ease;
}
.before-image {
clip-path: inset(0 50% 0 0);
}
.after-image {
clip-path: inset(0 0 0 50%);
}
.slider-line {
position: absolute;
top: 0;
left: 50%;
width: 40px; /* افزایش عرض برای راحتی لمس */
height: 100%;
background-color: rgba(255, 255, 255, 0.8); /* کاهش شفافیت برای بهتر دیده شدن */
transform: translateX(-50%);
cursor: col-resize;
z-index: 10;
touch-action: none; /* جلوگیری از اسکرول روی این المان */
transition: left 0.3s ease;
}
.slider-button {
position: absolute;
top: 50%;
left: 50%;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: white;
transform: translate(-50%, -50%);
/* حذف سایه */
/* box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); */
display: flex;
align-items: center;
justify-content: center;
border: 2px solid #ccc; /* اضافه کردن حاشیه برای بهتر دیده شدن */
}
.slider-button::before,
.slider-button::after {
content: '';
position: absolute;
width: 8px;
height: 2px;
background-color: #666;
}
.slider-button::before {
transform: translateX(-5px);
}
.slider-button::after {
transform: translateX(5px);
}
.label {
position: absolute;
top: 20px;
padding: 5px 10px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
border-radius: 4px;
font-size: 14px;
z-index: 5;
}
.before-label {
left: 20px;
}
.after-label {
right: 20px;
}
</style>
</head>
<body>
<div class="comparison-slider">
<img class="before-image" src="https://uploadkon.ir/uploads/b6b716_24بارگیری-8-.jpeg" alt="تصویر بیکیفیت">
<img class="after-image" src="https://uploadkon.ir/uploads/112016_24Negar-1729092931854.jpg" alt="تصویر با کیفیت">
<div class="slider-line">
<div class="slider-button"></div>
</div>
<div class="label before-label">قبل</div>
<div class="label after-label">بعد</div>
</div>
<script>
const slider = document.querySelector('.comparison-slider');
const beforeImage = document.querySelector('.before-image');
const afterImage = document.querySelector('.after-image');
const sliderLine = document.querySelector('.slider-line');
let isResizing = false;
function setSliderPosition(x) {
const rect = slider.getBoundingClientRect();
let position = (x - rect.left) / rect.width;
position = Math.min(Math.max(position, 0), 1);
beforeImage.style.clipPath = `inset(0 ${(1 - position) * 100}% 0 0)`;
afterImage.style.clipPath = `inset(0 0 0 ${position * 100}%)`;
sliderLine.style.left = position * 100 + '%';
}
function onStart(event) {
isResizing = true;
event.preventDefault();
event.stopPropagation();
}
function onEnd(event) {
isResizing = false;
event.preventDefault();
event.stopPropagation();
}
function onMove(event) {
if (!isResizing) return;
const clientX = event.type === 'mousemove' ? event.clientX : event.touches[0].clientX;
setSliderPosition(clientX);
event.preventDefault();
event.stopPropagation();
}
sliderLine.addEventListener('mousedown', onStart);
sliderLine.addEventListener('touchstart', onStart, { passive: false });
document.addEventListener('mousemove', onMove); /* تغییر برای مستقیماً مستمع کردن بر روی document */
document.addEventListener('touchmove', onMove, { passive: false });
document.addEventListener('mouseup', onEnd); /* تغییر برای مستقیماً مستمع کردن بر روی document */
document.addEventListener('touchend', onEnd);
// جلوگیری از انتشار رویدادها به خارج از اسلایدر
slider.addEventListener('touchstart', (e) => e.stopPropagation(), { passive: false });
slider.addEventListener('touchmove', (e) => e.stopPropagation(), { passive: false });
slider.addEventListener('touchend', (e) => e.stopPropagation(), { passive: false });
</script>
</body>
</html>
با این تغییرات، اسلایدر مقایسه تصاویر شما بدون سایههای اضافی و با ظاهر بهبود یافتهای نمایش داده میشود. همچنین، حرکت اسلایدر نرمتر و کاربر پسندتر خواهد بود.