This issue started when I realized that the slide feature on my web application was not working on mobile, but was working on desktop. I have a table full of rows that each have an “x” next to them. When an “x” is clicked, I want the row to slide left and display a delete button. When it is clicked again, I want the row to slide back to its original position. I found I could do this by simply putting overflow: hidden
on the table
element and using left
with a transition
property on each tr
. The following code works for me on desktop, but not for mobile:
CSS:
table {
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
width: 100%;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
position: relative;
overflow: hidden;
}
tbody tr {
border-bottom: 1px solid #dddddd;
position: relative;
left: 0;
transition: left 0.5s;
}
.swipe {
left: -70px;
}
HTML/PHP:
echo "
<div class='history'>
<h2 id='weight-history-title' align='center'>Weight History</h2>
<table>
<thead>
<tr>
<th class='th_border'>Date Weighed</th>
<th>Weight</th>
<td></td>
</tr>
</thead>
<tbody>
";
while($row=$selectQuery->fetch_assoc()){
$dateWeighed = htmlspecialchars(date("m/d/y", strtotime($row["date_weighed"])));
$weight = htmlspecialchars($row["pounds"]);
$id = htmlspecialchars($row["id"]);
echo "
<tr>
<td>$dateWeighed</td>
<td>$weight lbs</td>
<td><img class='delete-icon' src='images/delete.png' style='cursor: pointer; width: 15px; height: 15px;'></td>
<td class='delete'><a href='delete.php?id=$id' class='delete-button'>Delete</a></td>
</tr>
";
}
echo "
</tbody>
</table>
";
JavaScript:
for(const x of document.querySelectorAll(".delete-icon")){
x.addEventListener("click", function(){
let tr = x.parentElement.parentElement;
tr.classList.toggle("swipe");
alert("tagname: " + tr.tagName + "nclassname: " + tr.className + "nposition: " + getComputedStyle(tr).position);
});
}
When an “x” is clicked, the class swipe
is added to its parent row which should slide 70px
to the left, but on mobile, nothing happens when the “x” is clicked.
Through some research, I found out that the reason that it wasn’t working on mobile was because position: relative
was not getting applied to each tr
. To double check this, I made JavaScript alert the position
property of each tr
when clicked. On desktop, it alerts position: relative
, while on mobile it alerts position: static
.
Why would the position
property be getting ignored on mobile?
NOTE: the code works when you preview a mobile screen through Developer Tools on desktop browser. You have to actually view the webpage on an IOS/Android device to see the issue. The table is at the bottom of the page.
Webpage URL: https://madlibsrandomgenerator.000webhostapp.com/weight/index.php