Sign Up Form

Sign Up

Substitute photos after and before by scaling ?

1024 683 point-admin
  • 0

To substitute photos with a scaling effect, you can use CSS and JavaScript to animate the transition. Here’s an example to achieve this:

HTML :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Substitution with Scaling</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="photo-container">
<img id="beforePhoto" src="before.jpg" alt="Before Photo">
<img id="afterPhoto" src="after.jpg" alt="After Photo" class="hidden">
</div>
<button onclick="togglePhotos()">Toggle Photos</button>
<script src="script.js"></script>
</body>
</html>

CSS (styles.css)

.photo-container {
position: relative;
width: 300px; /* Adjust as needed */
height: 300px; /* Adjust as needed */
}

.photo-container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: transform 0.5s ease;
}

.hidden {
transform: scale(0);
opacity: 0;
pointer-events: none;
}

.visible {
transform: scale(1);
opacity: 1;
pointer-events: auto;
}

JavaScript (script.js) :


function togglePhotos() {
const beforePhoto = document.getElementById('beforePhoto');
const afterPhoto = document.getElementById('afterPhoto');

if (beforePhoto.classList.contains('hidden')) {
beforePhoto.classList.remove('hidden');
beforePhoto.classList.add('visible');
afterPhoto.classList.remove('visible');
afterPhoto.classList.add('hidden');
} else {
beforePhoto.classList.remove('visible');
beforePhoto.classList.add('hidden');
afterPhoto.classList.remove('hidden');
afterPhoto.classList.add('visible');
}
}

  • HTML: Contains two images within a container and a button to toggle the images.
  • CSS: Defines the photo container and sets up the scaling transition for the images. The .hidden class scales the image down and hides it, while the .visible class scales it up and shows it.
  • JavaScript: The togglePhotos function switches the visibility of the photos by toggling the hidden and visible classes.

Leave a Reply

Your email address will not be published.