I am making a simple Iphone IOS project in which there will be an Iphone frame and user will be able to use it as a phone and some basic features in it like camera, calendar, time etc…. so I was thinking to apply drag up gesture in it just like normal phones have but the drag function in not working I am an beginner so I don’t have so much of knowledge in html css and js so please help me solve the issue.
I was making the ios iphone replica project and trying to apply drag up gesture unlock feature in it so I wrote some html css and js for it
const iphone = document.querySelector('.iphone');
const mainScreen = document.querySelector('.main-screen');
const controlBar = document.querySelector('.control');
const lockScreen = document.querySelector('.lockscreen')
let isLocked = true;
let startY = 0;
let currentY = 0;
function unlocked() {
let isLocked = false;
mainScreen.style.display = 'flex';
lockScreen.style.display = 'none';
}
// Hover effect to show control bar
iphone.addEventListener('mouseenter', () => {
if (isLocked) {
controlBar.style.opacity = 1;
iphone.style.cursor = 'pointer';
}
});
iphone.addEventListener('mouseleave', () => {
if (isLocked) {
controlBar.style.opacity = 0;
}
});
// Drag gesture to unlock
iphone.addEventListener('mousedown', (e) => {
if (isLocked) {
startY = e.clientY;
currentY = startY;
iphone.addEventListener('mousemove', drag);
}
});
window.addEventListener('mouseup', () => {
if (isLocked) {
iphone.removeEventListener('mousemove', drag);
if (currentY - startY < -100) {
unlockScreen();
}
}
});
function drag(e) {
currentY = e.clientY;
}
function unlockScreen() {
isLocked = false;
mainScreen.style.transform = 'translateY(0)';
controlBar.style.opacity = 1;
iphone.style.cursor = 'default';
}
@import url('https://fonts.googleapis.com/css2?family=Ubuntu&display=swap');
* {
margin: 0px;
padding: 0px;
font-family: 'Ubuntu', sans-serif;
}
/* Update the positioning for the info sections */
.info-left,
.info-right {
font-size: 12px;
/* Adjust the size as needed */
}
/* Update the positioning for the info sections */
.info-left {
position: absolute;
top: 5px;
/* Adjust the vertical position as needed */
left: 10px;
}
.info-right {
position: absolute;
top: 5px;
right: 10px;
display: flex;
align-items: center;
color: white;
}
.info-right .signal {
margin-right: 5px;
/* Adjust the margin as needed */
}
.info-right .network-type {
margin-right: 5px;
/* Adjust the margin as needed */
}
.iphone {
background-image: url(ios-wallpaper.jpg);
background-repeat: no-repeat;
background-size: cover;
width: 23vw;
height: 95vh;
margin: auto;
color: white;
border: 10px solid black;
border-radius: 30px;
position: relative;
}
.notch {
background-color: black;
display: flex;
height: 4vh;
width: 9vw;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
align-items: center;
justify-content: center;
position: relative;
left: 80px;
}
.speaker {
background-color: rgba(48, 47, 47, 0.6);
box-shadow: 2px solid rgb(88, 88, 88);
height: 3px;
width: 20px;
border-radius: 4px;
margin-left: 5px;
}
.front-cam {
margin-left: 5px;
position: absolute;
left: 5px;
top: 6px;
height: 14px;
width: 14px;
box-shadow: inset 0 0 1px rgba(26, 25, 25, 0.2);
}
.lens {
height: 6px;
width: 6px;
border-radius: 50%;
background-color: rgba(216, 216, 216, 0.7);
display: flex;
align-items: center;
justify-content: center;
box-shadow: inset 0 0 1px rgba(0, 0, 0, 0.5);
}
.lens img {
height: 12px;
width: 13px;
}
.DateAndTime {
text-align: center;
position: relative;
top: 15vh;
}
.bottom {
position: relative;
bottom: -350px;
}
.flashlight {
position: relative;
background-color: black;
height: 6vh;
width: 3vw;
left: 10px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 500px;
}
.camera {
position: relative;
background-color: black;
height: 6vh;
width: 3vw;
right: -210px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 500px;
bottom: 6vh;
}
.control {
width: 10vw;
height: 3px;
border-radius: 20px;
background-color: white;
display: flex;
justify-content: center;
align-items: center;
position: relative;
left: 30%;
bottom: -40px;
opacity: 0;
transition: opacity 0.3s;
}
.main-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: white;
/* Customize the background color */
display: none;
/* Hide by default */
transition: transform 0.3s ease;
z-index: -1;
/* Place it behind the lock screen */
}
.iphone.hovered.control {
opacity: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/fontawesome.min.css" integrity="sha384-QYIZto+st3yW+o8+5OHfT6S482Zsvz2WfOzpFSXMF9zqeLcFV0/wlZpMtyFcZALm" crossorigin="anonymous">
<title>IOS : Replica</title>
</head>
<body>
<div class="container">
<div class="iphone">
<div class="notch">
<div class="speaker"></div>
<div class="front-cam">
<div class="lens"><img src="lens.png" alt=""></div>
</div>
</div>
<div class="lockscreen">
<div class="DateAndTime">
<h1 class="lock-time">11:57</h1>
<p class="date">Saturday, 26 August</p>
</div>
<div class="info-left">
<div class="operator">
Airtel
</div>
</div>
<div class="info-right">
<div class="signal">
<i class="fas fa-signal"></i>
</div>
<div class="network-type">
5G
</div>
<ion-icon name="battery-half-outline"></ion-icon>
</div>
<div class="bottom">
<div class="control"></div>
<div class="flashlight">
<ion-icon name="flashlight"></ion-icon>
</div>
<div class="camera">
<i class="fas fa-camera"></i>
</div>
</div>
</div>
<div class="main-screen"></div>
</div>
</div>
</body>
<script type="module" src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.js"></script>
<script src="script.js"></script>
</html>
Please check the code and someone help me in this problem I want that the main screen (which is currently empty) should appear when someone drags up on the screen or the control bar and the lock screen should disappear but it is not happening
2
Answers
Within the
mouseup
callback if you add thecurrentY=e.clientY
, making sure that the event handler assignse
as the event, then I think your code does what you want. I added some CSS to help illustrate what happens when you drag up by more than designated amount (screen will turn green)Read this article on pointer events.
Details are commented in example
View in Full page mode — Initial display within
<iframe>
is not rendered correctly