Demo available at the bottom of the page
Within my code, there is a div called #myDiv that defaults to an opacity of 0.
Upon hovering over #myDiv, the opacity changes to 1. See the CSS snippet below:
#myDiv {
opacity: 0;
}
#myDiv:hover {
opacity: 1;
}
Additionally, there is an
tag inside the div with a default width of 0 and transitions to 100% on hover:
hr {
width: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#myDiv:hover hr {
width: 100%;
}
A script on the website detects mobile users and sets the opacity to 1 if the div is visible without triggering the hover effect.
I am looking for a way to apply the
effects on mobile devices. While I initially considered using an if statement in CSS, it's not possible. The best solution may involve JavaScript. Here is what I had in mind:
if (#myDiv.opacity == 1) {
hr {
width: 100%;
}
}
Since this cannot be achieved with CSS alone, I'm seeking guidance on how to implement this functionality using JavaScript, as I have limited experience with it.
To see a working demo on desktop, please visit: My Work and hover over the project section. How can I replicate this behavior on mobile?